双变量保存错误的值 [英] Double variable keeping wrong value

查看:191
本文介绍了双变量保存错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Simimilar问题 <一href="http://stackoverflow.com/questions/1957801/math-atan2-or-class-instance-problem-in-c">http://stackoverflow.com/questions/1957801/math-atan2-or-class-instance-problem-in-c 和 <一href="http://stackoverflow.com/questions/1193630/add-two-double-given-wrong-result">http://stackoverflow.com/questions/1193630/add-two-double-given-wrong-result

Simimilar problem to http://stackoverflow.com/questions/1957801/math-atan2-or-class-instance-problem-in-c and http://stackoverflow.com/questions/1193630/add-two-double-given-wrong-result

这是一些简单的线条:

public static String DegreeToRadianStr(Double degree)
    {
        Double piBy180 = (Math.PI / 180);

        Double Val = (piBy180 * degree);  // Here it is giving wrong value
    }

piBy180 *度= -3.1415926535897931 但瓦尔= -3.1415927410125732

piBy180 * degree = -3.1415926535897931 but Val = -3.1415927410125732

我真的不知道该怎么办...... 请询问有关这一些问题,这样我可以指出它是怎么了。

I really have no clue what to do .. Please ask some questions regarding this, so that I can point out where it is going wrong.

这是惊人的,piBy180是保持正确的值。

It is amazing that piBy180 is keeping correct value.

同样的事情发生与其他功能也是如此,但是有些我怎么操纵,以获得正确的值。

Same thing is happening with other functions too, But some how I manipulated to get correct values.

我使用微软的Visual Studio的C#.NET SP1。

I am using MS Visual Studio's C#.net SP1.

推荐答案

这将帮助,如果您对我们的那么我们可以尝试重现该问题的价值。 ..

It would help if you told us the value of degree then we could try to reproduce the problem...

三样东西:

  • 您应该期待在浮点运算一定的误差。我不希望这么多,但不要指望看到一个准确值。
  • 字符串转换,一般不会告诉你确切的价值。我有一个文件 - DoubleConverter.cs - 以 ToExactString 方法,可以帮助诊断这样的事情。
  • 是使用的DirectX您的应用程序呢?这可以改变处理器模式有效地利用32位运算,即使你有双打在code。这在我看来是问题的最可能的原因,但它仍然只是一个猜测真的。
  • You should expect a certain amount of error in floating point arithmetic. I wouldn't expect this much, but don't expect to see an exact value.
  • String conversions generally won't show you the exact value. I have a file - DoubleConverter.cs - with a ToExactString method which can help diagnose this sort of thing.
  • Is your app using DirectX at all? That can change the processor mode to effectively use 32 bit arithmetic even when you've got doubles in your code. That seems to me to be the most likely cause of the issue, but it's still just a guess really.

编辑:好了,所以这听起来像我猜得不错,和DirectX可能是原因。你可以通过 CreateFlags.Fpu preserve 到的 设备构造< /一>,以避免它这样做。这将减少的DirectX,无可否认的性能 - 但这是你需要考虑自己的权衡

Okay, so it sounds like I guessed right, and DirectX is probably the cause. You can pass CreateFlags.FpuPreserve to the Device constructor to avoid it doing this. That will reduce the performance of DirectX, admittedly - but that's a tradeoff you'll need to consider for yourself.

这篇关于双变量保存错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