The Matlab function eps(x)
returns "the positive distance from abs(x) to the next larger floating-point number of the same precision as x
." I use this to calculate the smallest floating-point number greater than x
, via x + eps(x)
. I would also like to obtain the largest floating point number less than x
, but I'm unaware of a function similar to eps
that would facilitate this. How can I find the largest floating point number less than x
?
You can subtract eps
in almost all cases.
However as you probably have realized, this does not apply when the mantisa changes, or in other words, when you want to subtract from a power of two.
A negative-side eps
then is easy to implement, knowing that the current eps
is smaller than the distance to the next power of two that will trigger a step change. Therefore, the eps
of our number minus its eps
should do the trick.
function out=neps(in)
out=eps(in-eps(in));
This seem to work fine
eps(2)
4.440892098500626e-16
neps(2)
2.220446049250313e-16
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With