> 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
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@...> 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
>
>
On Mon, May 23, 2005 at 02:53:16PM +0200, Guenther Schmidt wrote:
> I want to do an arithmetic expression like:
>
> 2 power 3, ie. (2 * 2 * 2).
Others have answered the question. Just so you know, though, Ted Kaehler's
method finder is excellent for finding these answers. If you start up
Squeak, open a method finder (with the "open" menu), and type in:
2 . 3 . 8
Then you will find the existing method for this operation. This is
a fantastic tool for all the little basic questions that people have
when they are learning a new language.