MATLAB: Sinc(pi) doesn’t give zero

pisinc

When I compute sinc(pi) I get a small non-zero number, but I know that sin(pi)/pi should equal zero. How do I get sinc(pi) to equal zero in Matlab? For that matter, even sin(pi) doesn't return a value of zero (and it should) so I can't replace sinc(x) with sin(x)/x to get the right answer
>> sinc(pi)
ans =
-0.0436
>> sin(pi)
ans =
1.2246e-16

Best Answer

Since pi is an irrational number and cannot be represented exactly by a finite number of binary digits, there is always going to be an infintesimal error assocated with doing math with irrational numbers (really, there is always an error doing arithmetic on a computer). If you really want it to be zero you can compare it to a tolerance and say that any number smaller than your tolerance is set to zero. For example:
tol = 1e-15;
z = sin(pi);
if abs(z) < tol
z = 0;
end
Alternatively, you could use
round
round(sin(pi),15)
will return 0