## Loren on the Art of MATLABTurn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

# Precision and realmax

I was on a recent email thread at the office regarding realmax, eps, and overflow.

### What is realmax?

From the help for realmax we learn that

h = help('realmax');
disp(h(1:48))
 REALMAX Largest positive floating point number.


Here's the double precision value for realmax shown with about 15 digits.

format long
rmax = realmax
rmax =

1.797693134862316e+308



### Overflow with Respect to realmax

Let's see what it takes to add to realmax to get to the next number.

epsrmax = eps(rmax)
big = rmax+epsrmax
epsrmax =

1.995840309534720e+292

big =

Inf



Are you surprised that the next largest number is Inf?

### How about Less than eps?

What if we try a number a tenth of eps?

biggish = rmax + epsrmax/10
isequal(biggish, rmax)
biggish =

1.797693134862316e+308

ans =

1



It's not far enough away on the floating point scale to produce a value different from rmax. In fact, the help for eps has a list of values for eps at some interesting double and single values.

help 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)).

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)

See also REALMAX, REALMIN.

Overloaded methods:
codistributed/eps
quantizer/eps
qfft/eps

Reference page in Help browser
doc eps



### References

Here are some references to help you learn more about floating point issues.

### Do Floating Point Issues Haunt You?

Do you recognize when issues you have are from floating point considerations? Do you have any war stories or best practices to share with others? Post them here.

Published with MATLAB® 7.8

### コメント

コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。