[This is a repost of a message that I sent to
microsoft.public.dotnet.vb.general on 7th September 2004; it looks
like this group is more active than that one, so there may be MVPs who
can read this and pass a bug report on to Microsoft if appropriate.]
I've come across an odd situation, where doing a floating point
division produces different results for the same numbers. Basically,
there are 4 ways to run this application:
A) Debug build, inside the IDE
B) Debug build, outside the IDE (e.g. launched from Explorer)
C) Release build, inside the IDE
D) Release build, outside the IDE
Using methods A-C produces one result, and using method D produces a
different result.
The minimal code to reproduce this problem follows. Create a standard
Windows application, one form, one button, then copy this code into
Form1:
***
Private Function DivideDouble(ByVal sngX As Single) As Double
Dim sngY As Single
sngY = sngX / 1000
Return 1 / sngY
End Function
Private Function DivideSingle(ByVal sngX As Single) As Single
Dim sngY As Single
sngY = sngX / 1000
Return 1 / sngY
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim sngZ As Single
Dim dblZ As Double
sngZ = DivideSingle(15)
dblZ = DivideDouble(15)
MessageBox.Show("Z (single) = " & sngZ.ToString() & ", " &
vbNewLine & _
"Z (double) = " & dblZ.ToString())
End Sub
***
The idea is that this calculation involves 3 numbers, X/Y/Z. Y=X/1000,
and Z=1/Y.
If you run it with X=15 (as above), then methods A-C produce:
"Z (single) = 66.66667,
Z (double) = 66.6666681567828"
whereas method D produces:
"Z (single) = 66.66666,
Z (double) = 66.6666666666667"
If you run it with X=25, methods A-C produce:
"Z (single) = 40,
Z (double) = 39.9999994039536"
whereas method D produces:
"Z (single) = 40,
Z (double) = 40"
There are various workarounds for this, e.g. putting the calculations
into the button click event rather than calling separate functions, or
using doubles everywhere rather than singles. The truncating/rounding
seems slightly odd, but I can live with that. My main concern is that
this produces different results in the different cases, which has
caused me trouble with my testing.
Any thoughts? I'm using VB.NET 2003 with the .NET framework v1.1. My
machine has SP1 for the framework, but this also occurs on machines
that don't have the service pack.
John