I am trying to implement the matlab eps(x) function in C ++
For example, in Matlab:
>> eps(587.3888) ans = 1.1369e-13 >> eps(single(587.3888)) ans = 6.1035e-05
However, when I try to do this in C ++, I cannot get the correct single-precision answer.
#include <limits> #include <iostream> #include <math.h> #define DEBUG(x) do { std::cerr << x << std::endl; } while (0) #define DEBUG2(x) do { std::cerr << #x << ": " << x << std::endl; } while (0) int main() { float epsf = std::numeric_limits<float>::epsilon(); DEBUG2(epsf); double epsd = std::numeric_limits<double>::epsilon(); DEBUG2(epsd); float espxf = nextafter(float(587.3888), epsf) - float(587.3888); double espxd = nextafter(double(587.3888), epsd) - double(587.3888); DEBUG2(espxf); DEBUG2(espxd); }
Running the program I get the following output:
$ ./a.out epsf: 1.19209e-07 epsd: 2.22045e-16 espxf: -1.13687e-13 espxd: -1.13687e-13
It seems that for some reason, although the eps values ββfor single and double precision are correct, exiting using the nextafter function nextafter outputs the double precision value. My value for epsxf should be 6.1035e-05, as in Matlab.
Any thoughts?
source share