Why are sine and cosine values inaccurate?
99 views (last 30 days)
Show older comments
Hi, I'm a medical researcher interested in DSP, and am trying to learn how to use MATLAB. I am just testing out basic functions, including sine and cosine functions. After inputting cos(pi/2) I got 6.1232e-17, and for sin(pi) I got 1.2246e-16. Clearly these are very close to the value 0 which I expected. But I am confused as to why it did not return zero. Is this round-off error?
thanks
Jem
0 Comments
Accepted Answer
Honglei Chen
on 19 Mar 2013
Those are floating point errors. If you want exact zeros, you can use sind/cosd, although they are slower
sind(180)
cosd(90)
5 Comments
John D'Errico
on 8 Feb 2021
d is not an "operator". sind and cosd are simply other functions that have been defined in MATLAB for the use of people who prefer to work/think in terms of degrees and not radians. Other functions, such as tand, atand, etc., are also defined to work in the same way.
Steven Lord
on 8 Feb 2021
In addition, if you are computing the sine and cosine of multiples of π (the pi function returns a value very close to, but not exactly equal to, the value of the irrational number π) you could use the sinpi and cospi functions or you could perform the calculations symbolically using Symbolic Math Toolbox.
radian = sin(pi)
degree = sind(180)
multipleOfPi = sinpi(1)
symbolic = sin(sym(pi))
More Answers (1)
Shashank Prasanna
on 19 Mar 2013
pi itself is an approximation. I would point you to this solutions page for some more insight to this behavior:
See Also
Categories
Find more on Trigonometry in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!