LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

pow() does not handle fractional exponent correctly

Solved!
Go to solution

The pow() function does not appear to handle fractional exponents correctly.  The correct value is returned by pow() for the following code:

 

hole_position[B5_B].neutronCount.presentCount = pow ( (hole_position[10].detectorCurrent  *hole_position[12].detectorCurrent* hole_position[13].detectorCurrent), 0.333); 

 

For current values of 1.1E-7, 1.3E-7, and 1.4E-7, a value of 1.28E-7 is returned.

 

I had originally used the following code:

 

hole_position[B5_B].neutronCount.presentCount =
                pow ( (hole_position[10].detectorCurrent * hole_position[12].detectorCurrent
                        * hole_position[13].detectorCurrent), 1/3);   

 

For this case, pow() returned a value of 1 for the same current values.

 

Using 0.333 gives the needed precision for my application, but the behavior when passing a fractional exponent seems to be a minor bug in this library function. 

 

 

 

  

0 Kudos
Message 1 of 3
(7,805 Views)
Solution
Accepted by topic author Double_D

This is not a problem with pow, but rather a gotcha of the C language. In your code, 1 and 3 are being treated as integers, and the division of two integers yields an integer result. Thus 1/3 becomes the integer 0. This is why pow returns 1.

 

The solution is to do something to this effect:

 

((double)1) / 3

or better,

1.0 / 3.0

 

As long as you ensure that one of the operands is a double, the result will be too.

 

Mert A.

National Instruments

Message 2 of 3
(7,799 Views)

If you ever need to replace the numeric constants 1 and 3 with variables, you can mutiply them with 1.0 to convert them to double.

Sometimes I prefer this to type casting. 

S. Eren BALCI
IMESTEK
0 Kudos
Message 3 of 3
(7,778 Views)