I am in a bit of state at the moment, I am really surprised at what I have found, please see the two code samples below. The first is Visual Basic and yields a result of: 100040624.0 the second C# and yields a result of: 100040629.0, which is correct. This error is causing my Line Circle intersection function to fail!! Oh my God have just spent the last two years writing my app in VB and now have to convert to C#.
Dim a, b, c As Double
a = 10002
b = 25
c = a * a + b * b
result: 100040624.0
double a,b,c;
a = 10002;
b = 25;
c = a * a + b * b;
result: 100040629.0
I would really appreciate your comments on what may be occurring, I spend I lot of time dealing with Floating Point arithmetic issues, but this has really surprised me.
Thank you
Julian

Floating Point arithmetic difference between VB and C#
dcritch
Freek Bos
OK, found what’s causing the problem: DirectX! After creating a device the error is present, see below. I will move this thread over to the directX forum.
using System;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
private Device m_device = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PresentParameters presentParameters = new PresentParameters();
presentParameters.Windowed = true;
presentParameters.SwapEffect = SwapEffect.Discard;
double a;
double b;
double c;
a = 10002;
b = 25;
c = a * a + b * b;
MessageBox.Show(String.Format("Result = {0}", c), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
m_device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParameters);
a = 10002;
b = 25;
c = a * a + b * b;
MessageBox.Show(String.Format("Result = {0}", c), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Kiku
This is a known “problem”. Direct3D works internally only with single precision and set the FPU flags during device creation. You can change this that it set and reset it anytime you call a Direct3D function and not global.
Add CreateFlags.FpuPreserve to your create flags.
carzyR
double a,b,c;
a = 10002d;
b = 25d;
c = a * a + b * b;
joerage
RSatter
Thanks for the reply, your comments prompted me to create a new test project and yes you are right it’s Ok in that one. Yes I’m just looking in the debugger as well.
I have tried to see if there are any settings that are different, but to no avail. I had the test code in the event of a toolbar button, I have tried moving the code to different areas of my app and sometimes it works and sometimes not! I can’t find a pattern yet though.
Not sure where to go from here!!
AJ_101
What can I say, thank you so much people!!
Julian
ocanon
Hi, unfortunately that did not work, in my actual function that I found the problem in, the variables are actually arguments of type double. So I never assign any values as per the sample. The sample code is for simplicity to demonstrate the problem.
Are there any advanced techniques for debugging at a lower level, I’m not sure how to proceed with the problem, I can’t thing of a way to provide an actual demo app without giving out my whole app.
Thanks for you help in this matter
Julian
syouki
Something else must be going on... Did you try it in a completely-standard generated application How were you displaying the result I just looked at 'c' in the debugger.