Maximum whole number a float can represent?

what is the maximum whole number value a float can be used to represent? (32bit)
For you it would have 4 bytes, which is the same as a long int.

To find out the ints max, you can initialise an int with its MAX value by:
int x = INT_MAX;

same with minimum.
long x = LONG_MAX;
cout << x;

I do not know the number the float can hold though, sorry. I just though i would give you this information, you might already know it though.
Depends on the number of bits used for the exponent.

http://en.wikipedia.org/wiki/Single-precision_floating-point_format
This is what the numeric_limits class is designed for.

See http://cplusplus.com/reference/std/limits/numeric_limits/
and http://cplusplus.com/reference/clibrary/cfloat/

I get really weird values for max() -- see digits10 and the like to calculate something more reasonable.

Hope this helps.
> what is the maximum whole number value a float can be used to represent? (32bit)

Do not use float to represent whole numbers.

On my implementation, (where the representation of float has 32 total bits with the mantissa having 24 bits),
a float can represent the whole numbers 33554430 ( ( 2^24 - 1 ) * 2 ) and 134217720 ( ( 2^24 - 1 ) * 2^3 ),
but can't represent 33554429, 33554431, 134217719 or 134217721.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <limits>
#include <iostream>

int main()
{
    std::cout << std::numeric_limits<float>::digits << "\n\n" ; // 24
    std::cout << std::fixed ;

    for( int i = 33554425 ; i < 33554435 ; ++i )
    {
        volatile float f = i ;
        std::cout << i << " == " << f << '\n' ;
    }

    std::cout << '\n' ;

    for( int i = 134217715 ; i < 134217725 ; ++i )
    {
        volatile float f = i ;
        std::cout << i << " == " << f << '\n' ;
    }
}

Output:
24

33554425 == 33554424.000000
33554426 == 33554426.000000
33554427 == 33554428.000000
33554428 == 33554428.000000
33554429 == 33554428.000000
33554430 == 33554430.000000
33554431 == 33554432.000000
33554432 == 33554432.000000
33554433 == 33554432.000000
33554434 == 33554432.000000

134217715 == 134217712.000000
134217716 == 134217712.000000
134217717 == 134217720.000000
134217718 == 134217720.000000
134217719 == 134217720.000000
134217720 == 134217720.000000
134217721 == 134217720.000000
134217722 == 134217720.000000
134217723 == 134217720.000000
134217724 == 134217728.000000
@JLBorges:
I'm a little confused by the math you did.
The Mantissa is the significant bits, not the exponent.
If the exponent is 8 bits, the maximum range would be from 2^-126 - 2^127 (Times the value of 1.mantissa), with 00 given the value of 0, and FF the value infinity. If I understand floats correctly?

EDIT: And if you need to represent very large numbers (At a slight cost of precision), floats are actually very good. An unsigned integer can only represent up to 2^32-1.
Last edited on
The "slight cost of precision" is significant.

Think of it in terms of numbers this way: suppose the mantissa represents exactly ten decimal digits, and the exponent two decimal digits. So:

1.234567890 x 1009 == 1234567890

However:

1.234567890 x 1010 == 12345678900

The very next number has a gap of 1010-9 == 101:

1.234567891 x 1010 == 12345678910

As you already know, floats are stored in a way more convenient to binary arithmetic -- but the same principle holds. Unless the exponent exactly matches the number of digits in the mantissa's maximum value, you have gaps.
Topic archived. No new replies allowed.