开发者

Rounding Error in C#: Different results on different PCs

I have a function in C# that returns the following:

...
float amount = smallestPercentage * (float)quantity;
return (int)amount;

Now I know I am suppose to use Convert.Int32(amount) rather than type cast an int, and that has fixed the开发者_开发技巧 problem. But my problem was really this...

When developing my program at home (Windows Vista) I would get a return value of 1, but when a deployed the program to another environment (Windows XP), I would get a return value of 0.

I was wondering if this has to do with the Windows version, the .NET version or even the CPU processor?

Thanks.

David


In fact, you can get different results:

  • on different machines

  • depending on whether you compiled with debug or retail build settings

  • depending on whether you did the math in compile-time constants or runtime values

  • how local variables and other temporary values are used in your method

  • and so on.

The C# specification calls out that floating point arithmetic may be done in higher precision than you expect. It's never done in lower precision than you'd expect, but we reserve the right to use higher-precision algorithms on certain hardware and with certain optimizations available, any time the jitter thinks that it can get away with it. That means that in operations that are highly sensitive to small changes in precision -- like rounding -- can give very different results seemingly without explanation.

You are not the first Stack Overflow user to discover this fact: Problem converting from int to float


When dealing with floating point numbers its really advisable that you use some sort of rounding routine. In C#, I believe the best approach is the Math.Round method.

As to why its happening, different processors have different routines for computing floats and doubles. On your target machine you're likely getting a value slightly below 1 (say, .999987) that when casted gets turned into 0. Floats have been around since the CLR was created, so this is most likely a processor specific thing. OSes very rarely interfere with direct application code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