some problem with the min exponent of float and double in <limits> header?

W

Wayne Shu

Hei everyone:

Just see the output of the following program
#include <iostream>
#include <cstdlib>
#include <limits>

int main()
{
std::cout << "minimum exponent of double: " <<
std::numeric_limits<double>::min_exponent
<< "\nmaximum exponent of double: " <<
std::numeric_limits<double>::max_exponent
<< "\nminimum exponent of float: " <<
std::numeric_limits<float>::min_exponent
<< "\nmaximum exponent of float: " <<
std::numeric_limits<float>::max_exponent
<< std::endl;

system("pause");
return 0;
}

I use the vc8 and gcc 3.4 compile it, and the output is the same:
minimum exponent of double: -1021
maximum exponent of double: 1024
minimum exponent of float: -125
maximum exponent of float: 128

but in the IEEE-754(IEEE Standard for Binary Floating-Point
Arithmetic)
the minimum and maximum exponent of double is -1022 and +1023
the minimum and maximum exponent of float is -126 and +127

Why they are not the same?
min_exponent and max_exponent in the <limits> header are not the same
mean in the IEEE standard?

someone explain it for me??
thanks.
 
J

John Carson

Wayne Shu said:
Hei everyone:

Just see the output of the following program
#include <iostream>
#include <cstdlib>
#include <limits>

int main()
{
std::cout << "minimum exponent of double: " <<
std::numeric_limits<double>::min_exponent
<< "\nmaximum exponent of double: " <<
std::numeric_limits<double>::max_exponent
<< "\nminimum exponent of float: " <<
std::numeric_limits<float>::min_exponent
<< "\nmaximum exponent of float: " <<
std::numeric_limits<float>::max_exponent
<< std::endl;

system("pause");
return 0;
}

I use the vc8 and gcc 3.4 compile it, and the output is the same:
minimum exponent of double: -1021
maximum exponent of double: 1024
minimum exponent of float: -125
maximum exponent of float: 128

but in the IEEE-754(IEEE Standard for Binary Floating-Point
Arithmetic)
the minimum and maximum exponent of double is -1022 and +1023
the minimum and maximum exponent of float is -126 and +127

Why they are not the same?
min_exponent and max_exponent in the <limits> header are not the same
mean in the IEEE standard?

someone explain it for me??


Subtract one from each of the C++ numbers you are getting and you get the
IEEE values.

This adjustment by 1 is specifed in the C++ standard. I don't know why. See
section 18.2.1.1 of the standard, especially:


static const int min_exponent;
23 Minimum negative integer such that radix raised to the power of one less
than that integer is a normalized floating point number.

26 Meaningful for all floating point types.
static const int max_exponent;
27 Maximum positive integer such that radix raised to the power one less
than that integer is a representable finite floating point number.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top