Printing float to serial


I'm trying to perform some very simple math and simply output to the serial console, but it's not returning the expected result.

double msec = 1.00 / 3600000.00;
Serial.println(msec, 20);

Should return something like: 2.77777778
Instead I am getting: 0.00000027777779102325
I set it to output to 20 places as otherwise is was displaying: 0.000000

It is successful in doing the math, just wondering why it has all the preceding zeros?

Check your math! :slight_smile:

1 divided by 3,600,000 is not even near 2.

Expecting 20 places of decimals is a bit over-optimistic for a 32 bit float.

Your right! Where the hell was I getting that value from I was saying?? lol thanks.. it was right all along..
How is it able to print out to 20 decimal places if the doc says it only supports upto 6/7?

The smallest representable "float" value (FLT_MIN) is something like 1.1x10-38, but the representation only guarantees about six or seven places of decimals.

So, for instance, you can represent 1.0 in one "float" variable and FLT_MIN in another, but if you add them together, the result is still 1.0.
You can, however, add FLT_MIN to FLT_MIN and get 2 * FLT_MIN, just as you can add 1.0 to 1.0 and get 2.0