本文介绍了为什么 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 :(

附言我使用的是 32 位 Windows 7.

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.

顺便说一句,提供例如.PI 的 30 或 100 位数字不会有什么不同
如果您的类型一开始就不能容纳 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