From: Francois Charton Newsgroups: comp.os.msdos.djgpp Subject: Re: sqrt() problem Date: Wed, 11 Dec 1996 18:54:04 +0100 Organization: CCMSA Lines: 27 Message-ID: <32AEF53C.74D2@pobox.oleane.com> References: <32aa47fd DOT 8683086 AT nntp DOT southeast DOT net> <32AABA23 DOT 15FC AT cs DOT com> NNTP-Posting-Host: deef.pobox.oleane.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: fighteer AT cs DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp John M. Aldrich wrote: > > #include > > double s; > s = sqrt( pow( r, 2 ) - pow( m, 2 ) ); > This is *very* likely to be extremely slow... Power functions are usually much slower than multiplies... I'd suggest either : sqrt(r*r-m*m); or, better, sqrt((r-m)*(r+m)); (only one multiply) The latter formula has another advantage, it should be a less floating-point-error-prone : if r and m are close and very big, r*r - m*m will result in incorrect results Still better, formulae like r * sqrt((1-m/r)*(1+m/r)); (if r and m are both positive). Which are less rounding error prone, and will overflow less easily. Francois