简体   繁体   中英

C, FLT_MAX value larger than 32 bits?

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.

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 2 4 ) 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM