Board index » delphi » Raising a number to a power

Raising a number to a power

I've started venturing into getting bits from bytes, which I discovered
from programming on my Commodore 64 all those years back :-) that this
involves using powers to calculate binary positions, ie. 1 2 4 16 32 64
128, which is 2 to the power of the bit number.

But as I found out yesterday there is apparently no simple way of
getting powers, whereas on the C64 it was number^exponential. Of course
in Pascal, ^ is to do with lists and pointers.

Other than making a lookup table or a series of constants, is there a
simple way of doing this? I tried using loops to keep multiplying the
number by itself, but it was a bit cumbersome.

--
+---------------+---------------------------+
| Iain Jones    | xh0u9...@NOSPAM.liv.ac.uk |
|               | (Remove the NOSPAM.)      |
+---------------+---------------------------+

Sent via Deja.com http://www.deja.com/
Before you buy.

 

Re:Raising a number to a power


In article <81e1qj$ok...@nnrp1.deja.com>,
  Iain Jones <i_p_jo...@my-deja.com> wrote:

Quote
> I've started venturing into getting bits from bytes, which I
discovered
> from programming on my Commodore 64 all those years back :-) that this
> involves using powers to calculate binary positions, ie. 1 2 4 16 32
64
> 128, which is 2 to the power of the bit number.

> But as I found out yesterday there is apparently no simple way of
> getting powers, whereas on the C64 it was number^exponential. Of
course
> in Pascal, ^ is to do with lists and pointers.

> Other than making a lookup table or a series of constants, is there a
> simple way of doing this? I tried using loops to keep multiplying the
> number by itself, but it was a bit cumbersome.

> --
> +---------------+---------------------------+
> | Iain Jones    | xh0u9...@NOSPAM.liv.ac.uk |
> |               | (Remove the NOSPAM.)      |
> +---------------+---------------------------+

> Sent via Deja.com http://www.deja.com/
> Before you buy.

If memory serves, it was the exp() function that was used (It's been a
while...)

  exp( x ) returned e**x (or e^x in C64 notation).  To get y**x, you had
to do something like

  y**x = exp( x * ln(y) );

  So, 2^4 would be   exp( 4 * ln( 2 ) );

Sent via Deja.com http://www.deja.com/
Before you buy.

Re:Raising a number to a power


Hi Iain

You'll have to dig into your MAT101 books. True, Pascal does not support
xxEyy, so you have to change the equation to LN (LOG is not supported
either).

20E2 becomes 2*LN(20) etc.

Have fun
Marius

Quote
Iain Jones wrote:
> I've started venturing into getting bits from bytes, which I discovered
> from programming on my Commodore 64 all those years back :-) that this
> involves using powers to calculate binary positions, ie. 1 2 4 16 32 64
> 128, which is 2 to the power of the bit number.

> But as I found out yesterday there is apparently no simple way of
> getting powers, whereas on the C64 it was number^exponential. Of course
> in Pascal, ^ is to do with lists and pointers.

> Other than making a lookup table or a series of constants, is there a
> simple way of doing this? I tried using loops to keep multiplying the
> number by itself, but it was a bit cumbersome.

> --
> +---------------+---------------------------+
> | Iain Jones    | xh0u9...@NOSPAM.liv.ac.uk |
> |               | (Remove the NOSPAM.)      |
> +---------------+---------------------------+

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Re:Raising a number to a power


How about using a mask with AND or OR

Re:Raising a number to a power


In article <383A9D9A.F0724...@mailbox.co.za>,

Quote
Marius  <mar...@mailbox.co.za> wrote:
>Hi Iain

>You'll have to dig into your MAT101 books. True, Pascal does not support
>xxEyy, so you have to change the equation to LN (LOG is not supported
>either).

>20E2 becomes 2*LN(20) etc.

Just what nonsense is that? 20E2 is 2000. 2*ln(20) is 5.99146...

Osmo

Re:Raising a number to a power


In article <81e1qj$ok...@nnrp1.deja.com>,
Iain Jones  <i_p_jo...@my-deja.com> wrote:

Quote
>I've started venturing into getting bits from bytes, which I discovered
>from programming on my Commodore 64 all those years back :-) that this
>involves using powers to calculate binary positions, ie. 1 2 4 16 32 64
>128, which is 2 to the power of the bit number.

You can calculate 2^n with 1 shl n.

Osmo

Re:Raising a number to a power


In article <81e5io$rc...@nnrp1.deja.com>,
Thomas Cloutier  <clouti...@my-deja.com> wrote:

Quote
>In article <81e1qj$ok...@nnrp1.deja.com>,
>  Iain Jones <i_p_jo...@my-deja.com> wrote:
>> I've started venturing into getting bits from bytes, which I
>discovered
>> from programming on my Commodore 64 all those years back :-) that this
>> involves using powers to calculate binary positions, ie. 1 2 4 16 32
>64
>> 128, which is 2 to the power of the bit number.

...

>If memory serves, it was the exp() function that was used (It's been a
>while...)

>  exp( x ) returned e**x (or e^x in C64 notation).  To get y**x, you had
>to do something like

