Undefined reference to `pow 'when changing exponent?

I'm having some weird issues with pow() . If i do

return 44330*(1 - pow((float) pressure / PRESSURE0, 1/5.255F));

where pressure is int32_t and PRESSURE0 is constant, I get an undefined error message on `pow '. However, if I do

return 44330*(1 - pow((float) pressure / PRESSURE0, 1.0F));

it's good. Am I doing something wrong?

thanks

+6
source share
1 answer

Typically, to use math functions such as sqrt() , pow() , etc., you need to link to the math library, libm (on gcc, use the -lm option).

However, the compiler can apply optimization, especially when function arguments are constants. Consider the following two simplified functions:

 double f1(int pressure) { return pow((float) pressure / PRESSURE0, 1/5.255F); } double f2(int pressure) { return pow((float) pressure / PRESSURE0, 1.0F); } 

This compiles to i386 code as follows (unnecessary assembler directives removed):

 f1: pushl %ebp movl %esp, %ebp subl $24, %esp ; (float) pressure / PRESSURE0 fildl 8(%ebp) fldl .LC0 fdivrp %st, %st(1) ; pow() fldl .LC1 fstpl 8(%esp) fstpl (%esp) call pow leave ret 
 f2: pushl %ebp movl %esp, %ebp ; (float) pressure / PRESSURE0 fildl 8(%ebp) fldl .LC0 fdivrp %st, %st(1) ; NO call to pow(x, 1.0), since it would not change the value of x popl %ebp ret 

In f2() , since the exponent of pow() is 1.0 , there is no need to call the pow() function - the compiler detects this and removes the function call. Therefore, if there are no other calls to any mathematical functions in your code, there is no need to refer to libm in this particular case.

see also

+7
source

Source: https://habr.com/ru/post/987546/


All Articles