Most compilers (and ISO 10206 Extended Pascal) use "**" as the
expoentiation operator.
--
John Reagan
HP Pascal/{A|I}MACRO for OpenVMS Project Leader
Hewlett-Packard Company
There is no standard power function. You have to make it yourself.
function power (base,exponent:real):real;
begin
if base<1e-30
then power:=0
else power:= exp(exponent*ln(base))
end;
also handy for graphs is the 10 based logarithm
function logx(x:real):real;
begin
if x< 1e-30 then logx:=-30
else logx:=ln(x)/ln(10);
end;
--
Femme
--
Femme Verbeek
Hmmm... it depends on the compiler. FPC (and Delphi, I believe) has
"Power" function for example.
Cheers
Mike
uses math;
begin
writeln(2**5);
end.
result: 32
Note that the uses math is required.
©erif a écrit:
> ok so i have to make a graph makeing program in pascal and i don't know how
> to make somethin like a^x, where a would be a constant.
> i know that in c the command is pow(a,x), but how do i make it in pascal?
If you use gpc (GNU pascal) you can write either
a ** b (b real)
a pow i (i integer)
both are ISO 10206 Extended Pascal
Maurice
BTW in old ages, when I used BP, I wrote my own function
for real and integer power:
Function PowerR(x,n:extended): extended;
begin
PowerR:=exp(n*ln(x));
end;
Function PowerI(x:extended; n:longint): extended;
var p,xi:extended;
begin
if n<0 then begin
x:=1/x; n:=-n
end;
xi:=x; p:=1;
while n>0 do begin
if odd(n) then p:=p*xi;
xi:=sqr(xi);
n:=n div 2;
end;
PowerI:=p;
end;
--
Maurice Lombardi
Laboratoire de Spectrometrie Physique,
Universite Joseph Fourier de Grenoble, BP87
38402 Saint Martin d'Heres Cedex FRANCE
Tel: 33 (0)4 76 51 47 51
Fax: 33 (0)4 76 63 54 95
mailto:Maurice....@ujf-grenoble.fr
Look up exp(x). Spell Pascal capitalized please.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
F Verbeek wrote:
> In the obscure news:d2rid6$4qa$1...@ss405.t-com.hr,
> Someone calling himself ©erif
Note he said standard. Use of ** and/or pow requires ISO10206
extended Pascal. However exp() is always available, and thus
maximally portable.
Also non standard TP/BP never had a power function.
--
Femme Verbeek
Although the FAQ is for Turbo Pascal a standard Pascal answer can be
found in
169462 Oct 27 2004 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
tsfaqp.zip Common Turbo Pascal Questions and Timo's answers
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
If you are using the Pascal Macro Compiler then you can create Power
functions that generate inline code, and therefore avoid the overhead
of a function call and/or a For loop. I have created two such macro
functions, Power for arbitrary powers, and IPower for integer powers.
For example IPower(x,3) would generate
(x)*(x)*(x)
The two macros are available for downloading at
http://www.pascalmacro.com (If that link does not work, try
http://www.contestcen.com/pasmisc.htm )
Elsewhere in this thread I have seen code of the form
if x < 1E-30
then Power := 0
else {calculate the power}
I have looked into that a little. I found with Borland Pascal that you
could obtain valid values for exponents from 0.2 up to 1.2 but for 1.4
you got an overflow. However, for TMT Pascal I found that you got
valid values for the power with exponents from 0.2 up to 3.0. So I
suspect that test x<1E-30 is a left-over from some long-ago Pascal
compiler that may not have given accurate values for the logarithms of
numbers less than 1E-30, but is not required with newer compilers.
> I have looked into that a little. I found with Borland Pascal that you
> could obtain valid values for exponents from 0.2 up to 1.2 but for 1.4
> you got an overflow. However, for TMT Pascal I found that you got
> valid values for the power with exponents from 0.2 up to 3.0. So I
> suspect that test x<1E-30 is a left-over from some long-ago Pascal
> compiler that may not have given accurate values for the logarithms of
> numbers less than 1E-30, but is not required with newer compilers.
TP doesn't use the FPU by default, since that device was not yet common in
its era. Compile with {$N+} and it will use normal 8 byte IEEE floats.
> If you are using the Pascal Macro Compiler then you can create Power
> functions that generate inline code, and therefore avoid the overhead
> of a function call and/or a For loop.
Or you can use a compiler that would automatically inline routines for
you.
Also, given that system supplied routine will have to do some sort of
EXP and LN function in any case, the overhead of the call/return is
probably noise.
No, it will be able to use them.
"Real" will still be the non IEEE 6 byte format
Single Double and Extended will use the co-proc.
--
Femme Verbeek