sin(2*pi) vs sind(360)
13 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(3 个)
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.
0 个评论
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 个评论
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
2015-7-13
Yes, it is due to pi not being represented precisely due to the fact that floating point representation is finite.
2 个评论
Torsten
2015-7-13
For a numerical computation, sin(pi)=1.2246e-16 should be exact enough.
Best wishes
Torsten.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Trigonometry 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!