First of all, you need to ensure that you actually print sufficiently many digits to ensure all representable values of double
are displayed. You can do this as follows (make sure you #include <iomanip>
for this):
std::cout << std::scientific << std::setprecision(std::numeric_limits<double>::max_digits10) << getMax(a) << std::endl;
Secondly, numeric_limits<>::min
is not appropriate for this. If your starting value is 1.0
, you can use numeric_limits<double>::epsilon
, which is the smallest difference from 1.0
that is representable.
However, in your code example, the starting value is 32
. Epsilon does not necessarily work for that. Calculating the right epsilon in this case is difficult.
However, if you can use C++11(*), there is a function in the cmath
header that does what you need std::nextafter
:
#include <iostream>
#include <limits>
#include <iomanip>
#include <cmath>
double getMax(double a)
{
return std::nextafter(a,std::numeric_limits<double>::lowest());
}
int main()
{
double a = 32;
std::cout << std::scientific
<< std::setprecision(std::numeric_limits<double>::max_digits10)
<< getMax(a)
<< std::endl;
return 0;
}
I've also put it on liveworkspace.
To explain:
double nextafter(double from, double to);
returns the next representable value of from in the direction of to. So I specified std::numeric_limits<double>::lowest()
in my call to ensure you get the next representable value less than the argument.
(*)See Tony D's comment below. You may have access to nextafter()
without C++11.