Mail Archives: djgpp/1998/04/10/08:17:00
Michael Phelps wrote:
>
> I know that there are differences between compiling with -lm and
> using the default math library, but I didn't know that it was that
> significant. I wrote a little routine to extract a variable number of
> digits from an int and return them. Observe:
>
>[example snipped]
>
> This is correct! I assume that it is the pow() function that differs
> here. Should this be the correct behavior when it is not linked with the
> math library?
>
When you do not link libm, the math functions in libc are linked by
default. The pow() function in libc, although it is quite all right in
most cases, is a little less elaborate than that of libm: when you give
pow() an integer power, the libm pow knows that pow(x,4)=x*x*x*x, the
libc pow() does it the hard way. This reduces the round off errors (to
zero when both arguments are integers).
This of course is not 100% efficient: libm pow() still doe snot know that
pow(x,1.5)= sqrt(x*x*x), so you could still get errors when using pow()
to get integer results.
Anyway, you should avoid using pow() when you have an alternative
solution (products, square roots...): it is always slower, and always
less precise.
Francois
- Raw text -