Generally, in order to use math functions like sqrt()
, pow()
etc. you need to link with the math library, libm
(on gcc, use the -lm
option).
However, the compiler is free to apply optimizations, especially when the arguments to the functions 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 gets compiled into 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 to pow()
is 1.0
, there is no need to call the pow()
function - the compiler detects this and removes the function call.
Hence, if there are no other calls to any math functions in your code, it is not necessary to link against libm
in that particular case.
See also