power of a number

Julien Bourdon bourdux at gmail.com
Mon May 23 13:17:30 UTC 2005


Hi !

If you want ABSOLUTELY a #power: or a #** method you should alias #raisedTo:

Number>>power: aNumber
   ^ (self raisedTo:aNumber)

The #raisedTo: method is more elaborated than a mere loop cause you
can reduce the complexity of a power calculus with logarithms. You
should really look at the #raisedTo: method...

On 5/23/05, Cornelius Huber <corny at impara.de> wrote:
> Guenther Schmidt wrote:
> 
> > Hi,
> >
> > I can't figure this out by myself.
> >
> > I want to do an arithmetic expression like:
> >
> > 2 power 3, ie. (2 * 2 * 2).
> >
> > What's the right syntax in Squeak Smalltalk?
> >
> > Günther
> >
> my suggestion would be to implement a method called power: in the
> category Number
> 
> power: aNumber
> 
>     | result |
>     aNumber timesRepeat: [result := result * self].
>     ^result.
> 
> 
> Of Course in the case of power, there is already a method implemented
> called 'raisedTo:'
> Corny
> 
> 


-- 
    _  _               Julien Bourdon
   (o)(o)--.           http://www.purl.org/net/JulienBourdon
    \../   (  )          self isAddictedTo:(#Squeak&#linux)
   m\/m--m'`--.    ^true



More information about the Squeak-dev mailing list