Floating Point-mode woes

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Van
Hobgoblin
Posts: 512
Joined: Fri Nov 19, 2004 3:56 am

Floating Point-mode woes

Post by Van »

I have spent the entire day trying to figure out why this simple math problem was not working properly:

Code: Select all


double mTest = -706850036.0;
mTest += 1.0;

It was coming up with -706850048.0; (note that it should end in 035!). Then, just for kicks, I changed the floating point-mode on the startup screen from fastest to consistent and low and behold, the math works right.

I was under the impression that this feature was for the GPU and would not interfere with the standard math libs. Would someone explain to me what is actually happening behidn the scenes? Why is this feature interferring with math outside of Ogre?
voxel
Gnome
Posts: 334
Joined: Wed Aug 02, 2006 9:27 am
Location: Toronto, Canada

Re: Floating Point-mode woes

Post by voxel »

Van wrote:I was under the impression that this feature was for the GPU and would not interfere with the standard math libs. Would someone explain to me what is actually happening behidn the scenes? Why is this feature interferring with math outside of Ogre?
I'm guessing that "fastest" implies single-precision floats. Converting between single and double isn't always exact either.

I've noticed Visual C++ has fast and strict modes for floating-point optimizations.

http://msdn.microsoft.com/library/defau ... apoint.asp

Not sure if Ogre uses these C++ settings.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Post by Kojack »

The floating point mode has nothing to do with the gpu. It controls how D3D uses the fpu.

By default, Direct3D changes the fpu to single precision. This means all maths are performed as floats, even if you have them as doubles in your code. When you create the Direct3D device, you can give the D3DCREATE_FPU_PRESERVE flag, which tells D3D to leave the fpu state alone.

In Ogre, "Fastest" starts D3D in the default way. "Consistent" passes the D3DCREATE_FPU_PRESERVE flag.

From the DX sdk:
D3DCREATE_FPU_PRESERVE
Forces Direct3D to not change the floating-point unit control word, running the pipeline using the precision of the calling thread. Without this flag, Direct3D defaults to setting the floating-point unit to single-precision round-to-nearest mode. Using this flag with the floating-point unit in double-precision mode will reduce Direct3D performance.
Note Portions of Direct3D assume floating-point unit exceptions are masked. Unmasking such exceptions may lead to undefined behavior.
Van
Hobgoblin
Posts: 512
Joined: Fri Nov 19, 2004 3:56 am

Post by Van »

Ah thank you.

Is there a way I can FORCE the "consistent" flag in code?
User avatar
Deranged
Greenskin
Posts: 114
Joined: Sun Aug 14, 2005 11:48 pm

Post by Deranged »

If you can give mRenderSystem->setConfigOption("Floating-point mode", "Consistent"); a shot, before initializing root. I'm not sure if you can do that with the config window, I don't use it.
-Sheridan Bulger