Math.Sin,Math.Cos和Math.Tan precision和方式来正确显示这些正确、方式、Sin、Math

2023-09-03 22:02:23 作者:高龄儿童

我编码计算器在C#。

textBoxResult 是我显示该号码的文本框

textBoxResult is a text box where I display the number

各显神通是函数,它接受的角度度和报酬的弧度

recount is the function which takes the angle in degrees and returns in radians

我从 texBoxInput

public double recount(int number)
{
    double wyjscie = 0.0;
    double Number = number;
    wyjscie = Number * (Math.PI / 180);
    return wyjscie;
}

//function which is called out when user presses the button:
textBoxResult.Text = Math.Round(Math.Tan(recount(Convert.ToInt32(texBoxInput.Text))),2).ToString();

正如你看到的我是想用的时候要圆这个数字 Math.Tan ,但仍 Math.Tan 给我,从90度晒得1,63317787283838E + 16。

As you can see I was trying to round this number when using Math.Tan, but still Math.Tan gives me that tan from 90 degrees is 1,63317787283838E+16 .

我一直在试图寻找答案,但失败了。我无法弄清楚如何显示正确的结果。

I have been trying to find the answer but failed. I can't figure it out how to display correct result.

推荐答案

基本上,它看起来像这样有望从 Math.Tan 行为。我不知道其他语言非常好,所以我不知道这是正常的浮点运算或特定于C#实现。 (注:后来,我发现谷歌的在线计算器返回相同的建议,预期行为浮点三角函数,可能与该的 PI 的不合理和事实的双重precision的局限性数据类型)

Basically, it looks like this is expected behavior from Math.Tan. I don't know other languages very well, so I'm not sure if this is normal for floating point Math or specific to the C# implementation. (NOTE: Afterwards, I found that Google's online calculator returns the same suggesting it expected behavior for floating point trig functions, probably related to the fact that pi is irrational and the limitations of the double precision data type)

不过,从这个结果的工作向后我看到了 Math.Atan(//你的结果); Math.Atan(双。 PositiveInfinity时)都返回90度,这表明这种预期?

However, working backwards from this result I am seeing that Math.Atan(// your result); and Math.Atan(double.PositiveInfinity) both return 90 degrees, suggesting this is expected?

下面是我的测试:

var deg = 90.0;
var rads = deg * (Math.PI / 180);
var result = Math.Tan(rads);

if (Double.IsInfinity(result))
    Console.WriteLine("Tan of 90 degrees is Infinity");
else if (Double.IsNaN(result))
    Console.WriteLine("Tan of 90 degrees is Undefined");
else
    Console.WriteLine("Tan of 90 degrees is {0}", result);

Console.WriteLine("Arc Tan of {0} is {1} degrees", double.PositiveInfinity, Math.Atan(double.PositiveInfinity) * 180 / Math.PI);
Console.WriteLine("Arc Tan of {0} is {1} degrees", result, Math.Atan(result) * 180 / Math.PI);

其中给出的输出:

Which gives the output of:

Tan of 90 degrees is 1.63317787283838E+16
Arc Tan of Infinity is 90 degrees
Arc Tan of 1.63317787283838E+16 is 90 degrees

所以我的猜测是,除非有人能进来,并提供了一​​个解决方法,您可能需要围绕这一计划作为一个边缘的情况下,以得到正确的结果。

So my guess is unless someone can come in and provide a workaround, you might have to program around this as an edge case to get the correct result.

正确的结果对任何的三角函数将被限制的precision ,这是15显著的数字,因此,如果您需要更多的重要的是,你需要找到一个支持更多precise数学库。

The "correct result" for any of the trig functions will be limited to the precision of double, which is 15 significant figures, so if you need more than that, you will need to find a library that supports more precise mathematics.

由于 Math.Tan(Math.PI / 2)似乎提供,你可以做这样的事情的不良反应:

Since Math.Tan(Math.PI/2) seems to provide an undesirable response you could do something like this:

public double ComputeTangent(double angleRads)
{
    if (angleRads == Math.PI/2)
        return double.PositiveInfinity
    if (angleRads == - Math.PI/2)
        return double.NegativeInfinity

    return Math.Tan(angleRads);
}