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


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

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

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.

| improve this answer | |

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.

| improve this answer | |

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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