Page 1 of 1

Directx driver effects double precision

Posted: Tue Feb 20, 2007 5:11 pm
by drewbacca
I am having a problem where using directx in the Irrlicht device is changing the precision of math using doubles. The math results aren't used for the position of anything and is completely unrelated to video. Below I have a simple example where i add a small amount to a larger number (the result is well within double precision). Doing this before or after creating the irrlicht device changes the results if I use directx. I have verified this with the debugger as well as just cout. I am using the Irrlicht 1.2 release with visual studio2005 on winXP64 (though everything im doing is 32bit). In the process of typing this up, i have also noticed that problems only happen when optimizations are disabled. If optimizations are enabled, it works.

An example:

Code: Select all

#include <irrlicht.h>
#include <iostream>

using namespace irr;
using namespace std;

#pragma comment(lib, "Irrlicht.lib")


int main()
{

	double bigNum = 4304763.0;
	double smallNum = 0.1;
	double answer = bigNum+smallNum;

	cout.precision(12);
	cout << answer<<endl;


	IrrlichtDevice *device = device = createDevice(video::EDT_DIRECT3D8, core::dimension2d<s32>(800, 600), 16, false, false, false, 0);
	//IrrlichtDevice *device = device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600), 16, false, false, false, 0);

	double bigNum2 = 4304763.0;
	double smallNum2 = 0.1;
	double answer2 = bigNum2+smallNum2;


	cout.precision(12);
	cout << answer2<<endl;

	return 0;
}
The code before createDevice calculates the correct answer, while the same code below does not. Changing the video driver to openGL fixes this problem, but I would like to use directx if possible. What is really weird, is that this problem only occurs when VS's optimizations are disabled. Things work fine if the code is optimized for speed.


Is this a bug in Irrlicht or something I don't understand about Visual studio?

Posted: Tue Feb 20, 2007 10:40 pm
by hybrid
This is a DirectX feature which you can avoid by setting the according flag in the Irrlicht parameters at device creation. Set HighPrecisionFPU to true.

Posted: Tue Feb 20, 2007 11:33 pm
by drewbacca
Thank you. I was only looking at createDevice and hadn't looked at the extra parameters set in createDeviceEx.

Posted: Tue Feb 20, 2007 11:52 pm
by sio2
Don't forget the Windows "feature" where the floating-point control word gets reset by loading a dll. I forget the specifics, but a search on MSDN should uncover the details.