1. ## float representation

Hello everyone,

In my knowledge, float could only be represented by 7 decimal bits. But why in debugger, I can see more than 7 decimal bits?

(in my sample, Result2 is of 9 bits, other than 7, why?)

Code:
float TotalBonus = 199.321F;
float Worker1 = 100F;
float Worker2 = 300F;

// 		Result1	49.83025	float
float Result1 = TotalBonus * Worker1 / (Worker1 + Worker2);

// 		Result2	149.490753	float
float Result2 = TotalBonus * Worker2 / (Worker1 + Worker2);

George

2. The float format is architecture dependant, but on x86, it is a 23(+1)-bit mantissa, which means that you get approximately 24/log2(10) -> 24/3.3 -> 7.2 digits. This means that you have AROUND 7 digits, but the precise number of digits depends on the value in the float itself.

--
Mats

3. Hi Mats,

1.

I do not think 7.2 digits is near 9 digits. :-)

Any ideas? Maybe debugger has a special representation for float number?

2.

I think the calculation formular for how many digits in base 10 should be log(10 base) (2 ^ 24 - 1), approximately to log(10 base) (2 ^ 24 ), could you explain your calculation formular please?

Originally Posted by matsp
The float format is architecture dependant, but on x86, it is a 23(+1)-bit mantissa, which means that you get approximately 24/log2(10) -> 24/3.3 -> 7.2 digits. This means that you have AROUND 7 digits, but the precise number of digits depends on the value in the float itself.

--
Mats

regards,
George

4. Originally Posted by George2
Hi Mats,

1.

I do not think 7.2 digits is near 9 digits. :-)
No, but it's only 1 digit more - in the sense that the 0.2 is either present or not present, which makes the nominal number either 7 or 8 digits. But that's on average. Since a binary number is a precise representation of a decimal number, you also get artifacts, so I suspect that you may have a "endless number", e.g 0.1, that would come out as 0.0999999999[keep going with nines until the end of the universe (time or space, whichever you reach first)] if there is no rounding involved. Other numbers may also "leave some leftover" after it's been translated to decimal.

Any ideas? Maybe debugger has a special representation for float number?
It probably uses something like "fit as much as possible in ten places with either %f or %e", but I'm not sure.
2.

I think the calculation formular for how many digits in base 10 should be log(10 base) (2 ^ 24 - 1), approximately to log(10 base) (2 ^ 24 ), could you explain your calculation formular please?
To form one decimal digit, we need log2(10) bits. Since we have 24 bits, we can produce 24/3.3 digits from that. Of course, a digit is either present or not present. Similarly, a 32-bit binary number will take up 32/log2(16) digits in hex -> so 8 digits, or 32/log2(8) in octal. All these numbers would require rounding up to the nearest integer. Floating point numbers may or may not fill all the positions in the number.

--
Mats