04-22-2009 04:01 PM
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.
Solved! Go to Solution.
04-22-2009 05:16 PM
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
04-23-2009 04:47 AM
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.