2015-06-08 04:03 AM
Hi everybody,
I use the function powf (that is supposed to return a float), included in the library#include <math.h>
This is the definition of the function in math.h :
float
powf _PARAMS((
float
,
float
));
When I run this code:
float
EXPONENT_BASE_2;
EXPONENT_BASE_2 = powl(4.73, 0);
The result is
125410439
instead of
1254104217423
Powf don't return a float but a int. Do you have any idea of what happens? i also tried pow and powl but it's the same result...Thanks a lot!
2015-06-08 04:21 AM
This is the definition of the function in math.h :
float
powf _PARAMS((
float
,
float
));
When I run this code:
float
EXPONENT_BASE_2;
EXPONENT_BASE_2 = powl(4.73, 0);
Perhaps you should use a different character font in your editor, otherwise you would realized that you call the wrong function. No wonder the function returns an integral number.
Edit:
The result is
125410439
instead of
1254104217423
BTW, I would not expect such a numerical resolution from a float variable ...2015-06-08 06:00 AM
And this is with what tool chain, and printed out how?
2015-06-08 06:14 AM
I use GNU Tools ARM Embedded 4.9 and I print out the variable on my board graphic screen, or with the debugger of Coocox.
About the example I give, it's just to illustrate my problem: I have only int returns, even when it's supposed to returns a floats.2015-06-08 07:04 AM
I have only int returns, even when it's supposed to returns a floats.
No. If your code is exactly like the example you give, it is not. The powl() function returns a long (integer) value - casting to float doesn't make it a float.
2015-06-08 09:07 AM
Do you have any idea of what happens?
24-bit mantissa? So perhaps 7-8 digits?2015-06-09 12:24 AM
Sorry but I didn't understand... The function powf is supposed to return floats... Otherwise why do they created pow (to return an int) and powf (to return a float) ? A power exponent without floating point is quite useless in my case...
Thanks!2015-06-09 03:45 AM
Sorry but I didn't understand... The function powf is supposed to return floats...
To be more blunt: you are calling the wrong function in your given example. The pow
F
() function returns a float value, but the powL
() function (which you actually called
) does return an integer. (Capitalisations in function names only for clarification).2015-06-09 05:47 AM
Sorry but I didn't understand... The function powf is supposed to return floats... Otherwise why do they created pow (to return an int) and powf (to return a float) ? A power exponent without floating point is quite useless in my case..
Then you really need to review your understanding of floating point, and precision, and then pick appropriately sized variables to hold the digits you expect. You have a 32-bit float, holding a 24-bit mantissa (the digits), and 8-bit exponent. In 24-bit unsigned numbers you have a range of 16 million, 16777216, observe that it has 8 digits with values, if you multiply it by an exponent, say 6 (106), you have 16777216000000, you DON'T get any more significant digits, you get ZEROS. The processor uses base 2 (binary) exponents, but the concept is the same, except some fractions/numbers are going to be poorly represented. You're not going to magically get 16 digits of precision you seem to be expecting. If you're using floating point it's critical you understand how they work and their limitations.http://en.wikipedia.org/wiki/Single-precision_floating-point_format
2015-06-09 06:05 AM
Capitalisations in function names only for clarification
OP : ''Powf don't return a float but a int. Do you have any idea of what happens? i also tried pow and powl but it's the same result.'' Stop banging on about F or L, it's not the underlying issue here.