TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Patrick Annette
from: Mike Bilow
date: 1995-12-19 15:05:02
subject: Cube root

Patrick Annette wrote in a message to Mike Bilow:

MB>  BC> One would hope that an optimizing compiler manages to reduce 
MB>  BC> the log. form to the power form. Exp, log, and (I think) power 
MB>  BC> are intrinsic Intel FPU functions, but are all done using a 
MB>  BC> microcoded series and interpolation approach. Using the log 
MB>  BC> form not only incurs more function calls, but also cascades 
MB>  BC> errors in precision. One strives to avoid cascaded operations 
MB>  BC> unless needed.
MB> 
MB> Yes, there are direct FPU instructions for all of these 
MB> things.

 PA> Well, not exactly!  You do them all using the FPU, but it
 PA> requires  combining rudimentary operations like raising 2 to
 PA> a power, taking the logarithm of a nuumber to the base 2,
 PA> etc.  Take a look at Richard Startz's book (8087
 PA> Applications and Programming for the IBM PC and Other PCs). 
 PA> The newer 80287, 80387, 80486 all have essentially the same
 PA> FP instructions, and none directly do the log, exponential
 PA> or power functions. 

I think you misunderstood what I was talking about.  It is true that there
is no FPU instruction for doing logarithms and antilogarithms to the base
e, which is the only base supported in Pascal (as I understand it).  The
FPU uses the base 2, but the base is totally irrelevant for the purposes of
this discussion and can be considered a pure efficiency issue.  Presumably,
the Pascal compiler will implement the language's log() function by
computing the logarithm to the base 2 in the FPU and then converting this
to the logarithm to the base e.  There is some loss of accuracy involved in
these unnecessary conversions, but that is again not really the point.  As
long as you have a logarithm and an antilogarithm to the same base, that is
all that matters.

Raising a number to a power in the FPU is a matter of three instructions if
done properly, not counting the load and unload operations.  I saw your
assembly language post earlier, and there is a lot of housekeeping going on
with regard to storing the control word and so on.  Taking the logarithm to
base 2 is one instruction, multiplying by the other number is one
instruction, and then taking the antilogarithm to base 2 is one
instruction.  This all relies on the identity:

    y               y
   x  = exp  (log  x ) = exp  (y log  x)
           2     2          2       2

Now, the subtle part of this which may not be obvious is exactly what it
means to compute a logarithm or an antilogarithm in base 2 as opposed to
some other base.  It turns out that, like multiplying and dividing in base
2, this is a considerably simpler operation without the usual hassles.

MB> This is becoming clear to me.  As you may have guessed by 
MB> now, I don't know very much about Pascal.  I answered the 
MB> question from a mathematical point of view, never thinking 
MB> that some modern language would be unable to implement 
MB> exponentiation directly.

 PA> It's just not included in the library with most Pascal
 PA> compilers, just as it wasn't included with C in the old
 PA> days.  It's easy enough to add the feature in a Pascal unit,
 PA> just as it's added in a library for C.  

 PA> I know there are things C can do that Pascal can't, but
 PA> adding functions is no harder in Pascal than in C...

I understand this.  However, ANSI C defines a power() function, and it has
been in all compilers for years as part of the standard language.
 
-- Mike


--- 
* Origin: N1BEE BBS +1 401 944 8498 V.34/V.FC/V.32bis/HST16.8 (1:323/107)
SEEN-BY: 270/101 620/243 711/401 409 410 413 430 808 809 934 955 712/407 515
SEEN-BY: 712/517 628 713/888 800/1 7877/2809
@PATH: 323/107 150 3615/50 396/1 270/101 712/515 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.