2
$\begingroup$

In MATLAB, there is a function named eps.

eps Spacing of floating point numbers. D = eps(X), is the positive distance from ABS(X) to the next larger in magnitude floating point number of the same precision as X. X may be either double precision or single precision. For all X, eps(X) is equal to eps(ABS(X)).

Is there an equivalent in Mathematica?

Edit:

eps, with no arguments, is the distance from 1.0 to the next larger double precision number, that is eps with no arguments returns 2^(-52).

eps('double') is the same as eps, or eps(1.0). eps('single') is the same as eps(single(1.0)), or single(2^-23).

Except for numbers whose absolute value is smaller than REALMIN,
if 2^E <= ABS(X) < 2^(E+1), then
eps(X) returns 2^(E-23) if ISA(X,'single')
eps(X) returns 2^(E-52) if ISA(X,'double')

For all X of class double such that ABS(X) <= REALMIN, eps(X)
returns 2^(-1074).   Similarly, for all X of class single such that
ABS(X) <= REALMIN('single'), eps(X) returns 2^(-149).

Replace expressions of the form
   if Y < eps * ABS(X)
with
   if Y < eps(X)

Example return values from calling eps with various inputs are
presented in the table below:

      Expression                   Return Value
     ===========================================
      eps(1/2)                     2^(-53)
      eps(1)                       2^(-52)
      eps(2)                       2^(-51)
      eps(realmax)                 2^971
      eps(0)                       2^(-1074)
      eps(realmin/2)               2^(-1074)
      eps(realmin/16)              2^(-1074)
      eps(Inf)                     NaN
      eps(NaN)                     NaN
     -------------------------------------------
      eps(single(1/2))             2^(-24)
      eps(single(1))               2^(-23)
      eps(single(2))               2^(-22)
      eps(realmax('single'))       2^104
      eps(single(0))               2^(-149)
      eps(realmin('single')/2)    2^(-149)
      eps(realmin('single')/16)   2^(-149)
      eps(single(Inf))             single(NaN)
      eps(single(NaN))             single(NaN)
$\endgroup$
4
  • $\begingroup$ In my PC, 'MachineEpsilon' return $2.22045 \times 10^{-16}$. $\endgroup$
    – diverger
    Commented Dec 19, 2014 at 8:44
  • $\begingroup$ Log[2,$MachineEpsilon]==-52 $\endgroup$
    – LLlAMnYP
    Commented Oct 9, 2015 at 8:48
  • $\begingroup$ In[425]:= eps[x_Real] := (1 + $MachineEpsilon)*x - x In[430]:= Log2[Map[eps, {.2, .5, 1., 1.6, 2., 5.5}]] Out[430]= {-54., -53., -52., -51., -51., -50.} $\endgroup$ Commented Aug 2, 2016 at 20:58
  • 1
    $\begingroup$ A closely related question. You should be able to take the difference of the original number and the result of nextafter to get something equivalent to eps. $\endgroup$ Commented Mar 28, 2017 at 16:12

2 Answers 2

2
$\begingroup$

I don't believe there is a directly equivalent function in Mathematica. However, as you comment, $MachineEpsilon (with a $ character) gives the value of eps.

$MachineEpsilon

2.22045*10^-16

If you look at the Examples > Applications section of the documentation linked you'll see that eps(x) could be defined in Mathematica as:

eps[x_:1] := x $MachineEpsilon

See also this answer to the somewhat related "Issues with $MachineEpsilon" question.

$\endgroup$
2
$\begingroup$

Ulp is a equivalent function in Mathemtica,you need Needs["ComputerArithmetic`"] first

Needs["ComputerArithmetic`"]
ComputerArithmetic`Ulp[1]

1.11022*10^-16

Of course,we can plot it

Plot[ComputerArithmetic`Ulp[x], {x, -10, 10}]

$\endgroup$
4
  • $\begingroup$ Seems to give half epsilon sometimes, but not always. $\endgroup$
    – Michael E2
    Commented Mar 28, 2017 at 16:24
  • $\begingroup$ @MichaelE2 Could you give a example about what you have encountered? $\endgroup$
    – yode
    Commented Mar 28, 2017 at 16:25
  • $\begingroup$ Your example above is epsilon over 2. $\endgroup$
    – Michael E2
    Commented Mar 28, 2017 at 16:26
  • $\begingroup$ E.g. all of these are wrong: Table[ComputerArithmetic`Ulp[2^n], {n, 0, 10}]; but all of these are right: Table[ComputerArithmetic`Ulp[1.1*2^n], {n, 0, 10}]. $\endgroup$
    – Michael E2
    Commented Mar 28, 2017 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.