0

I'm actually researching how to display a float number (with write) and I'm facing about something which is confusing me.

I found that float are stored in 32 bits, whith 1 bits for sign, 7 bits for exponant and the rest for the Mantissa.

Where my trouble are coming, is when I display FLT_MAX with printf, I will get 340282346638528859811704183484516925440.000000 by simply doing printf("%f\n", FLT_MAX)

This value is bigger than INT_MAX, bigger than LLONG_MAX, how can this number of digit can be stored in 32 bits ? This is really 32 bits or system dependent ? I'm on Ubuntu x86_64 GNU/Linux.

I can't understand how more than 10 digits (INT_MAX len) can be stored in the same number of bits.

If think the problem is linked, but I also have trouble for double who will give me

printf("%lf", DBL_MAX);
#179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

It's making the mystery bigger ! Thanks for helping, hope I was clear.

rSim
  • 344
  • 4
  • 17
  • 2
    I recommend you do some research about [the IEEE754 floating point representation](https://en.wikipedia.org/wiki/IEEE_754). While the value is large, it's not very precise (over a certain limit, the precision of floating point values goes down significantly). – Some programmer dude Nov 13 '18 at 12:42
  • After I first reading, a lot of intersting stuff but still misunderstanding how 39 decimal digits can be stored in 32 (actually less for mantissa) bits. In wikipedia they spoke about floating value with more than 64 bits, which can be an exaplanation, but they still speak about 32 or 64 bits for single and double precision. In an array, they said 7.22 decimal digit for single precision, why do I have 39 ? – rSim Nov 13 '18 at 13:32
  • If you read more about [single precision](https://en.wikipedia.org/wiki/IEEE_754-1985#Single_precision) then you will see that the maximum value is ±(1−2⁻²⁴ ) × 2¹²⁸ ≈ ±3.40282×10³⁸. Which indeed turns out to be the big value you see. – Some programmer dude Nov 13 '18 at 13:45
  • 1
    Possible duplicate of [Why double can store bigger numbers than unsigned long long?](https://stackoverflow.com/questions/30052710/why-double-can-store-bigger-numbers-than-unsigned-long-long) – phuclv Nov 13 '18 at 13:51
  • 1
    [Why double.MaxValue is larger than long.MaxValue?](https://stackoverflow.com/q/2639941/995714), [Why do float and int have such different maximum values even though they're the same number of bits?](https://stackoverflow.com/q/11158476/995714), [Does double have a greater range than long?](https://stackoverflow.com/q/13022016/995714) – phuclv Nov 13 '18 at 13:55

1 Answers1

0

Bits are merely physical things with two states. There is no inherent meaning to them. When we use the bits to represent an integer in binary, we interpret each bit as having a value, 1 for one bit, 2 for another, 4 for another, 8 for another, and so on. There is nothing in physics, logic, or law that requires us to give them this interpretation.

When we use the bits to represent a floating-point object, we give each bit a different meaning. One bit represents the sign. Eight bits contain an encoding of the exponent. 23 bits contain an encoding of the significand.

To figure out the meaning of the bits given the floating-point encoding scheme for numbers in the normal range, we interpret the exponent bits as a binary numeral, then subtract 127, then raise two to the resulting power. (For example, “10000011” is the binary numeral for 131, so it represents 24) Then we take the significand bits and append them to “1.”, forming a binary numeral such as “1.01011100000000000000000”. We convert that numeral to a number (it is 159/128), and we multiply it by the power from the exponent (producing 159/8 in this example) and apply the sign.

Since the exponent can be large, the value represented can be very large. The software that converts floating-point numbers to characters for output such as “340282346638528859811704183484516925440.000000” performs these interpretations for you.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312