More Decimals In Float Variable

Hello,
I want to know how am I supposed to store more decimals (am I right with this word? sorry English is not my language) in a Float variable.
For example, (127.0 x 6.144) / 32768.0 = 0.0238125
But when I calculate it with an Arduino, I just get 0.02:

float ADC = analogRead(A0); //actually I receive the value from a 16bit ADC but the example would be so long so just forget about it
float voltage = ( ADC * 6.144 ) / 32768.0; //now, the float voltage just wont be more than two decimals (0.XX)

How should I fix it? I need very small values for my application.

But when I calculate it with an Arduino, I just get 0.02:

I suspect that you mean that when you print it you only get 2 decimal places

Try

Serial.print(floatVariable, 6);  //print 6 decimal places rather than the default of 2

No, when you print it you get 0.02 because .print defaults to this precision. The number is still correct.

You need to make .print show more digits. From the Arduino library documentation:

Serial.print(1.23456, 0) gives "1"

Serial.print(1.23456, 2) gives "1.23"

Serial.print(1.23456, 4) gives "1.2345"

EDIT: Bob just beat me to this!

marco_c:
No, when you print it you get 0.02 because .print defaults to this precision. The number is still correct.

You need to make .print show more digits. From the Arduino library documentation:

EDIT: Bob just beat me to this!

Oh so the value wont be cut to the shown number? ok that's good.

One more question, in the Arduino reference page in the float variable page, you can see this paragraph:

Floating point numbers are not exact, and may yield strange results when compared. For example 6.0 / 3.0 may not equal 2.0. You should instead check that the absolute value of the difference between the numbers is less than some small number.

Can you please explain this paragraph for me? I don't get it, does it mean that the calculated values are not correct all the time?

Setrik_aZ:
One more question, in the Arduino reference page in the float variable page, you can see this paragraph:

Can you please explain this paragraph for me? I don't get it, does it mean that the calculated values are not correct all the time?

Without getting into a long digression, the simple answer is, "yes they are not correct all the time". Really, it's "their values are never considered exact".

Trying to explain why to you, will only confuse you more.

aarg:
Trying to explain why to you, will only confuse you more.

An analogy makes it pretty clear - Try writing down the exact representation of 1/7 as a decimal number: 1/7 = 0.141857..... How many digits do you have to include before the result is EXACTLY 1/7?

aarg:
Without getting into a long digression, the simple answer is, "yes they are not correct all the time". Really, it's "their values are never considered exact".

Trying to explain why to you, will only confuse you more.

By "Getting into a long digression" you mean using long(variable) function?

gfvalvo:
An analogy makes it pretty clear - Try writing down the exact representation of 1/7 as a decimal number: 1/7 = 0.141857..... How many digits do you have to include before the result is EXACTLY 1/7?

Ooooh I get it now, thanks.:slight_smile:

One thing to bear in mind is that the analog read has a resolution of 1part in 1024 , doing calculations in float may lead you think you are getting more accuracy than is possible , for example a result such as 2000.4 may be meaningless .

Also be aware that there will be numbers that can be expressed exactly in decimal that cannot be expressed exactly in a float, which is based on powers of 2 not 10. The reverse will also be true.

[Edit]
See reply #13 and subsequent discussion.

hammy:
One thing to bear in mind is that the analog read has a resolution of 1part in 1024 , doing calculations in float may lead you think you are getting more accuracy than is possible , for example a result such as 2000.4 may be meaningless .

PerryBebbington:
Also be aware that there will be numbers that can be expressed exactly in decimal that cannot be expressed exactly in a float, which is based on powers of 2 not 10. The reverse will also be true.

Thanks for the tips.

Setrik_aZ:
Can you please explain this paragraph for me? I don't get it, does it mean that the calculated values are not correct all the time?

Float variables can accurately handle anything that's a power of 2 - 1/2, 1/4, 1/8, 1/16 and so on. Anything else it has to approximte the value to the nearest value that it can represent. If it helps - think of Pi, or the square root of 2, or even 1/3. You cannot express these values precisely as a decimal number, no matter how many digits you have. Their decimal expansions are infinite.

PerryBebbington:
Also be aware that there will be numbers that can be expressed exactly in decimal that cannot be expressed exactly in a float, which is based on powers of 2 not 10. The reverse will also be true.

Not so. Any number which can be expressed precisely as a finite binary float can also be expressed in decimal. 1/2=.5, 1/4=.25, 1/8=.125, 1/16=.0625 and so on. Notice how those numbers - 5,25,125,625, are powers of 5. This is not a coincidence :slight_smile: .

PaulMurrayCbr:
Float variables can accurately handle anything that's a power of 2 - 1/2, 1/4, 1/8, 1/16 and so on. Anything else it has to approximte the value to the nearest value that it can represent. If it helps - think of Pi, or the square root of 2, or even 1/3. You cannot express these values precisely as a decimal number, no matter how many digits you have. Their decimal expansions are infinite.

No, but you can express Pi exactly in 2 digits; you just set your base to Pi then Pi itself becomes 10.

PaulMurrayCbr:
Not so. Any number which can be expressed precisely as a finite binary float can also be expressed in decimal. 1/2=.5, 1/4=.25, 1/8=.125, 1/16=.0625 and so on. Notice how those numbers - 5,25,125,625, are powers of 5. This is not a coincidence :slight_smile: .

++Karma; // I don't know if you are right or wrong, I suspect wrong but I don't think I have the mathematical ability to test it. Your argument looks reasonable, but I can't help thinking there is a flaw. Anyway, the karma is for giving me something to think about, which I appreciate.

PaulMurrayCbr:
Not so. Any number which can be expressed precisely as a finite binary float can also be expressed in decimal. 1/2=.5, 1/4=.25, 1/8=.125, 1/16=.0625 and so on. Notice how those numbers - 5,25,125,625, are powers of 5. This is not a coincidence :slight_smile: .

Do you agree that this is true one way round: there are numbers that can be expressed precisely in decimal with a finite number of digits but which cannot be expressed precisely in binary with any number of digits?

PerryBebbington:
Do you agree that this is true one way round: there are numbers that can be expressed precisely in decimal with a finite number of digits but which cannot be expressed precisely in binary with any number of digits?

Yes. 1/5.

PaulMurrayCbr:
Yes. 1/5.

Ah! That makes more sense. I have edited reply #10 to correct it while leaving the original text in place. Is it now true?

For a fraction to be represented exactly using a finite number of digits to the right of the Radix Point, its denominator must be representable as the product of powers of the radix's prime factors.

So for Base 10, the fraction's denominator must be representable as: 2^k * 5^m where k and m are integers >= 0.

For Base 2, the fraction's denominator must be representable as: 2^k where k is an integer >= 0.