Board index » delphi » How to do powers in turbo pascal

How to do powers in turbo pascal

Hello,

i am lloking for Pascal code which can cuaculate:

   4.1
3.4     or something like that.

I now there is a formula to do that but I can't rember it.

If you have this formula, do you want to mail it to me:
nowayj...@universal.nl

I would apreciate your help.
--
-------------------------------------------------------------------
  Derick Rethans
  alias Dimitri
  alias rojwI'
-------------------------------------------------------------------
       http://www.universal.nl/users/drethans/drhome.htm
-------------------------------------------------------------------
                      Live long and prosper.
                     It is a good day to die.
-------------------------------------------------------------------

 

Re:How to do powers in turbo pascal


In article <342C02E0.696BB...@universal.nl>,

Quote
Drick Rethans <nowayj...@universal.nl> wrote:

:i am lloking for Pascal code which can cuaculate:
:
:   4.1
:3.4     or something like that.
:
:I now there is a formula to do that but I can't rember it.

Both the simple and the more generic formula for powers is one of
the main items of

 117975 Jul 12 1997 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   Co-moderator of news:comp.archives.msdos.announce
Moderating at 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

Spam foiling in effect.  My email filter autoresponder will return a
required email password to users not yet in the privileges database.

Re:How to do powers in turbo pascal


Quote
"Drick Rethans" <nowayj...@universal.nl> wrote:
>Hello,
>i am lloking for Pascal code which can cuaculate:
>   4.1
>3.4     or something like that.
>I now there is a formula to do that but I can't rember it.
>If you have this formula, do you want to mail it to me:
>nowayj...@universal.nl
>I would apreciate your help.
>--
>-------------------------------------------------------------------
>  Derick Rethans
>  alias Dimitri
>  alias rojwI'
>-------------------------------------------------------------------
>       http://www.universal.nl/users/drethans/drhome.htm
>-------------------------------------------------------------------
>                      Live long and prosper.
>                     It is a good day to die.
>-------------------------------------------------------------------

Program Powers;
{This will raise POSITIVE numbers to either positive or negative
powers. The numbers and powers may be entered as integers or reals
(including decimal fractions) although the integers will be assumed to
be real numbers. For example, 2 ^ -0.5 = 0.707... which is the
reciprocal of the sqaure root of 2 or the familiar sine of 45 degrees.
Turbo v.6.0   <clifp...@airmail.net>  4/24/96, rev 8/9/97}

Label finis;

VAR
Answer, x, p:real;

Function Raise(t:real; n:real): real;
VAR
NegExp:Boolean;
Ans:real;

Begin
     If t < 0 then Ans := 0  (* not needed if controlled externally *)
     Else
     Begin
           If n = 0 then Ans := 1
           Else
           Begin
                NegExp := n < 0;
                If NegExp then n := Abs(n);
           End;
           If n <> 0 then Ans := exp(n * ln(abs(t)));
           If NegExp then Ans := 1 / Ans;
     End;
     Raise := Ans;
End;

Begin (* main program *)
      Writeln; Writeln; Writeln;
      Writeln('Three Examples:');
      Answer := Raise(4, 3);
      Writeln('4^3 = ', Answer:1:4);

      Answer := Raise(8, 0.333333333);
      Writeln('(8)^0.3333 = ', Answer:1:4);

      Answer := Raise(2, -0.5);
      Writeln('(2) ^ -0.5 = ', Answer:1:8);

      Repeat
            Writeln;
            Write('Enter a POSITIVE real number (0 to quit): ');

            Readln(x);
            If x = 0 then goto finis;
            Write('Enter a positive or negative real power: ');
            Readln(p);

            Answer := Raise(x, p);
            If Answer = 0 then Writeln(Chr(7))
            Else Writeln(x:1:8, ' ^ ', p:1:8, ' = ', Answer:1:8);
finis:
      Until x = 0;
End.

Re:How to do powers in turbo pascal


On Fri, 26 Sep 1997 20:45:52 +0200, "Drick Rethans"

Quote
<nowayj...@universal.nl> wrote:
>Hello,

>i am lloking for Pascal code which can cuaculate:

>   4.1
>3.4     or something like that.

>I now there is a formula to do that but I can't rember it.

>If you have this formula, do you want to mail it to me:
>nowayj...@universal.nl

>I would apreciate your help.

function power(x,e: real): real;
begin if x=0.0 then power:= 0.0 else power:= exp(e*ln(x)) end;

Bob Ferguson

-----------------------------------------
J.R. Ferguson, Amsterdam, The Netherlands
e-mail: j.r.fergu...@iname.com
web: www.xs4all.nl/~ferguson
Freeware for DOS and Windows

Re:How to do powers in turbo pascal


In article <C4EED6AB3D2BEDDC.16A02DECF4A78810.6AFC66265A105...@library-proxy.airnews.net>,

