# double precition on calculation and serial.print

i have a double that has 7 digits since it is 4 byte when i print it and ask to print more than 7 digits i get a number slightly different.

What i am concerned is if this happens only at Serial.print. If i try to use the number to calculation will it give the wrong number or the correct?

Doubles on Arduino are actually floats.

http://en.wikipedia.org/wiki/Floating_point

i know but can you answer to my question?

http://arduino.cc/en/Reference/Float Floats have only 6-7 decimal digits of precision. That means the total number of digits, not the number to the right of the decimal point.

You use the second parameter for printing floats/doubles ?

``````Serial.println(1.234567, 4) gives "1.2346"
``````

i know all that thank you for your help but the question is different.

i know all that thank you for your help but the question is different.

No, it isn't. You are being told that the string that is printed will only be an approximation of the actual value, and that the accuracy of the actual value is only 6 to 7 digits.

That covers every possibility. There are no other ways to answer your question.

If you (think you) need 32 decimal places of accuracy, no computer in the world is going to help you. A reality check is in order.

kyrpav:
i know but can you answer to my question?

kyrpav:
i know all that thank you for your help but the question is different.

Oh, right, I see then.

Read this before posting a programming question

Then this:

How to use this forum

i have a double …

What double?

that has 7 digits

What do you mean “it has 7 digits”?

since it is 4 byte when i print it and ask to print more than 7 digits

i get a number slightly different.

And what would that different number be?

What i am concerned is if this happens only at Serial.print.

No, it will happen with all code when you make a human readable presentation. As stated above the Arduino double is implemented as a float which is an IEEE&54 implementation of a floating point number.

IIRC This representation has 23 bits for the mantissa and 8 bits for the exponent (power of 2) and 1 sign bit. With 23 bits it is possible to have max 7.2 digits precision in decimal notation. The fact that this is a float number itself means that in practice you have between 6 and 7 digits right.

Printing a floating point implicit means rounding it to the nearest number to keep the rounding error less than half the last digit. Printing more digits means that you do the rounding on another level , until you have more than 7 digits, then you are in fact just rounding noise.

So far I have not seen a proper 8 byte double implementation for the Arduino, but there is a big-number library (ported by Nick Gammon, search the forum) that has arbitrary precision and works very well unless you are in the need for speed.

Finally working with floats introduces rounding errors in your math. That is the nature of the float type and although you can keep it as minimal as possible the error will grow. That means results can differ quite a bit, depending on your implementation of a certain algorithm.