I've seen the following table on Wikipedia:
However I couldn't find an explanation regarding what "bits precision" and "number of decimal digits" are and how they are related. What do they mean exactly? How to convert between them?
Thanks
I've seen the following table on Wikipedia:
However I couldn't find an explanation regarding what "bits precision" and "number of decimal digits" are and how they are related. What do they mean exactly? How to convert between them?
Thanks
Both of the two answers (JRE and Spehro Pefhany) are okay except IEEE floating point has an extra feature.
I hope it is clear that
2.3 x 10^2 = 23 x 10^1 = 230 x 10^0
It would create more work for the hardware if all three were valid (translated into base 2). For example two different bit patterns could be the same number.
So, IEEE mandates that the top bit of the mantissa is always 1. This is a normalised representation. Each number is only ever stored as one specific bit pattern.
Then, because the first bit is always 1, it is not stored. So the mantissa is actually 1 bit longer than stored because of this implied top 1 bit.
Take an example: 2.5E3 that would be 2.5 *10^3. The 2.5 is known as the mantissa, 3 is known as the exponent.
With a floating point number, you have some certain number of bits to represent both of these things together.
For single precision floating point you have 32 bits to represent the mantissa and the exponent. The 32 available bits are split into 24 for the mantissa and 8 for the exponent. The 24 bits for the mantissa represent a decimal number.
The whole floating point number will only have 24 bits that are significant (that are non-zero.) This number (24) is also the bits of precision given in the table. The exponent then says how many zeros to add to the end. The largest number you can represent with 24 bits is 16777215. You have 7 digits that can represent anything from 0 to 9, and one digit that can only represent from 0 to 1 - this is the decimal precision.
As mentioned by Spehro Pefhany, you can calculate the number of digits of precision from the bits of precision by multiplying the bits of precision by ln(2)/ln(10) - for 24 bits that gives you 7.225 digits of precision when the binary number is printed in decimal form.
The number of bits can be converted to decimal digits by multiplying it by ln(2)/ln(10) ~= 0.3.
This assumes that the sign is handled similarly in each case.
A number that can vary from 0 to 99.9 in increments of 0.1 has a precision of log2(100/0.1) ~=10 bits (binary digits) and a decimal precision of log10(100/0.1) = 3 decimal digits.
Similarly the precision in any given base n is logn(100/0.1). If n = 16 (hex) the precision is ~2.5 nibbles.
I used the example of 99.9 and 0.1 increment because the bits in a floating point number mantissa do not directly represent the magnitude (they have to be multiplied by two raised to the power of the exponent- or left/right shifted a number of times dependent on the exponent value.
If the mantissa is 24 bits, then it can represent 2^24 different values and it has a precision of log10(2^24) = 7.2 decimal digits. If you think of it as a fraction from 0 to 1 it could only move in increments of 1/2^24 or 0.00000005960. That's a little hard to visualize, perhaps, so I used the decimal example.
In the case of a typical pocket calculator, they actually do work in decimal internally, so you will get an 'even' number of digits precision. For example, an 8-digit calculator would have a precision of about 25.6 bits (inefficiently represented in 32 bits internally usually because calculators use binary coded decimal).
0.1
... Is it to say that a number that can vary from 0 to 9999 in increments of 0.1 has a decimal precision of 4 decimal digits? It actually makes sense if I think in terms of "there are four digits" in 9999. But why do we specifically use "increments of 0.1" here? Is it because "1000/0.1 = 10000" and log10(10000) = 4? Thanks!
\$\endgroup\$