tags:

views:

259

answers:

2

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

A: 

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.

popester
+6  A: 

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

Eric Lippert