[comp.lang.pascal] TP: Exponentiation?

defaria@hpclapd.HP.COM (Andy DeFaria) (07/24/90)

Perhaps I'm just brain-dead  and unable to  hear the  doctor  pronnounce me
comotose (sp?) but how to you do exponentiation in TP 4.0  (or 5.0 or 5.5).
I scanned the section about arithmetic operators in the TP book that I have
(5.5) and  searched the index but I  found no mention on exponentiation and
the usually "**" operator only produced a compile error.  What gives?

ehrlich@bimacs.BITNET (Gideon Ehrlich) (07/25/90)

In article <950036@hpclapd.HP.COM> defaria@hpclapd.HP.COM (Andy DeFaria) writes:
>Perhaps I'm just brain-dead  and unable to  hear the  doctor  pronnounce me
>comotose (sp?) but how to you do exponentiation in TP 4.0  (or 5.0 or 5.5).
>I scanned the section about arithmetic operators in the TP book that I have
>(5.5) and  searched the index but I  found no mention on exponentiation and
>the usually "**" operator only produced a compile error.  What gives?

Very easy. Either use    x**y = exp(y*ln(x))
           or write your own function ,using Tailor sequence.

jeroenk@cnps.PHILIPS.nl (Jeroen Kessels) (07/25/90)

defaria@hpclapd.HP.COM (Andy DeFaria) writes:

> how to you do exponentiation in TP 4.0  (or 5.0 or 5.5).
> ...
> the usually "**" operator only produced a compile error.  What gives?

If you mean 'exponential' (e raised to the x'th power) then try the
'exp()' function. If you mean 'exponentiate' (x raised to the y'th
power) then you could use the following function of mine, there is no
ready-made function.


{$N+}

function Power(x, y : extended) : extended;
{ Return x to the power of y. Note: a non-integer power of a negative
  number is not defined in mathematics. This routine will therefore
  round y to an integer if x is negative. }
var
  i : longint;
begin
if x > 0
  then Power := exp(y*ln(x))
  else if x = 0.0
    then Power := 0.0
    else
      begin
      i := round(int(y));
      if odd(i)
        then Power := -exp(ln(abs(x)) * i)
        else Power := exp(ln(abs(x)) * i);
      end;
end;
-- 
Jeroen C. Kessels
Software Engineer, Philips C&P-LSS
VA-25, P.O. Box 218, 5600 MD Eindhoven, The Netherlands
Uucp = jeroenk@cnps.philips.nl

granoff@vaxwrk.enet.dec.com (Mark H. Granoff) (07/25/90)

In article <950036@hpclapd.HP.COM>, defaria@hpclapd.HP.COM (Andy DeFaria)
writes:
> Perhaps I'm just brain-dead  and unable to  hear the  doctor  pronnounce me
> comotose (sp?) but how to you do exponentiation in TP 4.0  (or 5.0 or 5.5).
> I scanned the section about arithmetic operators in the TP book that I have
> (5.5) and  searched the index but I  found no mention on exponentiation and
> the usually "**" operator only produced a compile error.  What gives?

Nothing gives.  There's no built-in operator in Turbo Pascal for
exponentiation.  Write a function instead.

Of course, if you are using C++, you could overload an operator, like ^ for
example.

---------------------------------------------------------------------------
Mark H. Granoff   |    Enterprise Integration Services/Engineering VAXworks
---------------------------------------------------------------------------
Digital Equipment Corporation | ARPAnet: granoff@vaxwrk.enet.dec.com
129 Parker Street             | Usenet : ...!decwrl!vaxwrk.dec.com!granoff
PKO2-1/M21                    | AT&T   : +1 508 493 4512
Maynard, MA 01754             | FAX    : +1 508 493 2240
---------------------------------------------------------------------------
Opinions herein are my own and do not necessarily reflect those of Digital.
---------------------------------------------------------------------------

hardarso@currituck.cs.unc.edu (Kari Hardarson) (07/25/90)

Could some kind netlander point me to a good article/book that compares
the various windowing environments and describes the differences in 
programmin under them? Specifically, I need a comparison of the Mac
Toolbox, OS/2, MS-Windows 3.0, X-Windows, Sunview, Smalltalk MVC or
any subset or superset thereof. Thanks!




Kari Hardarson         |  T'was brillyg and the slithy toves
217 Jackson Circle     |  did gyre and gimble in the wabe...
Chapel Hill, NC 27514  |  (Jabberwocky by Lewis Carroll)

ts@uwasa.fi (Timo Salmi LASK) (07/26/90)

In article <950036@hpclapd.HP.COM> defaria@hpclapd.HP.COM (Andy DeFaria) writes:
>Perhaps I'm just brain-dead  and unable to  hear the  doctor  pronnounce me
>comotose (sp?) but how to you do exponentiation in TP 4.0  (or 5.0 or 5.5).
... rest deleted dor brevity ...

In addition to the codes already posted, there are exponentiation
routines of varying generality in the (/pc/ts/)tspas20.arc units
collection which was announced in this group about 24 hours before
this posting.  As everyone points out, exponentiation routines must
be written separately, since TP does not include an exponentiation
operator.

...................................................................
Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

hardarso@arras.cs.unc.edu (Kari Hardarson) (07/26/90)

I have two questions about TP. Is there a good reason why a function
in TP cannot return a record? Also: is there any way to implement
processes in your programs? A reserved word like FORK doesn't exist but
I thought someone might know about a (not-too-cludgy) fix.



Kari Hardarson         |  T'was brillyg and the slithy toves
217 Jackson Circle     |  did gyre and gimble in the wabe...
Chapel Hill, NC 27514  |  (Jabberwocky by Lewis Carroll)

eli@smectos.gang.umass.edu (Eli Brandt) (07/26/90)

In article <2041@bimacs.BITNET> ehrlich@bimacs.biu.ac.il.UUCP (Gideon Ehrlich) writes:
>
>Very easy. Either use    x**y = exp(y*ln(x))
>           or write your own function ,using Tailor sequence.

The power function is fine, but you should really never use a Taylor sequence for
*anything* other than maybe prototyping.  If you write your own power function, 
you should consider these modifications:
   1) Check for negatives and 0^0, obviously.
   2) A significant fraction of calls will probably involve integer powers.  The
      way to handle this is to use a simple for loop for low powers and a repeated
      squaring loop for higher powers.  The crossover point depends on the machine
      and compiler but is usually around 5 to 10.
   3) If you have a power y which is i+r, where i is floor(y), you can use your
      integer-power routine and multiply the result by the output of a power
      routine optimized for its fixed domain.  I didn't get any significant
      speedup with this, though.

