 # RE: Decimal Digits of Precision

Hi Gang

Some time ago I started a thread hoping to better understand floats specifically decimal digits of precision.

RE: Decimal Digits of Precision

Initially it seemed straight forward. Pi for instance was only worth recoding six/seven digits (see below).

const float pi = 3.14159

The last post by JimEli however cast doubt on this simplified approach. Since then I've read about how Arduino stores floats (IEEE-754 Format). I've read about Two's Compliment. I've even read about Significant Digits. Unfortunately I'm not having a light bulb moment. :(

It appears that it's a rather convoluted process converting a decimal number into a single precision binary number. In addition the conversion from decimal to binary presents a number of problems such as rounding errors.

In a nutshell when using floats where do I draw the line?

const float pi = 3.141592653589...

Should I be running the decimal number I wish to use through something like the following? If so is there something specific I should be looking for?

https://www.h-schmidt.net/FloatConverter/IEEE754.html

Am I overthinking the problem and stick to six significant decimal digits?

const float pi = 3.14159

OR

Any help would be greatly appreciated.

Cheers

Jase :)

How many decimal places precision do you need ? Do you need to work with floats at all ?

Hi UKHeliBob

Thanks for your reply. That's a good question. I would assume the more digits the better but there's a limit.

I understand that it's desirable to avoid floats and maybe in the future when I get comfortable with the numbers I'll take that next step.

Cheers

Jase :)

In a nutshell when using floats where do I draw the line?

Use the link that you provided. Enter 3.14 and hit the tab key. Add a 1 and tab again.

When the stored value quits changing, you've passed the number of significant digits, and can stop typing more digits after the decimal point.

Hi PaulS

I assume you mean the following link.

IEEE-754 Floating Point Converter

If I type 3.14 in the 'You entered' field then press Tab the other fields get populated. If I then click on the +1 button the fields appear to change but I can't work out what's happening? The article below doesn't seem to explain this either. Can you explain?

Cheers

Jase :)

If I type 3.14 in the 'You entered' field then press Tab the other fields get populated. If I then click on the +1 button the fields appear to change but I can't work out what's happening?

I didn't say to click on the +1. If you enter 3.14 and press the tab or enter key, the other fields get populated. If you modify the entered value to 3.141 and press the tab or enter key, the other fields get updated. At some point, the other fields stop changing values.

Hi PaulS

I think this is what you want me to try.

You entered: 3.1 Value actually stored in float: 3.099999904632568359375

You entered: 3.14 Value actually stored in float: 3.1400001049041748046875

You entered: 3.141 Value actually stored in float: 3.1410000324249267578125

You entered: 3.14159265 Value actually stored in float: 3.1415927410125732421875

You entered: 3.141592653 Value actually stored in float: 3.1415927410125732421875

In the last two attempts there is no change to the 'Value actually stored in float'. I'm guessing that this means there is no point in entering further numbers because they simply can't be stored?

Secondly, 'You entered' and 'Value actually stored in float' are a match up until here 3.141592. Is this where I stop?

Cheers

Jase :confused:

ilovetoflyfpv: Secondly, 'You entered' and 'Value actually stored in float' are a match up until here 3.141592. Is this where I stop?

Jase :confused:

you landed on the truth

https://www.arduino.cc/reference/en/language/variables/data-types/float/

remember . we are talking the most significant digits, so likewise:

123456.7f

is as much precision as

3.141592f

In the last two attempts there is no change to the 'Value actually stored in float'. I'm guessing that this means there is no point in entering further numbers because they simply can't be stored?

Exactly.

You entered: 3.14159265
Value actually stored in float: 3.1415927410125732421875

I have two queries:

1. According to binary32 format, there are 23 fractional digits after the decimal point. Why are there only 22 digits after the decimal point?

2. The entered value is 3.14159265 which is accurate up to 8 digit. In the print out (3.1415927410125732421875), the value has come out with 6/7 digit accuracy. Where and how have we lost the accuracy?

According to binary32 format, there are 23 fractional digits after the decimal point

Hint: it’s a binary point, not a decimal point

Additional hint: log223 = . . .?

Hint: it's a binary point, not a decimal point

Exactly! I note it and recite to avoid next time mistake!

Am I confused? I call it decimal point looking at the decimal point of the Real value definition as shown above. To me Real value is a decimal number along with a decimal point.

A decimal point goes between decimal digits. A binary point goes between binary digits.

The Real Value can be represented in decimal, binary, or any other base. For example, a Real Value could be 1.011*2^5 or 101100 (binary) or 44 (decimal).

Regardless of what base you represent the Real Value in, float32 has only 23 significant binary digits. The "decimal point" in a binary number (such as 1.011) is more accurately called a "binary point". You can also call it a "radix point" to use it with any base.

GolamMostafa: Am I confused? I call it decimal point looking at the decimal point of the Real value definition as shown above. To me Real value is a decimal number along with a decimal point.

This is my faded memory from studying this in college (30+ years ago). But, I’m sure it’s a binary point and all 23 bit of the mantissa are to the right of it. There is an assumed “1” to the left of it.

So, for the example shown, the full mantissa (in binary) is: 1.01000000000000000000000

The 8-bit exponent is 0b01111100 = 124. So, e = 124 - 127 = -3

So, take the mantissa and move the binary point thee places to the left and you get:

0.0010100000000000000000 (binary)

In decimal, that’s 5/32 = 0.15625

That means:

(0.0010100000000000000000)2

==> 0+02-1+02-2+ 12-3+02-4+12-5+ 02-6 + … + 0*2-23

==> 0 + 0 + 0 + 0.125 + 0 + 0.03125+ 0 + … +0 //every binary bit has fractional decimal contribution

==> (0.15625)10

So, the point could be called:

binary point
decimal point

Can we call it simply ‘point’?

GolamMostafa: So, the point could be called:

binary point

That would be correct.

decimal point

That would be incorrect.

That would be correct, but not as specific.

ilovetoflyfpv: In a nutshell when using floats where do I draw the line?

Before a comparison for equality.

Hi Gang

Thanks for all the replies. So the root of the problem is the 'mismatch' between binary and decimal fractions? So at some point your number will depart from what's being stored? This is why we only have 6-7 decimal digits for 32 bit single precision?

Really appreciate the help.

Cheers

Jase :)