I am developing code that will be used to control movement, and I am having a problem with the pow function. I am using VS2010 as an IDE.
Here is my problem: I have:
double p = 100.0000; double d = 1000.0000; t1 = pow((p/(8.0000*d),1.00/4.000);
When evaluating this last function, I do not get a better approximation as a result. I get 7 decimal digits correctly, and subsequent digits are all rubbish. I assume that the pow function only introduces any input variable as a float and handles the calculations.
- I'm right?
- If so, is there any code that I can get โinspiredโ to redefine pow for better accuracy?
Edit: Allowed.
In the end, I had problems with the FPU configuration bits caused by Direct3D, which were used in the OGRE 3D environment.
If you use OGRE, in the configuration GUI just set "Floating Point Mode = Negotiable".
When using raw Direct3D, when calling CreateDevice, be sure to pass the "D3DCREATE_FPU_PRESERVE" flag to it.
Original post:
Perhaps you are using libray, which changes the standard FPU precision to single point. Then all floating point operations, even on doubles, will actually be performed as operations with the same precision.
As a test, you can try calling _controlfp (_CW_DEFAULT, 0xfffff); (you need to turn it on), before you do the calculation, see if you get the correct result. This will reset the floating point to enter the control word in the default values. Please note that this will reset other settings as well, which may cause problems.
One common library that changes floating point precision is Direct3D 9 (and possibly other versions too): By default, it changes the FPU for single-precision when creating the device. If you use it, specify the D3DCREATE_FPU_PRESERVE flag when creating a device to prevent it from changing FPU accuracy.