Go Down

### Topic: Arduino calculating math incorrectly? (Read 1 time)previous topic - next topic

#### nate890

##### Jul 24, 2012, 12:15 am
Serial.println(LightValue);
Serial.println(LightValue/770*255);
>552
>0

while it should be
>552
>182.805

Why is this occurring?

#### Arrch

#1
##### Jul 24, 2012, 12:18 am

Serial.println(LightValue);
Serial.println(LightValue/770*255);
>552
>0

while it should be
>552
>182.805

Why is this occurring?

That is correct.

The problem is you are expecting floating point math, but telling the compiler that you want integer math:
Code: [Select]
`Serial.println(LightValue);Serial.println(LightValue/770.0*255.0);`

#### nate890

#2
##### Jul 24, 2012, 12:24 am
Tried making it a double to see if it would work, would have made sense to make it a float.

Thanks!

#### wildbill

#3
##### Jul 24, 2012, 12:50 am
Float and double are the same size for the Arduino

#### nate890

#4
##### Jul 24, 2012, 01:29 am
Really? Hm. What's the point in having both then?

#### marco_c

#5
##### Jul 24, 2012, 02:15 amLast Edit: Jul 24, 2012, 03:07 am by marco_c Reason: 1
Quote
for the Arduino

That is not the case for other implementations of C/C++. The bigger the precision the more it takes to calculate, especially as there is no floating point hardware support. How many decimal places to you need?
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

#### nate890

#6
##### Jul 24, 2012, 02:24 am
"That is not the case for other implementations of C/C++."
I know. But, since arduino's float counts as double and double counts as float, why have both?

#7
##### Jul 24, 2012, 02:29 am

Because the C++ standard requires both types.

#### Arrch

#8
##### Jul 24, 2012, 02:39 am

"That is not the case for other implementations of C/C++."
I know. But, since arduino's float counts as double and double counts as float, why have both?

Portability.

Go Up

Please enter a valid email to subscribe