三角函数值与实际不符

发布于 2024-12-11 07:57:10 字数 384 浏览 0 评论 0原文

我使用 toRadians() 来转换值并找到三角函数,例如

dataMain = Math.cos(Math.toRadians(dataSub);

但我遇到了诸如 之类的问题dataSub = 60

答案应该是0.5 但我的程序中的答案是 0.50000000000001

甚至是 dataSub = 30

dataMain = Math.sin(Math.toRadians(dataSub);

在我的程序中答案是 0.49999999999994

我该如何解决这个问题?

I use toRadians() to convert value and find trigonometry such this

dataMain = Math.cos(Math.toRadians(dataSub);

but i have a problem such as dataSub = 60

Answer should be 0.5
but answer in my program is 0.50000000000001

Or even dataSub = 30

dataMain = Math.sin(Math.toRadians(dataSub);

answer in my program is 0.49999999999994

How can i fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

洒一地阳光 2024-12-18 07:57:10

将角度从度数转换为弧度时,结果是一个通常不能精确表示为浮点数的数字。您只有大约 16 位数字可供使用。

因此,当您计算 toRadians(60) 的余弦时,您实际上计算的是非常接近但不等于 π/3 的数字的余弦。

您想要如何解决此问题取决于您的应用程序。在绝大多数实际应用中,像这样的微小错误实际上并没有什么影响。如果你想很好地呈现结果,那么你可以简单地四舍五入到你想要的位数。

When you convert an angle from degrees to radians, the result is a number that is usually not exactly representable as a floating-point number. You only have about 16 digits to work with.

So, when you compute the cosine of toRadians(60), you're actually calculating the cosine of a number very close to, but not equal to π/3.

How you want to fix this depends on your application. In the vast majority of real-world applications, a tiny inaccuracy such as this really doesn't make a difference. If you want to present the result nicely, then you can simply round to your desired number of digits.

国产ˉ祖宗 2024-12-18 07:57:10

分数存储为浮点数,本质上是一个近似值;这会导致一些小的错误,例如您所描述的错误。比我更聪明的人已经在 sin、cos、tan 和舍入误差中介绍了这一点

最好的解决方法是将值四舍五入到可接受的位数,如何在 Java 中将数字四舍五入到小数点后 n 位< /a>

Fractions are stored as floating-point numbers, which are essentially an approximate value; this causes small inaccuracies such as the ones you've described. Wiser heads than mine have covered this at sin, cos, tan and rounding error

The best work-around is to round the value to an acceptable number of digits, and several methods are covered at How to round a number to n decimal places in Java

不乱于心 2024-12-18 07:57:10

杰弗里所说的绝对正确,尤其是在这种情况下。但是,由于以下问题,您可能会发现类似的结果:通常浮点数在内部使用基数 2 表示。这意味着您的数字被写为尾数*2^(指数)。这实际上是获取您的数字并将其转换为基数 2,但这并不总是理想的。假设您想在内部表示 0.2。 0.2 的二进制表示不是有限的,它实际上是 0.00110011...(就像 1/3 在 10 基数中是 0.3333...)。由于您只存储有限位数的尾数,因此该数字在存储时会被截断,然后当它转换回十进制以进行打印时,它将显示为 0.19999...而不是 0.2。例如,我在 python 中经常注意到这一点。您只需在交互式 shell 中输入 0.2,它就会输出接近 0.2 但稍小或稍大的值。

What Jeffrey said is absolutely correct, especially in this case. However, you might find similar results due to the following issue: often floats are represented internally using base 2. What this means is that your numbers are written as mantissa*2^(exponent). This is effectively taking your number and converting it to base 2, which is not always ideal. Say you want to represent 0.2 internally. The binary representation of 0.2 is not finite, it is in fact 0.00110011... (just as 1/3 is 0.3333... in base 10). Since you're only storing a finite number of digits for the mantissa, the number will get truncated when stored, and then when it gets converted back to decimal for printing it will show as 0.19999... instead of 0.2. I've noticed this a lot in python, for example. You can just type 0.2 in the interactive shell and it will spit out something close to 0.2 but slightly smaller or larger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文