本文介绍了Math.Cos& amp;C#中的Math.Sin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些我认为应该相当简单的事情.我有一个角度,一个位置和一个距离,我想从这些信息中找到X,Y坐标.

I'm trying something that I thought should be reasonably simple. I have an angle, a position and a distance and I want to find the X,Y co-ordinates from this information.

使用90度的示例输入,我使用以下代码将值转换为弧度:

With an example input of 90 degrees I convert the value to radians with the following code:

public double DegreeToRadian(float angle)
{
  return Math.PI * angle / 180.0;
}

这给了我1.5707963267949弧度然后当我使用

This gives me 1.5707963267949 radiansThen when I use

Math.Cos(radians)

最后我得到的答案是:6.12303176911189E-17

I end up with an an answer of: 6.12303176911189E-17

到底发生了什么?90度的余弦应该为0,那么为什么会出现这样的偏差呢?更重要的是如何停止呢?

What the heck is going on? The cosine of 90 degrees should be 0, so why am I getting such a deviance... and more importantly how can I stop it?

推荐答案

让我再回答一个问题:您认为6.12303176911189E-17离0有多远?您所说的 deviance 实际上是由于浮点数在内部存储的方式.我建议您阅读以下文章.在.NET中,它们是使用 IEEE 754标准存储的.

Let me answer your question with another one: How far do you think 6.12303176911189E-17 is from 0? What you call deviance is actually due to the way floating point numbers are internally stored. I would recommend you reading the following article. In .NET they are stored using the IEEE 754 standard.

这篇关于Math.Cos& amp;C#中的Math.Sin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:14