In C and C ++, the following gives the closest 1.0 value:
#include <limits.h> double closest_to_1 = 1.0 - DBL_EPSILON/FLT_RADIX;
Note, however, that in later versions of C ++, limits.h deprecated in favor of climits . But then if you use C ++ code anyway you can use
#include <limits> typedef std::numeric_limits<double> lim_dbl; double closest_to_1 = 1.0 - lim_dbl::epsilon()/lim_dbl::radix;
And as Jarod42 writes in his answer, with C99 or C ++ 11 you can also use nextafter :
#include <math.h> double closest_to_1 = nextafter(1.0, 0.0);
Of course, in C ++ you can (and for later versions of C ++) include cmath and use std::nextafter .
celtschk Aug 6 '16 at 16:02 2016-08-06 16:02
source share