Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: Find next lesser floating point number

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?

like image 368
FalafelPita Avatar asked Oct 12 '25 18:10

FalafelPita


1 Answers

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