In C (and many other programming languages), there is a function
double nextafter(double x, double y)
which takes two (IEEE 754) floating-point numbers and returns the next representable floating-point number after x
in the direction of y
. What is the Mathematica equivalent of this function for MachinePrecision
numbers?
EDIT: This issue has been brought up in the comments, but for the benefit of future readers, note that this is a substantially more subtle task than simply adding or subtracting $MachineEpsilon
. The problem is that the distance between one floating-point value and the next changes with magnitude. $MachineEpsilon
, by definition, is the smallest positive floating-point value such that 1.0 + $MachineEpsilon > 1.0
. The distance between 1.0e-300
and the next number up will be much smaller, while the distance between 1.0e+300
and the next number up will be much greater. In addition, there are issues raised by the transitions between one order of magnitude and the next. Observe that 1.0 + $MachineEpsilon/2 == 1.0
, while 1.0 - $MachineEpsilon/2 < 1.0
.