cast to int

Hi Fellows,
Anybody knows why the code:

Serial.println( int(4.95 * 100.0) );

gives 494 in Serial Port Monitor widnow?


It gives me 495 here
Nano 33 IOT, Windows 10, IDE 1.8.10

Which board, OS and version of the IDE are you using ?

A float representation of "4.95" is: "4.949999980926513671875..."

These are compile-time constants, so even if double is the same as float on whatever flavor of Arduino you're compiling for, the compiler could still calculate numbers in double-precision.

If the compiler handles these values in double-precision floating point, then it would be 4.95000000000000017763568394003 * 100.0, which it then rounds to 495 when converted to int. Otherwise it's 4.94999980926513671875 * 100.0, which is rounded to 494.

Hi Fellows, Thanks for all the answers. So it seems that it is a matter of coding the number... christop, how to check the exact value (with all digits after the comma) of the constant which is used by the compler as you presented in you answer?

On Arduino UNO, let us execute this code: Serial.println(4.95*100.0); to get 495.00.