本文介绍了为什么sin(45)和cos(45)给出不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我没想到的.我知道这些数字不是100%精确的,但我没想到互补角会给出sincos的不同结果:

This is something I wasn't expecting. I know these numbers are not 100% exact, but I wasn't expecting complementary angles giving different results of sin and cos:

此以下函数返回0.70710678118654746000000...

sin(45 * PI / 180.0);

,而以下函数返回0.70710678118654757000000...

cos(45 * PI / 180.0);

所以,是:

0.707106781186547**46**000000... vs
0.707106781186547**57**000000...


不仅这些... sin(1 * PI / 180.0)还返回与cos(89 * PI / 180.0)稍有不同的数字,尽管它们应该相同.


and not only those... sin(1 * PI / 180.0) also returns a slightly different number than cos(89 * PI / 180.0) although they should be the same.

此外,这不仅是sin vs cos的问题,还是sin vs sin的问题:sin(1 * PI / 180.0)返回的值与sin(179 * PI / 180.0)不同,同样,它们应该相同.

Moreover it's not only a sin vs cos problem, it's also a sin vs sin problem: sin(1 * PI / 180.0) returns a different value than sin(179 * PI / 180.0), again they should be the same.

我尝试使用弧度而不是度,并且存在完全相同的区别,我尝试使用一个小的PI值,一个巨大的PI值(大约100个小数位和更多),它们仍然不同,使用cmath代替math.h,我尝试使用M_PI代替我自己定义的PI.

I tried to use radians and not degrees, and there is exactly the same difference, I tried to use a small PI value, a huge PI value (around 100 decimals and more) and they're still different, I tried to use cmath instead of math.h, I tried to use M_PI instead of a PI defined by myself.

差异始终是相同的,大约是16位小数.不要误会我的意思,我知道我永远也不会获得这些数字的100%精确值,但是我至少希望得到同样的不精确"信息.角sincos的值.这到底是怎么了?

The difference is always the same, around the 16th decimal. Don't get me wrong, I know I will never get a 100% precise value of these numbers, but AT LEAST I was expecting to get the same "imprecise" value of sin and cos of complementary angles. What the hell is wrong with all this?

我需要使它们相同,因为我正在处理的程序(被要求做的重力模拟器)使用的对象具有double(我也尝试过float)变量,这些变量基本上是角度(度)或弧度,我都尝试过).这些是对象用来移动的方向,我还需要角度来计算对象之间的相互作用.

I need them to be the same because the program I'm working on (a gravity simulator I was asked to do) uses objects that have double (I also tried float) variables which are basically angles (degrees or radians, I've tried both). Those are the directions the objects use to move, also I need the angles to calculate the interactions between the objects.

角度在程序的每次迭代中都会发生变化,并且在每次迭代中,角度都会根据前一次迭代的角度进行计算,因此,如果在任何一点上存在最小错误的角度值,则该误差会进一步放大,并且每次迭代中都有更多内容.

The angles change in every iteration of the program, and in every iteration, the angles change basing on the calculations on the previous iteration's angles, so if there is any minimally wrong angle value at any point, that error gets amplified more and more in every iteration.

该程序运行数千甚至数百万次迭代,因此值的错误变得异常巨大!明确地说,行星最终失去了平衡,一切都变成了灾难,我真的很生气:(

The program runs thousands and even millions of iterations, so the value's error gets absurdly huge! To put it clear, planets eventually get out of their balance and everything becomes a disaster, I'm really mad :(

P.s.我在Windows 7(32位)上.

P.s. I'm on Windows 7, 32 bits.

推荐答案

为什么?的计算方式不同,因此会发生(并累积)不同的浮点错误.您所看到的不是错误; FP算术是数学定律无法预测的.

Why? The are calculated differently, so different floating point errors will occur (and accumulate). What you see is not an error; FP arithmetic isn't predictable by math laws.

顺便说一句,例如提供30位或100位PI不会有什么不同
如果您的文字开头不能包含30位数字.

Btw., providing eg. 30 or 100 digits of PI won't be anything different
if your type can't hold 30 digits in the first place.

这篇关于为什么sin(45)和cos(45)给出不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 22:11