Surprising binary messages!

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

Surprising binary messages!

Noury Bouraqadi-2
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


Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

solar_sea
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 <[hidden email]> 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
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

Tudor Girba
In reply to this post by Noury Bouraqadi-2
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."





Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

Pietro Maggi
In reply to this post by Noury Bouraqadi-2
On Tue, Jan 18, 2011 at 12:29 PM, Noury Bouraqadi <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

Stéphane Ducasse
In reply to this post by Noury Bouraqadi-2
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
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

Igor Stasenko
In reply to this post by Noury Bouraqadi-2
On 18 January 2011 12:29, Noury Bouraqadi <[hidden email]> 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.

Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

Stéphane Ducasse
In reply to this post by Pietro Maggi
>
> 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
>


Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

Pietro Maggi
On Tue, Jan 18, 2011 at 12:40 PM, Stéphane Ducasse
<[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

Schwab,Wilhelm K
In reply to this post by Noury Bouraqadi-2
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: [hidden email] [[hidden email]] On Behalf Of Noury Bouraqadi [[hidden email]]
Sent: Tuesday, January 18, 2011 6:29 AM
To: [hidden email]
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



Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

Noury Bouraqadi-2
In reply to this post by Stéphane Ducasse
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
>>
>>
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Surprising binary messages!

Stéphane Ducasse
Yes man.

Stef

> You're right. It looks like I need to sleep more :-D
>
> Noury