Go Down

Topic: Arduino calculating math incorrectly? (Read 846 times) previous topic - next topic

nate890

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

while it should be
>552
>182.805

Why is this occurring?

Arrch


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

Tried making it a double to see if it would work, would have made sense to make it a float.

Thanks!

wildbill

Float and double are the same size for the Arduino

nate890

Really? Hm. What's the point in having both then?

marco_c

#5
Jul 24, 2012, 02:15 am Last 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

nate890

"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?

Coding Badly


Because the C++ standard requires both types.

Arrch


"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

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy