1

Since the maximum value of a float variable is 3.4028235E+38, shouldn't it represented with 128 bits instead of 32? (2^128=3.4028235E+38).

4 Answers 4

4

No, it is not integer, so you cannot calculate it that way. It have floating point (not in fixed place).

It is stored as

1.significand x 2 ^ exponent.

It takes one bit for sign (+/-), 23 for significand (0 is not stored it is implicit) and 8 more for exponent.

Because of this, you have fixed precision (number of significant digits) for very large numbers and also for numbers which are close to zero.

You have it all explained here: https://en.wikipedia.org/wiki/Single-precision_floating-point_format

3
  • That is a completely different approach. Indeed the wikipedia article is very useful. Thank you. Commented Jul 12, 2016 at 22:18
  • The implicit bit is actually a 1. Commented Jul 13, 2016 at 9:01
  • @Edgar thanks, it was long time ago when I learned this :) Fixed.
    – Darko
    Commented Jul 13, 2016 at 18:10
3

No, because IEEE 754 floats use a decimative compressed format. 64-bit floats allocate 1 bit to the sign, 11 bits to the exponent, and 52 bits to the mantissa, 32-bit floats allocate 1, 8, and 23, and so on.

You convert from it by adding an offset to the exponent, multiplying the mantissa by the exponent to the power of 2, and then applying the sign.

3

Just adding a little piece of information to the previous answers: it's worth noting that a float can represent exactly all integers from −224 to +224 (i.e. ±16777216).

The next integer, namely 16777217, is not representable as a float. A calculation giving this value would suffer a rounding error.

1

Another way of looking at it is that a float variable is actually a list of 4294967296 numbers. That's far less than the possible range of numbers and fractions that a float can represent, so only a subset of the numbers are actually represented. Any number you try and use that's not in that list of numbers is rounded to the nearest one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.