cancel
Showing results for 
Search instead for 
Did you mean: 

Powf (power exponent) don't return float values

jean_prieur
Associate III
Posted on June 08, 2015 at 13:03

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!

13 REPLIES 13
AvaTar
Lead
Posted on June 08, 2015 at 13:21

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 ...
Posted on June 08, 2015 at 15:00

And this is with what tool chain, and printed out how?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jean_prieur
Associate III
Posted on June 08, 2015 at 15:14

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.

AvaTar
Lead
Posted on June 08, 2015 at 16:04

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.

Posted on June 08, 2015 at 18:07

Do you have any idea of what happens?

24-bit mantissa? So perhaps 7-8 digits?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jean_prieur
Associate III
Posted on June 09, 2015 at 09:24

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!

AvaTar
Lead
Posted on June 09, 2015 at 12:45

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 pow

L

() function (

which you actually called

) does return an integer. (Capitalisations in function names only for clarification).

Posted on June 09, 2015 at 14:47

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 09, 2015 at 15:05

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.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..