Why are sine and cosine values inaccurate?

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

 Akzeptierte Antwort

Honglei Chen
Honglei Chen am 19 Mär. 2013

1 Stimme

Those are floating point errors. If you want exact zeros, you can use sind/cosd, although they are slower
sind(180)
cosd(90)

5 Kommentare

Jem
Jem am 19 Mär. 2013
Many thanks Honglei, that's great.
Gary Groves
Gary Groves am 8 Feb. 2021
Hi, so where do I find information on the "d" operator that you added to the end of sin, and cos? I have been looking everywhere and the only place that I am seeing it used is here in your example. Do you have a link to a help page that explains the use of the "d" function that you used here?
John D'Errico
John D'Errico am 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.
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)
radian = 1.2246e-16
degree = sind(180)
degree = 0
multipleOfPi = sinpi(1)
multipleOfPi = 0
symbolic = sin(sym(pi))
symbolic = 
0

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Shashank Prasanna
Shashank Prasanna am 19 Mär. 2013

1 Stimme

pi itself is an approximation. I would point you to this solutions page for some more insight to this behavior:

Kategorien

Mehr zu Get Started with Signal Processing Toolbox finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Gefragt:

Jem
am 19 Mär. 2013

Kommentiert:

am 8 Feb. 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by