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

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

问题描述


的类似问题 Math.Atan2或类实例C#中的问题

添加两个双重给定错误的结果

这是简单的线条:

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

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

piBy180 *度= -3.1415926535897931
但是Val = -3.1415927410125732

piBy180 * degree = -3.1415926535897931 but Val = -3.1415927410125732

alt text http://www.imagechicken.com/uploads/1262936639009840000.jpg
我真的不知道该怎么办..
请问一些关于这个问题,所以我可以指出出错的地方。

alt text http://www.imagechicken.com/uploads/1262936639009840000.jpg 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.

我正在使用MS Visual Studio的C#.net SP1。

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

推荐答案

如果您告诉我们 degree 的价值,那么我们可以尝试重现问题...

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

三件事:


  • 你应该期待一定量的flo中的错误起点算术。我不会期望这么多,但不要期待看到一个确切的值。

  • 字符串转换通常不会显示确切的值。我有一个文件 - DoubleConverter.cs - 一个 ToExactString 方法可以帮助诊断这种事情。

  • 您的应用程序是否使用DirectX?这可以改变处理器模式,以便有效地使用32位算术,即使你的代码有两倍。这似乎是我最可能的原因,但它仍然只是一个猜测。

  • 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.FpuPreserve 传递给 设备构造函数避免这样做。这将降低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天全站免登陆