www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/10/08:17:00

Message-ID: <352DFE3F.36AE@pobox.oleane.com>
Date: Fri, 10 Apr 1998 13:10:55 +0200
From: Francois Charton <deef AT pobox DOT oleane DOT com>
Organization: CCMSA
MIME-Version: 1.0
To: Michael Phelps <loverns AT welchlink DOT welch DOT jhu DOT edu>
CC: djgpp AT delorie DOT com
Subject: Re: Differences between -lm and not -lm
References: <Pine DOT SOL DOT 3 DOT 96 DOT 980409150133 DOT 20230A-100000 AT welchlink DOT welch DOT jhu DOT edu>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019