I'm trying to use the pow function in c++ but the result is not what I expect. Snippet:

#include <math.h>
float floatcopy = boost::lexical_cast<float>(copy); //Then floatcopy is 2.300000    
float exponent = boost::lexical_cast<float>(copy[foundEXP+1]); // Then exponent is 5.00000    
floatcopy = pow(floatcopy*10,-exponent);

Now, when typing 2.3*10^-5 on my calculator (or in my head..) I get as expected: 0.0000230

The above snipped results in 1.5536773e-007

What is the problem here??

有帮助吗?

解决方案

Your calculater is calculating 2.3*(10^-5). In your code you calculate (2.3*10)^-5.

其他提示

It looks like your are computing (2.3*10)^-5 instead of 2.3*10^-5.

try:

floatcopy = floatcopy*pow(10,-exponent);

You are not using correctly the pow function. What you are actually doing is (2.3 * 10)^-5, and what you try to do (if I am not wrong) is 2.3 * (10 ^ -5).

To do so, you should do:

floatcopy *= pow(10,-exponent);

23 to the power of minus five gives the result you are getting....

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top