CroCo
CroCo

Reputation: 5741

getting very small number instead of zero

I'm doing some calculations and I ended up with this problem which is not getting zero. In my equation, the theoretical part suggests the result to be zero however Matlab throws very small non sensible number z = -4.4409e-16. z here should be zero. In here, the problem has been discussed why this occurs. I've tried to pass this value to cos() to get one as a result and indeed I got one. My question is now does Matlab treat this value as a zero or it is treated as is? Another question is to which level should I assume this value to be zero so that I can reassign this value to be zero?

Upvotes: 1

Views: 2857

Answers (1)

chappjc
chappjc

Reputation: 30589

It's not zero, it is the value you see. While there is no hard-and-fast rule for deciding what you can treat as zero (if anything other than zero), there is a function that will return a useful number for a given data type that help you decide what a significant magnitude is, eps:

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.

Or you can call it with no input variable:

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

For example,

>> eps
ans =
   2.2204e-16
>> eps('double')
ans =
   2.2204e-16
>> eps('single')    
ans =
  1.1921e-07

Because there are often multiple floating point operations computed in sequence, the values you get are likely to be higher (and are in your case). If you aren't terribly concerned with precision, you can probably consider a multiple of eps to be zero.

Upvotes: 4

Related Questions