Reputation: 2821
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??
Upvotes: 0
Views: 156
Reputation: 669
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);
Upvotes: 0
Reputation: 24857
23 to the power of minus five gives the result you are getting....
Upvotes: -2
Reputation: 582
It looks like your are computing (2.3*10)^-5 instead of 2.3*10^-5.
try:
floatcopy = floatcopy*pow(10,-exponent);
Upvotes: 2
Reputation: 23324
Your calculater is calculating 2.3*(10^-5). In your code you calculate (2.3*10)^-5.
Upvotes: 6