Floating Point Addition Problem
I have a sticky and weird problem.
I have two C++ programs running under Visual Studio 2003. One program works correctly, the other has problems adding two double values. Take the following 3 simple test lines.
double dTest1 = 0.00061234567890;
double dTest2 = 3359.01234567890;
double dTest3 = dTest1 + dTest2;
Program that works, dTest3 = 3359.0129580245789
Program that doesn't work, dTest3 = 3359.0129394531250
Notes:
1 - The program that doesn't work may have come from the 16 bit world.
2 - I have checked the compiler and linker options between the 2 solutions and there isn't anything that is obviously different.
3 - I have turned ON the disassembly mode in the debugger for the two programs, so I can see what assembly instructions the two programs are executing. They are identical.
I expect the double dTest3 to provide around 15 digits of precision. It seems that, somehow, for the program that doesn't work, the CPU is giving me a float, instead of a double, for the dTest3 variable. It seems that I'm getting about 7 or 8 digits of precision for the program that doesn't work.
What gives!
I'm stumped!
Edit/Delete Message
[1287 byte] By [
pdavila] at [2007-11-11 8:39:28]

# 1 Re: Floating Point Addition Problem
16 bit, 32 bit, 64 bit, for floating point numbers, this does not matter (it does in terms of speed, but not precision).
My best guess, if the assembler is identical, is that your source code has a cast to a float but performs the computation into a double:
double test 3 = (float)test1 + (float) test2; //something like this
the compiler might promote the truncated values back to double (because of the result's type) but lose precision.
jonnin at 2007-11-11 21:01:14 >

# 3 Re: Floating Point Addition Problem
Jonnin,
Thanks for the response.
After a lot of system testing I was able to trace the problem to the addition of double variables. That's why I tried the simple 3 line test of adding the two double values that I had in my sample code below. I don't cast the doubles at all. I just add the two doubles and the CPU comes back with the incorrect 7 or 8 digit precision.
It's behaving as though it should be something in the compiler or linker options. Here are the options.
The compiler options:
/Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS"
/Gm /EHsc /RTC1 /MDd /Yu "Stdafx.h" /Fp ".\Debug/Application.pch" /Fo ".\Debug/"
/Fd ".\Debug/" /W3 /nologo /c /Wp64 /ZI
Here are the linker options:
/INCREMENTAL:NO /NOLOGO /DEBUG /SUBSYSTEM:WINDOWS /MACHINE:X86
I have not included the "/I" and other "harmless" options.
Any help is greatly appreciated.
Cuco
# 4 Re: Floating Point Addition Problem
Jonnin and Danny,
Finally, success.
It turns out that DirectX9.0 was the culprit. More specifically, the operator using DirectX.
There is a parameter in the "CreateDevice" call of DirectX, the 'BehaviorFlags', which must include "D3DCREATE_FPU_PRESERVE". Without this flag in the call to CreateDevice DirectX will reset the floating point unit control word to single precision.
Microsoft doesn't make it easy for us stressed out developers. Three days down the tube!