mecklenburg@storm.dnet.nasa.gov (07/27/90)

There is no exponentation in Pascal.  You will probably have to
write your own function to perform this task we take for granted in
other high level programming languages.


Rick Mecklenburg
NASA/Marshall Space Flight Center
Huntsville, AL
(205)-544-8294

"Where summer lasts and lasts and lasts...."

mike@DRD.Com (Mike Rovak) (07/27/90)

jeroenk@cnps.PHILIPS.nl (Jeroen Kessels) wrote:
} defaria@hpclapd.HP.COM (Andy DeFaria) writes:
} 
} > how to you do exponentiation in TP 4.0  (or 5.0 or 5.5).
} > ...
} > the usually "**" operator only produced a compile error.  What gives?
} 
    ... stuff deleted...

} function Power(x, y : extended) : extended;
} { Return x to the power of y. Note: a non-integer power of a negative
}   number is not defined in mathematics. This routine will therefore
}   round y to an integer if x is negative. }

As an aside, technically speaking, I'm sure you realize that non-integer
powers of negative numbers ARE defined in mathematics.  They produce
imaginary numbers.   

It would be more accurate to say that it's not defined for the domain of
REAL numbers.

picky picky picky    :-)

------------------------------------------------------------------------
Disclaimer: My opinions do not necessarily reflect those of my employer.
========================================================================
     mike@DRD.Com  
     uunet!apctrc!drd!mike
========================================================================

reagan@hiyall.enet.dec.com (John R. Reagan) (07/30/90)

In article <23995@adm.BRL.MIL>, mecklenburg@storm.dnet.nasa.gov writes...
>There is no exponentation in Pascal.  You will probably have to
>write your own function to perform this task we take for granted in
>other high level programming languages.
> 
> 
In the new Extended Pascal standard (already approved by ANSI, IEEE, & BSR),
the Pascal committee has added exponentiation to the language.  As a matter
of fact, we added TWO operators.  We added "**" for raising things to
real powers and "POW" for raising things to integer powers.

---
John Reagan
Digital Equipment Corporation
reagan@hiyall.dec.com
---

phmb@otter.hpl.hp.com (Peter Brooks) (07/31/90)

>In the new Extended Pascal standard (already approved by ANSI, IEEE, & BSR),
>the Pascal committee has added exponentiation to the language.  As a matter
>of fact, we added TWO operators.  We added "**" for raising things to
>real powers and "POW" for raising things to integer powers.

All well and good, but what if the result is complex? In order for the 
standard to be of use, Pascal ought to have a complex type and the 
above operators ought to return range/domain error if an attempt is
made to return a complex to a real.