>  y**x = exp( x * ln(y) );

>  So, 2^4 would be   exp( 4 * ln( 2 ) );

I just wonder did you read the message or did you just think with the
spinal cord: power => exp?

Now would you do that it you have to for example turn 1000x1000 bitmap
90 degrees?

Osmo

Re:Raising a number to a power


In article
<0E16861EE7BCD111BE9400805FE6841F0CB7C...@c1s5x001.cor.srvfarm.origin-it
.com>,
  "nl11717" <Ivar.rient...@nl.origin-it.com> wrote:

Quote

> How about using a mask with AND or OR

En dat werkt bij Origin. Het is te hopen dat hun klanten dit niet lezen!

(Translation: He's working for a big Dutch software house, and hasn't
got a clue what he is talking about. I hope their customers don't read
this - actually, I hope they do, it will show them the quality of people
these bodyshops supply)

Robert
--
Robert AH Prins
prin...@willis.com

Sent via Deja.com http://www.deja.com/
Before you buy.

Re:Raising a number to a power


How about a loop?

Exponent := ?
X := 1;
FOR Power := 1 TO Exponent DO
X := X * 2;

On Tue, 23 Nov 1999 12:37:39 GMT, Iain Jones <i_p_jo...@my-deja.com>
wrote:

Quote
>I've started venturing into getting bits from bytes, which I discovered
>from programming on my Commodore 64 all those years back :-) that this
>involves using powers to calculate binary positions, ie. 1 2 4 16 32 64
>128, which is 2 to the power of the bit number.

>But as I found out yesterday there is apparently no simple way of
>getting powers, whereas on the C64 it was number^exponential. Of course
>in Pascal, ^ is to do with lists and pointers.

>Other than making a lookup table or a series of constants, is there a
>simple way of doing this? I tried using loops to keep multiplying the
>number by itself, but it was a bit cumbersome.

Re:Raising a number to a power


Quote
Iain Jones <i_p_jo...@my-deja.com> wrote:
>I've started venturing into getting bits from bytes, which I discovered
>from programming on my Commodore 64 all those years back :-) that this
>involves using powers to calculate binary positions, ie. 1 2 4 16 32 64
>128, which is 2 to the power of the bit number.

For just getting bits from bytes don't bother with a power
function.
Jud McCranie

Re:Raising a number to a power


Quote
Iain Jones <i_p_jo...@my-deja.com> wrote:

>But as I found out yesterday there is apparently no simple way of
>getting powers, whereas on the C64 it was number^exponential. Of course
>in Pascal, ^ is to do with lists and pointers.

>Other than making a lookup table or a series of constants, is there a
>simple way of doing this?

If you are raising to a small, fixed integer power just use
sqr(x), x*sqr(x), sqr(sqr(x)), etc.  You can write a routine to
raise to a variable integer power that uses fixed calculations
(like these) or loops for relatively small powers, and the
binary method for larger integer powers.  If the power is a
floating point number, use x^y = exp( y*ln(x)).

Jud McCranie

Re:Raising a number to a power


JRS:  In article <81e1qj$ok...@nnrp1.deja.com> of Tue, 23 Nov 1999
12:37:39 in news:comp.lang.pascal.borland, Iain Jones <i_p_jones@my-

Quote
deja.com> wrote:
>I've started venturing into getting bits from bytes, which I discovered
>from programming on my Commodore 64 all those years back :-) that this
>involves using powers to calculate binary positions, ie. 1 2 4 16 32 64
>128, which is 2 to the power of the bit number.

It does not necessarily involve such; and, if it does, *you* need not do
it explicitly.

Quote
>But as I found out yesterday there is apparently no simple way of
>getting powers, whereas on the C64 it was number^exponential. Of course
>in Pascal, ^ is to do with lists and pointers.

>Other than making a lookup table or a series of constants, is there a
>simple way of doing this? I tried using loops to keep multiplying the
>number by itself, but it was a bit cumbersome.

Powers routines can be found in many places, including TSFAQP#13 and my
pas-math.htm.  But for powers of two, a simple (1 shl J) will do it,
with care on variable lengths for J>7 and not exceeding (IIRC) J=31.

But there is a far nicer way of doing it, easy to read once learnt and
generating less code; study my pas-opts.htm#BT, "Bit Testing".

You end up with, for example,

        type bit = set of 0..7 ;
        ...
        if 4 in bit(YourByte) then ...
or
        type Fish = Cod, Dace, Ray, Sprat, Kipper, Shark, Eel, Plaice ;
        Fishes = set of Fish ;
        var MyCatch : Fishes ;
        ...
        if Kipper in MyCatch then LegPull ;

Bob Schor pointed the use of sets for bit-testing.

