How to get full float number expression?

Hello, first post here from me!

I am a beginner in real programming, had some visual scripting experience as a hobby. I have a general question about floating point representation.

I tested some things and found that a maximum number of digits in a fractional part of a float number is 9 when dealing with long fractional numbers, meaning there is a cutoff from 9th digit after the floating point, so the rounding error is significant.

If, for example, I use reciprocal function 1/x, precision is lost: 1/119 returns 0.008403362, but 1/0.008403362 returns 118.99999. In 32 bit standard, there should be more digits, as stated here:

“The smallest normal value of a float, known as FLT_MIN, is 2-126. A significand of 24 1s starting at the place of the smallest normal power of two exponent (-126) is this 24 significant bit number:

1.11111111111111111111111 x 2-126

Written out longhand in binary it is 149 bits, 125 leading zeros followed by 24 1s. In decimal it is

0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875

It has 149 digits, 37 leading zeros followed by 112 significant digits."

source: https://www.exploringbinary.com/maximum-number-of-decimal-digits-in-binary-floating-point-numbers/

My questions are: How to get full expression of a float number as described above and use it in functions? Is there a limitation or restriction on long float numbers in processing? And, if there are restrictions, is it possible to work around them?

I searched for answers in processing forums and documentation without much results. So, I would very much appreciate if someone has more insight into this matter.

Thank you in advance.

Hello,

My brief exploration into this…

import java.math.*;

BigDecimal a = new BigDecimal("0.00000000000000000000000000000000000002350988561514728583455765982071533026645717985517980855365926236850006129930346077117064851336181163787841796875");
//System.out.println(a);

BigDecimal b = a;
BigDecimal c = a.add(b);

System.out.println(a);
System.out.println(b);
System.out.println(c);

System.out.println(c.floatValue());
System.out.println(c.doubleValue());

References:

https://www.simplexacode.ch/en/blog/2018/07/using-bigdecimal-as-an-accurate-replacement-for-floating-point-numbers/

:)

1 Like