sin(2*pi) vs sind(360)

조회 수: 13 (최근 30일)
david dang
david dang 2015년 7월 13일
답변: Mike Croucher 2022년 10월 21일
Could someone please explain to me why sin(2*pi) gives me non-zero number and sind(360)? Does this have to do with the floating points of pi?

답변 (3개)

Mike Croucher
Mike Croucher 2022년 10월 21일
If you ever need to compute sin(x*pi) or cos(x*pi), its better to do sinpi(x) or cospi(x). You never explicitly multily x by a floating point approximation ot pi so you always get the results you expect.

Stephen23
Stephen23 2015년 7월 13일
편집: Stephen23 2015년 7월 13일
Yes, it is because π is a value that cannot be represented precisely using a finite binary floating point number. This is also shown in the sind documentation:
"Sine of 180 degrees compared to sine of π radians"
sind(180)
ans =
0
sin(pi)
ans =
1.2246e-16
  댓글 수: 2
david dang
david dang 2015년 7월 13일
Thanks for the answer. I tried setting pi = sym(pi), but this increased my computation time significantly. Is there any way to perform my computations in radians, without increasing computation time significantly, and obtain the exact solution?
Stephen23
Stephen23 2015년 7월 13일
편집: Stephen23 2015년 7월 13일
No.
Unless of course you buy a computer with infinite memory to hold an infinite representation of π and yet can somehow perform operations at the same speed as your current computer.
π is an irrational number. How do you imagine representing an irrational number with a finite floating point value and not getting rounding error? All numeric computations with floating point numbers include rounding errors, and it is your job to figure out how to take this into account. To understand floating point numbers you should read these:

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2015년 7월 13일
Yes, it is due to pi not being represented precisely due to the fact that floating point representation is finite.
  댓글 수: 2
david dang
david dang 2015년 7월 13일
Thanks for the answer. I tried setting pi = sym(pi), but this increased my computation time significantly. Is there any way to perform my computations in radians, without increasing computation time significantly, and obtain the exact solution?
Torsten
Torsten 2015년 7월 13일
For a numerical computation, sin(pi)=1.2246e-16 should be exact enough.
Best wishes
Torsten.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Trigonometry에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by