[Pharo-project] Surprising binary messages!
5 is the surprising answer of the expression: 6 + 4 / 2 It should be 8 But, I got 5 in both 1.1 and 1.2. Noury
There is no such priority in smalltalk. + / - * are just normal binary messages - they take precedence over keyword messages and are second in line after unary ones. And everything is executed left to right so 5 is an expected answer here. Regards, Stanislav Paskalev On Tue, Jan 18, 2011 at 1:29 PM, Noury Bouraqadi <bouraqadi@gmail.com> wrote:
5 is the surprising answer of the expression: 6 + 4 / 2
It should be 8 But, I got 5 in both 1.1 and 1.2.
Noury
Why should it be 8? #+ and #/ have the same priority and they will be evaluated from left to right. Cheers, Doru On 18 Jan 2011, at 12:29, Noury Bouraqadi wrote:
5 is the surprising answer of the expression: 6 + 4 / 2
It should be 8 But, I got 5 in both 1.1 and 1.2.
Noury
-- www.tudorgirba.com "The coherence of a trip is given by the clearness of the goal."
On Tue, Jan 18, 2011 at 12:29 PM, Noury Bouraqadi <bouraqadi@gmail.com> wrote:
5 is the surprising answer of the expression: 6 + 4 / 2
It should be 8 But, I got 5 in both 1.1 and 1.2.
Noury
Hi, I'm just starting to learn smalltalk (thanks to PbE), but I'm pretty sure I know why this happens :-) Smalltalk has no knowledge of mathematics order, just operation order. In this case + and / are both binary operators so the operations are evaluated with the usual order, from left to right: (6 + 4) / 2 The answer 5 is correct. Regards Pietro
I'm just starting to learn smalltalk (thanks to PbE), but I'm pretty sure I know why this happens :-)
Excellent! I like that our energy helps people.
Smalltalk has no knowledge of mathematics order, just operation order. In this case + and / are both binary operators so the operations are evaluated with the usual order, from left to right:
(6 + 4) / 2
The answer 5 is correct.
Regards
Pietro
On Tue, Jan 18, 2011 at 12:40 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote:
I'm just starting to learn smalltalk (thanks to PbE), but I'm pretty sure I know why this happens :-)
Excellent! I like that our energy helps people.
It really does! Thanks to all the Pharo/Squeak community for the great effort. -- Pietro
You're right. It looks like I need to sleep more :-D Noury On 18 janv. 2011, at 12:38, Stéphane Ducasse wrote:
but this is normal. It was always like that. No operator precedence just messages
binary are evaluated from right to left 6 + 4 10 / 2 5
5 is the surprising answer of the expression: 6 + 4 / 2
It should be 8 But, I got 5 in both 1.1 and 1.2.
Noury
On 18 January 2011 12:29, Noury Bouraqadi <bouraqadi@gmail.com> wrote:
5 is the surprising answer of the expression: 6 + 4 / 2
It should be 8 But, I got 5 in both 1.1 and 1.2.
this is first thing all smalltalkers learn: smalltalk syntax does not using math operators precedence rules. because #+ and #/ is just selectors and you can put any behavior to it not nearly related to math.
Noury
-- Best regards, Igor Stasenko AKA sig.
6 + (4/2) will do what you want, for the reasons already well covered. As an aside, have you ever really looked at the way mathematics is typeset in books? Some of it does not bear close examination from a precedence/correctness perspective - missing parentheses, etc. The problem was that I was trying to teach students that many of their mistakes would disappear if they could reliably read what they just wrote. ________________________________________ From: pharo-project-bounces@lists.gforge.inria.fr [pharo-project-bounces@lists.gforge.inria.fr] On Behalf Of Noury Bouraqadi [bouraqadi@gmail.com] Sent: Tuesday, January 18, 2011 6:29 AM To: Pharo-project@lists.gforge.inria.fr Subject: [Pharo-project] Surprising binary messages! 5 is the surprising answer of the expression: 6 + 4 / 2 It should be 8 But, I got 5 in both 1.1 and 1.2. Noury
participants (7)
-
Igor Stasenko -
Noury Bouraqadi -
Pietro Maggi -
Schwab,Wilhelm K -
Stanislav Paskalev -
Stéphane Ducasse -
Tudor Girba