Why there are two diffrent result in two almost same formula

  • Thread starter Thread starter XjAcKs
  • Start date Start date
The thing that's weird is that the calculation is being performed
differently, independent of the precision (which must be the same in
either case), depending on context. I don't have time to look at it now,
and maybe by the time I do, someone else will have already figured it
out. But, one would expect that "XNT * num" would always produce the same
value, and that for a given value, dividing by "SX" would also always
produce the same value.

Besides forcing rounding as already noted elsewhere in this thread, there is
another potential difference:
Without the call to ToSingle, the calculation involves only variables which
have verifiably constant values. So the compiler can perform constant
propagation, performing the calculation at compile time. If the FPU setup
registers (such as rounding mode) are set differently by the compiler, you
could get very slightly different results.

But I also don't agree with the OP when he said he got two different
results. The values of the two expressions are as equal as floating-point
numbers can get.
The only explanation for what you're seeing is that somewhere in there is
an incorrect assumption. To me, the most obvious suspect is the
Convert.ToSingle() method, but the documentation claims that
Convert.ToSingle(Single) always returns exactly the value that was passed
to it.

And given all that, the most obviously explanation I see is that there's a
compiler bug that is either choosing the wrong overload for the ToSingle()
method (i.e. using ToSingle(Double), which could produce a subtle
difference in the result), or is actually generating different IL simply
because of the presence of the method call. There is a more remote
possibility that the ToSingle(Single) method does not in fact do what the
documentation says it does.

In the meantime, you can look into those theories yourself if you like.
ILDASM.EXE can show you the IL generated in your program, where you can
look for specific differences in the code the compiler generates (i.e.
differences in the calculations, or using a different method overload than
one would expect given the code). To check to see what ToSingle(Single)
does, you'll either need to use Red Gate's Reflector (Google it) or
configure your IDE to download the .NET source while debugging, and then
step into the method.

I'm curious myself, so if there's not an answer twelve hours or so from
now (when I'll actually have time to look at it myself), I'll see what I
can figure out.

Finally, note that if it is a bug, it's possible it's been fixed since
.NET 2.0/VS 2005 (depending on whether it's a framework or compiler bug).
I'm only going to be able to test the code with .NET 3.5/VS 2008 at the
moment, so one thing you might want to do is try to test it yourself on a
more recent compiler and/or .NET version and see if it still happens.
That would be a very useful data point.

Pete



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4160 (20090616) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
Back
Top