Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pow function in pasal

480 views
Skip to first unread message

委rif

unread,
Apr 4, 2005, 10:19:48 AM4/4/05
to
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?


John Reagan

unread,
Apr 4, 2005, 11:23:34 AM4/4/05
to

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

Serif

unread,
Apr 4, 2005, 11:25:40 AM4/4/05
to
thanks a lot
"John Reagan" <john....@hp.com> wrote in message
news:WZc4e.2872$Ib7....@news.cpqcorp.net...

F Verbeek

unread,
Apr 4, 2005, 11:43:57 AM4/4/05
to
In the obscure news:d2rid6$4qa$1...@ss405.t-com.hr,
Someone calling himself 委rif
suspiciously hiding as <nse...@gmail.com> uttered:

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

Michał Woźniak

unread,
Apr 4, 2005, 11:54:20 AM4/4/05
to
> <snip>

> There is no standard power function. You have to make it yourself.

Hmmm... it depends on the compiler. FPC (and Delphi, I believe) has
"Power" function for example.

Cheers
Mike

Marco van de Voort

unread,
Apr 4, 2005, 1:06:05 PM4/4/05
to

uses math;

begin
writeln(2**5);
end.

result: 32

Note that the uses math is required.

Maurice.Lombardi

unread,
Apr 4, 2005, 1:19:08 PM4/4/05
to

©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

CBFalconer

unread,
Apr 4, 2005, 1:54:34 PM4/4/05
to

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

Scott Moore

unread,
Apr 4, 2005, 1:50:24 PM4/4/05
to
Nicklaus Wirth specifically mentioned the power function in
the Users Manual and Report, as being an example of an unecessary
complication of the language that could be accomplished with
the lower level functions (exp, ln) in "do it yourself" fashion.
He also gives the receipe shown below.

F Verbeek wrote:
> In the obscure news:d2rid6$4qa$1...@ss405.t-com.hr,

> Someone calling himself ©erif

CBFalconer

unread,
Apr 4, 2005, 2:49:06 PM4/4/05
to
Michał Woźniak wrote:
>
>> <snip>
>> There is no standard power function. You have to make it yourself.
^^^^^^^^

>
> Hmmm... it depends on the compiler. FPC (and Delphi, I believe) has
> "Power" function for example.

Note he said standard. Use of ** and/or pow requires ISO10206
extended Pascal. However exp() is always available, and thus
maximally portable.

F Verbeek

unread,
Apr 5, 2005, 6:14:43 PM4/5/05
to
In the obscure news:425186B9...@yahoo.com,
Someone calling himself CBFalconer
suspiciously hiding as <cbfal...@yahoo.com> uttered:

> Michał Woźniak wrote:
>>
>>> <snip>
>>> There is no standard power function. You have to make it yourself.
> ^^^^^^^^
>>
>> Hmmm... it depends on the compiler. FPC (and Delphi, I believe) has
>> "Power" function for example.
>
> 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


Timo Salmi

unread,
Apr 11, 2005, 3:29:58 AM4/11/05
to

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

conte...@aol.com

unread,
Apr 11, 2005, 6:01:44 PM4/11/05
to


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.

Marco van de Voort

unread,
Apr 12, 2005, 2:34:26 AM4/12/05
to
On 2005-04-11, conte...@aol.com <conte...@aol.com> wrote:

> 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.

John Reagan

unread,
Apr 12, 2005, 12:28:59 PM4/12/05
to
conte...@aol.com wrote:

> 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.

F Verbeek

unread,
Apr 12, 2005, 7:07:46 PM4/12/05
to
In the obscure news:slrnd5mqvj....@turtle.stack.nl,
Someone calling himself Marco van de Voort
suspiciously hiding as <mar...@stack.nl> uttered:

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


0 new messages