Output std::numeric_limits<double>::epsilon()
instead. std::numeric_limits
is declared in the standard header <limits>
.
A more usual technique, if you really must calculate it (rather than trusting your standard library to provide a correct value) is
double epsilon = 1.0;
while ((1.0 + 0.5 * epsilon) != 1.0)
epsilon *= 0.5;
or to do the calculation.
Note that (although you haven't shown how you did it) it may actually be your long double
calculation that is incorrect, since literal floating point values (like 1.0
) default to being of type double
, not long double
- which might suggest the error is in your calculation of the long double
result, not the double
one.. If you want the result to be of type long double
, it would be advisable to give all of that literal values (1.0
, 0.5
) the L
suffix, to force them to be of type long double
.
Also remember to use appropriate formatting when streaming the resultant value to std::cout
, to ensure output also has the accuracy/precision you need. The default settings (what you get if you don't control the formatting) may differ.