Does +, -, *, / have more precedence over power?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Does +, -, *, / have more precedence over power?

RedTigerFish
I understand in Smalltalk numerical calculation, if without round brackets,
everything starts being calculated from left to right. Nothing follows the
rule of multiplication and division having more precedence over addition and
subtraction.

Like the following codes

3 + 3 * 2
The print output is 12 while in mathematics we get 9

But when I started to try power calculation, like

91 raisedTo: 3 + 1.
I thought the answer should be 753572

What I actual get is 68574964

Why's that?

Is it because that +, -, *, / have more precedence over power ?



-----
Dig, dig where you are,
Down below's well.
Let those that walk in darkness shout,
Down below's hell.
--
Sent from: http://forum.world.st/Squeak-Beginners-f107673.html
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Dig, dig where you are,
Down below's well.
Let those that walk in darkness shout,
Down below's hell.
Reply | Threaded
Open this post in threaded view
|

Re: Does +, -, *, / have more precedence over power?

Andreas Wacknitz
Am 11.11.17 um 23:12 schrieb RedTigerFish:

> I understand in Smalltalk numerical calculation, if without round brackets,
> everything starts being calculated from left to right. Nothing follows the
> rule of multiplication and division having more precedence over addition and
> subtraction.
>
> Like the following codes
>
> 3 + 3 * 2
> The print output is 12 while in mathematics we get 9
>
> But when I started to try power calculation, like
>
> 91 raisedTo: 3 + 1.
> I thought the answer should be 753572
>
> What I actual get is 68574964
>
> Why's that?
>
> Is it because that +, -, *, / have more precedence over power ?
No. Smalltalk has only message sends. And these message sends have a
precedence (from higest to lowest):
     Unary messages        (example: 100 factorial)
     Binary messages        (example: 2*3)
     Keyword messages    (91 raisedTo: 4)

In  "91 raisedTo: 3+1" you have a keyword message (raisedTo:) and a
binary message (+).
The binary message will be evaluated first and then the keyword message.

Regards
Andreas

>
>
>
> -----
> Dig, dig where you are,
> Down below's well.
> Let those that walk in darkness shout,
> Down below's hell.
> --
> Sent from: http://forum.world.st/Squeak-Beginners-f107673.html
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Does +, -, *, / have more precedence over power?

Jecel Assumpcao Jr
In reply to this post by RedTigerFish
RedTigerFish wrote on Sat, 11 Nov 2017 15:12:06 -0700 (MST)
> I understand in Smalltalk numerical calculation, if without round brackets,
> everything starts being calculated from left to right. Nothing follows the
> rule of multiplication and division having more precedence over addition and
> subtraction.
>
> Like the following codes
>
> 3 + 3 * 2
> The print output is 12 while in mathematics we get 9

You are right. See C for an example of how complicated things can get if
you want to have a bunch of precendence rules. And C has a fixed set of
such operators while in Smalltalk we can define a bunch of new operators
(binary selectors - see below) and then what should the precendence be
compared to the existing ones?

So the decision was made to go for simplicity even though that
contradicts the goal of not surprising beginners if possible.

> But when I started to try power calculation, like
>
> 91 raisedTo: 3 + 1.
> I thought the answer should be 753572
>
> What I actual get is 68574964
>
> Why's that?
>
> Is it because that +, -, *, / have more precedence over power ?

Yes, but the rules are simple and general which should make them easy to
learn.

Smalltalk messages have three different forms:

- unary messages are a simple alphanumerical name which follows the
expression defining the object which will receive the message:

1.1 sin

4 factorial

- binary messages have names composed entirely of "graphics characters"
and the follow the expression defining the receiver and come before the
expression defining the single argument:

3 + 4

7 <= 9

Unary selectors have a higher precedence than binary ones, so

3 + 4 factorial

is the same thing as

3 + (4 factorial)

and not

(3 + 4) factorial

- keyword messages have names which are one or more alphanumerical
words, each followed by a colon character. The first word comes after
the receiver and each colon is followed by an argument:

91 raisedTo: 3

4 between: 1 and: 7

Keyword selectors have the lowest precedence of all, so

91 raisedTo: 3 + 1

is the same as

91 raisedTo: (3 + 1)

and not

(91 raisedTo: 3) + 1

So there are only three simple rules to remember, but even so you will
often see code which uses more parenthesis (curved brackets) than needed
just to make sure.

One ugly part of Smalltalk syntax is that though "-" is a binary message
the same character is used for negative literal numbers.

2-1

returns 1.

2 -1

also returns 1.

2 * -1

returns -2,

2 *-1

is a syntax error with a dialog asking you to fix it.

2*-1

same thing.

With all that, we can mix all three kinds of selectors:

91 raisedTo: 3 + -1 abs

also returns 68574961.

-- Jecel
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Does +, -, *, / have more precedence over power?

Yoshiki Ohshima-3
In reply to this post by RedTigerFish


On Sat, Nov 11, 2017 at 2:12 PM, RedTigerFish <[hidden email]> wrote:

Is it because that +, -, *, / have more precedence over power ?

Others already gave good answers but I want to point out just one thing: the precedence rule is purely syntactical (i.e. computer decides it from only the text).  What those methods actually do is irrelevant.

For example, you can define a method called **, such as:

--------------
** aNumber
   ^ self raisedTo: aNumber.
-------------
and then, you can write:

2 ** 3 + 1

to get 9.

--
-- Yoshiki


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners