Pow for doubles

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.

+4
source share
2 answers

Perhaps you are using libray, which changes the standard FPU precision to single-precision. Then all floating point operations, even on double s, will actually be performed as operations with the same precision.

As a test, you can try calling _controlfp( _CW_DEFAULT, 0xfffff ); (you need to include <float.h> ) before doing the calculation to see if you get the correct result. This will reset the floating point control word for default values. Please note that it will also reset with other parameters, which may cause problems.

One common library that changes floating point precision is Direct3D 9 (and possibly other versions): by default, it changes the FPU to a single precision when creating the device. If you use it, specify the D3DCREATE_FPU_PRESERVE flag when creating the device to prevent it from changing FPU accuracy.

+4
source

How did you determine that you get only 7 digits of accuracy? Do you print t1 and specify the correct output format? On my machine, with VS2010, the following code:

 int main() { double p = 100.0000; double d = 1000.0000; double t1 = pow(p/(8.0000*d),1.00/4.000); printf("t1=%.15f\n", t1); // C std::cout << "t1=" << std::setprecision(15) << t1 << '\n'; // C++ } 

Produces this conclusion:

 t1=0.334370152488211 t1=0.334370152488211 
0
source

All Articles