In TP, the problem is compounded since the function will not return a
record, so even if you code a correct function returning you home
rolled complex record, it won't compile. I think that Borland is
encouraging you to either write a macro pre-processor to expand '**'
or '^' into in-line code before giving it to TP (not too difficult vide
software tools in Pascal) or to modify a global. I cannot believe that
any blue blooded Pascal adherent would feel anything other than
a chill of horror at the thought of modifying a global (or perhaps
a childhood memory of FORTRAN [child abuse of the mind]) so this 
must mean that Borland expects us to write macros. The question for 
Borland is then; where are the clips, stubs or hooks to allow us to
integrate our pre-processors into turbo.exe or tpc.exe?

My HP-28C happily returns the right result, changing into the complex
domain when required. The only little gripe is that it isn't possible to
set it to a mode where complex is the default ( why not hex, oct, dec,
real, complex ?).

Peter Brooks

milne@ics.uci.edu (Alastair Milne) (08/11/90)

In <17410@dime.cs.umass.edu> eli@smectos.gang.umass.edu (Eli Brandt) writes:

>In article <2041@bimacs.BITNET> ehrlich@bimacs.biu.ac.il.UUCP (Gideon Ehrlich) writes:
>>
>>Very easy. Either use    x**y = exp(y*ln(x))
>>           or write your own function ,using Tailor sequence.

    I was taught you shouldn't use exp(y*ln(x)) unless you really have
    to, because both of them are bound to be implemented in your library
    as approximations, and often not very good ones, so the result will
    get back a certain amount of error and multiply it by error.  Not good.

>you should consider these modifications:
>   1) Check for negatives and 0^0, obviously.
>   2) A significant fraction of calls will probably involve integer powers.  
>     The way to handle this is to use a simple for loop for low powers 
>     and a repeated squaring loop for higher powers.  The crossover point 
>     depends on the machine and compiler but is usually around 5 to 10.

   I was shown one that handles any non-negative integer exponent, with limited
   complexity (linear, I think, though I'm too tired to remember just now).
   You may remember the shift-and-add implementation of multiplication.
   This is a shift-and-multiply for exponentiation.  Start your power at 1.
   If the low bit of the exponent is 1, multiply the base into the power.
   Either way, square the base and shift the exponent right 1.  
   Repeat until the exponent is 0 and return the accumulated power.  
   At most, 32 multiplications (assuming a 16-bit word), usually much less.  
   And if the base is also an integer, 16 of the multiplications become 
   left shifts.

   For floating-point bases, this reduces the error a lot (unless your
   squaring operation is poor).

   This should be checked, since I'm doing it from memory, but I believe
   it's correct.

   As for floating-point exponents, all I can suggest is finding your 
   own very good approximations of ln and exp.  (I believe there's
   a continued fraction for either exp(x) or x*exp(x) that behaves 
   very well and has amazingly few operations.)

   That, however, plumbs the depths of what I remember from numeric
   analysis.  Hope it helps.

   Alastair Milne

milne@ics.uci.edu (Alastair Milne) (08/11/90)

>In the new Extended Pascal standard (already approved by ANSI, IEEE, & BSR),
>the Pascal committee has added exponentiation to the language.  As a matter
>of fact, we added TWO operators.  We added "**" for raising things to
>real powers and "POW" for raising things to integer powers.

   Does the standard say anything about how they are implemented?
   I understand that Wirth decided to omit exponentiation from his
   original version of the language in part because implementations
   of the operator tended to be unsatisfactory.

   By the way, was anybody else taught that the number on top is 
   the "exponent", and that the whole thing, base and exponent together,
   is the "power"?  Confuses me sometimes.


   Alastair Milne

reagan@hiyall.enet.dec.com (John R. Reagan) (08/13/90)

In article <26C3B78F.8576@ics.uci.edu>, milne@ics.uci.edu (Alastair Milne) writes...
> 
>   Does the standard say anything about how they are implemented?
>   I understand that Wirth decided to omit exponentiation from his
>   original version of the language in part because implementations
>   of the operator tended to be unsatisfactory.

The following is from the standard:

	A factor of the form x**y shall be an error if x is zero and
	y is less than or equal to zero.

	A factor of the form x**y, where x is of integer-type or
	real-type, shall be an error if x is negative; otherwise,
	the value of x**y shall be zero if x is zero, else 1.0 if
	y is zero, else an approximation to (though not necessarily
	calculated by) exp(y*ln(x)).

	A factor of the form x pow y shall be an error if x is zero
	and y is less than or equal to zero.

	The value of a factor of the form x pow y, where x is of
	integer-type, shall be zero if x is zero, else 1 if y
	is zero, else queal to x*(x pow (y-1)) if y is positive,
	else equal to (1 div x) pow (-y) if y is negative.

The quality is an implementation/vendor issue independent of the
definition of the language.  If your vendor provides a slow or
inaccurate exponentation, then complain to your vendor...

---
John Reagan
Digital Equipment Corporation
reagan@hiyall.dec.com
---