Timo: AFAICS, with ^F in MSIE to help, nowhere in your FAQ does "set of"
occur in Pascal (it's there in English); and I don't think "set" occurs
in any of the Pascal examples, though #136 is about sets.  Might it be
useful to have a question to which the use of explicit set types is the
answer?

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:Raising a number to a power


In article <81e1qj$ok...@nnrp1.deja.com>,
Iain Jones  <i_p_jo...@my-deja.com> wrote:
:But as I found out yesterday there is apparently no simple way of
:getting powers, whereas on the C64 it was number^exponential. Of course

 13) I need a power function but there is none in Turbo Pascal.

 164990 Apr 25 1999 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
 tsfaqp.zip Common Turbo Pascal Questions and Timo's answers, linked

Also, remotely related
 100) Could you please explain shl and shr operators to a beginner?
  60) How do I obtain a bit or bits from a byte, a word or a longint?

   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  procmail tips at  http://www.uwasa.fi/~ts/info/proctips.html

Re:Raising a number to a power


Quote
Iain Jones wrote in message <81e1qj$ok...@nnrp1.deja.com>...
>I've started venturing into getting bits from bytes, which I discovered
>from programming on my Commodore 64 all those years back :-) that this
>involves using powers to calculate binary positions, ie. 1 2 4 16 32 64
>128, which is 2 to the power of the bit number.

>But as I found out yesterday there is apparently no simple way of
>getting powers, whereas on the C64 it was number^exponential. Of course
>in Pascal, ^ is to do with lists and pointers.

>Other than making a lookup table or a series of constants, is there a
>simple way of doing this? I tried using loops to keep multiplying the
>number by itself, but it was a bit cumbersome.

For positive values of x and n, the following works fine and has
the virtue of simplicity.

{  raise x to the nth power }
Function Power( x, n : real ) : real;
begin
   Power := Exp( n * Ln( x ) )
end;

But a more general implementation (which I found in some magazine
about ten years ago but have lost the reference and therefore cannot give
proper credit) would be as follows:

{  raise x to the nth power }
function power( x, n : real ) : real;
   function f( base, exponent : real ) : real;
      begin f := exp( exponent * ln( base ) );
   end;
begin
  if x = 0.0
    then
      if n = 0.0
        then power := 1.0
        else
          if n < 0.0
            then runError( 207 )    { 0 ^ negative }
            else power := 0.0
    else
      if x > 0.0
        then power := f( x, n )
    else
      if frac( n ) = 0.0
        then
          if odd( trunc( n ) )
            then power := -f( -x, n )
            else power :=  f( -x, n )
        else runError( 207 )     { negative ^ noninteger }
end; { power }

Hope this helps.  (By the way, I have tested this only under
Turbo Pascal 3.0 and 5.5, but I see no reason why it wouldn't
work under later versions of Borland's compiler as well).

Stephen Mitchell
Alexandria, Va. (USA)

Re:Raising a number to a power


In article <81jrku$p9...@autumn.news.rcn.net>,

Quote
Stephen Mitchell <ssmi...@erols.com> wrote:
>Iain Jones wrote in message <81e1qj$ok...@nnrp1.deja.com>...
>>I've started venturing into getting bits from bytes, which I discovered
>>from programming on my Commodore 64 all those years back :-) that this
>>involves using powers to calculate binary positions, ie. 1 2 4 16 32 64
>>128, which is 2 to the power of the bit number.

>>But as I found out yesterday there is apparently no simple way of
>>getting powers, whereas on the C64 it was number^exponential. Of course
>>in Pascal, ^ is to do with lists and pointers.

>>Other than making a lookup table or a series of constants, is there a
>>simple way of doing this? I tried using loops to keep multiplying the
>>number by itself, but it was a bit cumbersome.

>For positive values of x and n, the following works fine and has
>the virtue of simplicity.

>{  raise x to the nth power }
>Function Power( x, n : real ) : real;
>begin
>   Power := Exp( n * Ln( x ) )
>end;

Another example of not reading the question. Please the correct answer
is "1 shl n".

Quote
>But a more general implementation (which I found in some magazine
>about ten years ago but have lost the reference and therefore cannot give
>proper credit) would be as follows:

>{  raise x to the nth power }
>function power( x, n : real ) : real;
>   function f( base, exponent : real ) : real;
>      begin f := exp( exponent * ln( base ) );
>   end;
>begin
>  if x = 0.0
>    then
>      if n = 0.0
>        then power := 1.0

0^0 is actually undefined.

Quote
>        else
>          if n < 0.0
>            then runError( 207 )    { 0 ^ negative }
>            else power := 0.0
>    else
>      if x > 0.0
>        then power := f( x, n )
>    else
>      if frac( n ) = 0.0
>        then
>          if odd( trunc( n ) )
>            then power := -f( -x, n )
>            else power :=  f( -x, n )
>        else runError( 207 )     { negative ^ noninteger }
>end; { power }

That is a nice routine, that is f you program in BASIC. In Pascal one
should view cases where the exponent is real and where it is integer as
separate ones and not try to mix them.

For integer exponents following recursive routine is pretty simple and
effective:

procedure power(x:real;n:word):real;
Begin
  if n=0 then power:=1
     else if odd(n) then power:=sqr(power(x,n div 2))*x
                    else power:=sqr(power(x,n div 2));
End;

Osmo

Go to page: [1] [2]

Other Threads