## 为什么在MATLAB中sin(Pi)不是精确的，而sin(pi/2)是精确的？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (565)

`sin(pi)`- >并不完全是因为`pi`。但 `sin(pi/2)`完全相等1

### 2 个回答

``````sin x = x - (x^3)/3! + (x^5)/5! - (x^7)/7! + (x^9)/9! ...
``````

``````clc
x = pi;     %  or x = pi/2
res = x;
factor = -1;
for ii=3:2:19
res = res + factor*power(x,ii)/factorial(ii);
factor = factor*-1;
fprintf ( 'iteration %2i  sin(x)=%1.16f\n', (ii-1)/2, res );
end
res
``````

``````Results for PI/2
iteration  1  sin(x)=0.9248322292886504
iteration  2  sin(x)=1.0045248555348174
iteration  3  sin(x)=0.9998431013994987
iteration  4  sin(x)=1.0000035425842861
iteration  5  sin(x)=0.9999999437410510
iteration  6  sin(x)=1.0000000006627803
iteration  7  sin(x)=0.9999999999939768
iteration  8  sin(x)=1.0000000000000437
iteration  9  sin(x)=1.0000000000000000
Final Result: 1.0000000000000000

Results for PI
iteration  1  sin(x)=-2.0261201264601763
iteration  2  sin(x)=0.5240439134171688
iteration  3  sin(x)=-0.0752206159036231
iteration  4  sin(x)=0.0069252707075051
iteration  5  sin(x)=-0.0004451602382092
iteration  6  sin(x)=0.0000211425675584
iteration  7  sin(x)=-0.0000007727858894
iteration  8  sin(x)=0.0000000224195107
iteration  9  sin(x)=-0.0000000005289183
Final Result: -0.0000000005289183
``````

``````sin(pi+dx)=sin(pi)+cos(pi)dx+o(dx^2)=-dx+o(dx^2)
sin(pi/2+dx)=sin(pi/2)+cos(pi/2)dx+o(dx^2)=1+o(dx^2)
``````