Quote
CLIF PENN <clifp...@airmail.net> wrote:

...

Quote
>Program Powers;
>{This will raise POSITIVE numbers to either positive or negative
>powers. The numbers and powers may be entered as integers or reals
>(including decimal fractions) although the integers will be assumed to
>be real numbers. For example, 2 ^ -0.5 = 0.707... which is the
>reciprocal of the sqaure root of 2 or the familiar sine of 45 degrees.
>Turbo v.6.0   <clifp...@airmail.net>  4/24/96, rev 8/9/97}

>Label finis;

>VAR
>Answer, x, p:real;

>Function Raise(t:real; n:real): real;
>VAR
>NegExp:Boolean;
>Ans:real;

>Begin
>     If t < 0 then Ans := 0  (* not needed if controlled externally *)
>     Else
>     Begin
>           If n = 0 then Ans := 1
>           Else
>           Begin
>                NegExp := n < 0;
>                If NegExp then n := Abs(n);
>           End;
>           If n <> 0 then Ans := exp(n * ln(abs(t)));
>           If NegExp then Ans := 1 / Ans;
>     End;
>     Raise := Ans;
>End;

There is no need to make things so complicated. One can just calculate
exp(n*ln(t)). That handles negative and zero exponents automatically.
Taking absolute value of the base is pointless as there is a check for
it before. Also in any case it is dangerous as it could lead to
incorrect results.

What I am saying is that it following thought of pattern is dangerous:

"Lets calculate logarithm ... The function is ln(x) .. but that fails if
x is negative. What shall I do? Hey, I'll use abs: ln(abs(x))." Instead
one should think how to avoid the case where x is negative and not kill
the messenger. In above there simply is no reason whatsoever to use abs.

In general it is good to avoid incorrect results and give run time
errors instead. Calling a function with improper values can be a symptom
of a major problem. (I know there are exception, like in plotting a
curve single incorrect point may be better than runtime error). No
other TP function has error recovery built in, so why should power have?
In anyway if one does it one should signal the error out and not just
give made up value.

I remember producing similar reply when the above solution was last
posted.

Osmo

Re:How to do powers in turbo pascal


In article <342f5a6e.6529...@news.xs4all.nl> of Sat, 27 Sep 1997
19:15:09 in comp.lang.pascal.borland, "J.R. Ferguson"

Quote
<j.r.fergu...@iname.com> wrote:
>function power(x,e: real): real;
>begin if x=0.0 then power:= 0.0 else power:= exp(e*ln(x)) end;

 function power(x,e: real): real;
 begin if e=0.0 then power:= 1.0 else
       if x=0.0 then power:= 0.0 else power:= exp(e*ln(x)) end;

(I think).

NOTE TO ALL : My news service is, at present, failing to send a small
percentage of posts from users to world, from world to users, and from
users to users - or so it seems to me.  If you don't see this, YHBW.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ - FAQqish topics, acronyms and links.
                                                            ^^^^^^^^
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)

Re:How to do powers in turbo pascal


Mr. Timo Salmi you're great !!!

eh eh
just a thought !

Andrea

Re:How to do powers in turbo pascal


Un giorno "Drick Rethans" <nowayj...@universal.nl> ispirato dal suo
subconscio ha scritto :

Quote
>Hello,

>i am lloking for Pascal code which can cuaculate:

>   4.1
>3.4     or something like that.

exp(4.1*ln(3.4))

If you need explanations let me know !
;o)
Andrea

Quote

>I now there is a formula to do that but I can't rember it.

>If you have this formula, do you want to mail it to me:
>nowayj...@universal.nl

>I would apreciate your help.
>--
>-------------------------------------------------------------------
>  Derick Rethans
>  alias Dimitri
>  alias rojwI'
>-------------------------------------------------------------------
>       http://www.universal.nl/users/drethans/drhome.htm
>-------------------------------------------------------------------
>                      Live long and prosper.
>                     It is a good day to die.
>-------------------------------------------------------------------

Re:How to do powers in turbo pascal


Un giorno "Drick Rethans" <nowayj...@universal.nl> ispirato dal suo
subconscio ha scritto :

Quote
>Hello,

>i am lloking for Pascal code which can cuaculate:

>   4.1
>3.4     or something like that.

exp(4.1*ln(3.4))

If you need explanations let me know !
;o)
Andrea

Quote

>I now there is a formula to do that but I can't rember it.

>If you have this formula, do you want to mail it to me:
>nowayj...@universal.nl

>I would apreciate your help.
>--
>-------------------------------------------------------------------
>  Derick Rethans
>  alias Dimitri
>  alias rojwI'
>-------------------------------------------------------------------
>       http://www.universal.nl/users/drethans/drhome.htm
>-------------------------------------------------------------------
>                      Live long and prosper.
>                     It is a good day to die.
>-------------------------------------------------------------------

Other Threads