Search the web
Sign In
New User? Sign Up
squeak
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Re: power of a number   Topic List   < Prev Topic  |  Next Topic >
Summarize Messages Sort by Date  
#94852 From: Cornelius Huber <corny@...>
Date: Mon May 23, 2005 1:05 pm
Subject: Re: power of a number
corny@...
Send Email Send Email
 
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




#94853 From: Julien Bourdon <bourdux@...>
Date: Mon May 23, 2005 1:17 pm
Subject: Re: power of a number
bourdux@...
Send Email Send Email
 
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@...> 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




#94857 From: Lex Spoon <lex@...>
Date: Mon May 23, 2005 2:39 pm
Subject: Re: power of a number
lex@...
Send Email Send Email
 
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.

-Lex




 
Advanced
Add to My Yahoo!      XML What's This?

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help