Special values: NaN, Inf, and eps

1.5.4 Special values: NaN, Inf, and eps

MATLAB supports the IEEE representations of NaN (Not-a-Number) and Inf (Infinity). Certain mathematical operations can cause these to arise and they then behave in defined ways. For exam- ple 0/0, Inf*0, Inf±Inf, and Inf/Inf all result in NaN. Furthermore, any arithmetic operator involving a NaN is required to produce NaN as an output. This can cause unexpected results. For example, max([1 2 3 NaN 4 5 6]) results in NaN and the functions min, mean, std and many others will also return NaN on this vector. Even worse, running fft on data with only a single NaN in it will produce a NaN for the entire output spectrum. Despite these apparent complications, NaN’s have enough uses that they are well worth having around. The occurrence of a NaN in your output data signals you that an unexpected arithmetic exception has taken place in your code. This must be found and corrected. NaN’s can also be used as convenient place-holders to mark some special point in a matrix because they can easily be found. Also, when a NaN occurs in graphics, the action is to blank that part of the display. Thus NaN’s can be used to temporarily turn off parts of a graphic, something that is especially useful in 3D.

When used in logical comparisons, such as NaN==5, the result is almost always False. This means that an expression such as find(v==NaN) will not correctly identify any NaN’s in the vector v. (Of course, it works just fine if you want to find a number such as find(v==5).) So, NaN’s must be located using find(isnan(v)) that returns a vector of indices of any NaN’s in v. (The function isnan returns a logical vector of 1’s and 0’s indicating vector elements are NaN’s.)

Infinity is handled similarly to NaN’s in that certain operations generate infinity that subse- quently behaves in a defined way. For example x/0 generates an Inf and x/Inf always returns zero (unless x is Inf when NaN results). Inf also must be handled specially in logical comparisons. The functions isinf and isfinite can be used by themselves or as input to find as discussed above

28 CHAPTER 1. INTRODUCTION with isnan. Also, the debug command dbstop if naninf is helpful to stop execution at the point

that NaN or Inf occurs in any function. MATLAB uses double precision floating point arithmetic for its calculations. This is actually

a higher standard of precision than has traditionally been used in seismic data processing. Most processing systems have been written in Fortran or C and use single precision arithmetic. MATLAB supplies the internal constant eps whose value is the smallest positive floating point number available on your machine. In a loose sense, eps represents the machine “noise level” and is about 2.2204x10 −16 on the author’s computer. (How many decibels down from unity is eps?) As a general rule, tests for the equality of two floating point variables should be avoided. Instead of a test like if(x==y) consider something like if(abs(x-y)<10*eps). eps can also be useful in computing certain limits that involve a division by zero. For example, computing a sinc function with x=0:.1:pi;y=sin(x)./x; generates a zero divide and y(1) is NaN. However, x=0:.1:pi;y=sin(x+eps)./(x+eps); avoids the zero divide and results in y(1) of 1.