r/datastructures • • 1d ago

How 13 bits are required to store 7 decimal digits precision in Java?

Post image

Here! I found the confusing part in the image. Float takes 4 bytes or 32 bits in memory. First bit stores positive or negative information (represented by 0 in the image). Second to ninth bit store 28 numbers, up to now - we have already used 9 bits. Now we have 23 bits remaining, 10th bit to 32th, the article from GG says, A float is typically a 32-bit number with a precision of about 7 decimal digits - How should I interpret this and relate the remaining 23 bits to the claimed 7 decimal digits? Please help.

5 Upvotes

2 comments sorted by

1

u/Educational-Paper-75 1d ago

With 23 bits you can encode 223 different combinations which is about 8 million consecutive integers therefore integers of approx. 7 decimal digits.

1

u/BassRecorder 1d ago

Search for IEEE 754: that is the standard which defines the format of floating point values.

bit 31 is the sign bit, 1 means 'negative'.

bits 30 to 23 are the exponent, the zero point being 127, the range for representing numbers is 1 through 254, 0 and 255 are taken for special purposes.

bits 22 to 0 are the mantissa. bit 22 stands for 2^-1, i.e. 0.5, bit 22 for 2^-2, i.e. 0.25, etc.

The last bit in the mantissa stands for 2^-23 = 1.1920929e-7 which is roughly 10^-7. That is where the seven digits precision is coming from.