473,473 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Division inconsistent between Debug/Release builds

[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
Nov 21 '05 #1
3 1912
John,

If you haven't already, I suggest you read

http://www.yoda.arachsys.com/csharp/floatingpoint.html

The JIT compiler does different optimizations and may yield slightly
different results depending on if you're running a releaase or debug
build, and whether or not a debugger is attached.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
John...

"John C Kirk" <jk***@pmsi-consulting.com> wrote in message
news:79**************************@posting.google.c om...
[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
In addition to Mattias' suggestion on understanding what might be going
on...
I would also recommend explicitly defining what you want it to do.
By this, I mean explicitly define the variable types.
Given: Private Function DivideDouble(ByVal sngX As Single) As Double
Dim sngY As Single

sngY = sngX / 1000

Return 1 / sngY

End Function


You are inputting a Single and want out a Double.
However, sngY is a Single, and 1000 is an Integer due to no decoration.
Also, 1 / sngY is an Integer / Single.
Yes, these will be cast to an appropriate datatype when they are evaluated,
however you are leaving it up to the compiler and/or chance as to what that
would be. They might all be cast to doubles, but you don't know for sure.
Additionally, they might also be rounded to accomodate the least precision.

In this case, I would change it to look like the following:

Private Function DivideDouble(ByVal sngX As Single) As Double
Dim dblY As Double
Dim dblX As Double

dblX = Convert.ToDouble(sngX)
dblY = dblX / 1000.0#

Return 1.0# / dblY

End Function

Yes, it is more verbose than necessary for clarity.
Given the particular end result, you could eliminate the reciprocal step by
reversing the original division.

Return 1000.0# / Convert.ToDouble(sngX)

It might not change the results you are seeing, but by taking out the
additional division, there is one less place for a rounding error to be
introduced.

As far as the difference between modes, this is entirely possible.
You have various amounts of precision available. I am not certain what
exactly is going on in each scenario, but it does not surprise me they are
different. This is common, even expected, behaviour when dealing with
floating point variables.

Gerald


Nov 21 '05 #3
> In addition to Mattias' suggestion on understanding what might be going
on...
I would also recommend explicitly defining what you want it to do.
Thanks to both of you for the advice on that. I've found a workaround, so
that aspect isn't a major problem, it's more the different behaviour that
was bothering me.
As far as the difference between modes, this is entirely possible.
You have various amounts of precision available. I am not certain what
exactly is going on in each scenario, but it does not surprise me they are
different. This is common, even expected, behaviour when dealing with
floating point variables.


I know that Debug and Release modes actually generate different MSIL, so I
can accept them giving different results (although it's annoying). But is
there any advantage at all to running in Release mode inside the IDE? It
doesn't help you for testing, since you won't necessarily get the same
results as end users. And it won't help you to isolate errors, since you
can't break into the source code. The only advantage I can see is that it's
easier to click the "run" toolbar button than to browse for the file in
Explorer and double-click on it there, but surely that's just a question of
how the toolbar button code is implemented?

John
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kevin | last post by:
Hi All, Can someone please tell me what the difference is between DEBUG and TRACE. From what I can see, they are two different classes, but with the same members? Whats the purpose of have two...
3
by: | last post by:
Since I need to dotfuscate my exe file anyway, does it make any difference if I use Debug or Release versions. Would a Debug version be easier to decompile/study/reverse engineer than a Release...
6
by: swartzbill2000 | last post by:
Hello, I have a VB 2005 Express project with a TraceListener-derived class to route Debug.Print output to a log file. It works fine for Debug builds. What is the correct combination of changes to...
1
by: TheSteph | last post by:
Hi, Does anybody know what's the difference between the "Debug build" of a C# application and the "Release build" ? In my case, both are 774.144 bytes, and both seems to have the same
5
by: B. | last post by:
We just recently move to code from VC++6 to VC++.NET 2005 and upgrade the warning level from 3 to 4. In debug mode, we compile the application with no warning no error, but when I build it in...
6
by: Andrew Rowley | last post by:
I am having trouble getting debug and release builds to work properly with project references using C++ .NET and Visual Studio 2003. I created a test solution, with a basic Windows form C++...
3
by: Bob Johnson | last post by:
It is my understanding - and please correct me if I'm wrong - that when building a project in debug mode, I can deploy the .pdb file along with the ..exe and thereby have access to the specific...
3
by: TBass | last post by:
Hello, Is there a way to get Visual Studio 2003 look to one directory for debug version dlls when set to DEBUG and then to another directory where I store the release version of a dll when set...
5
by: Andy B | last post by:
How do you tell vs2005 to keep debug info out of your release builds? When I build a release and a debug version, there seems to be no difference in them at all...The release builds still have the...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.