Order of precedence

Just checking

if I have

a = a - b*c

b*c gets calculated first then that result is subtracted from a ?
In other words, parantheses, in Ruby, is not necessary to make that
calculation ?

TIA
Stuart

On Jun 30, 2006, at 12:07 PM, Dark A. wrote:

TIA
Stuart

irb says yes:

irb(main):001:0> 2 - 3 * 5
=> -13

-Mat

On 6/30/06, Dark A. [email protected] wrote:

Just checking

if I have

a = a - b*c

b*c gets calculated first then that result is subtracted from a ?
In other words, parantheses, in Ruby, is not necessary to make that
calculation ?

It’s best to check this sort of thing in irb.

-austin

If you have pickaxe2, look at page 339.

Most languages that have expressions like this know the typical order
of operation for things that come from the world of math.

Be careful with this since - and * can be redefined based on the
classes they’re operating on:

a = %w(h t h h h t t t h h t h)
=> [“h”, “t”, “h”, “h”, “h”, “t”, “t”, “t”, “h”, “h”, “t”, “h”]
b = [ “h” ]
=> [“h”]
c = 7
=> 7
a = a - b*c
=> [“t”, “t”, “t”, “t”, “t”]

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Sat, 1 Jul 2006, Dark A. wrote:

TIA
Stuart

My Dear Aunt Sally

1 - multiply
2 - divide
3 - add
4 - substract

regards.

-a

On Jun 30, 2006, at 11:07 AM, Dark A. wrote:

Just checking

if I have

a = a - b*c

b*c gets calculated first then that result is subtracted from a ?

That’s right. Just like normal math rules.

In other words, parantheses, in Ruby, is not necessary to make that
calculation ?

As long as that’s what you want, yes.

James Edward G. II

On Jun 30, 2006, at 12:07 PM, Dark A. wrote:

TIA
Stuart

Yes. Ruby has the usual precedence rules for arithmetic operators.

Agreed , sorry.
Stuart

Dark A. wrote:

Just checking

if I have

a = a - b*c

b*c gets calculated first then that result is subtracted from a ?
In other words, parantheses, in Ruby, is not necessary to make that
calculation ?

Ruby uses pretty much the same precedence rules as C. Se p. 221 in the
PickAxe 1st Ed., for example.

Just checking

if I have

a = a - b*c

b*c gets calculated first then that result is subtracted from a ?
In other words, parantheses, in Ruby, is not necessary to make that
calculation ?

Table of ruby operators and their precedence:
http://phrogz.net/ProgrammingRuby/language.html#table_18.4

----- Original Message ----
From: [email protected]
To: ruby-talk ML [email protected]
Sent: Friday, June 30, 2006 3:23:35 PM
Subject: Re: Order of precedence

On Sat, 1 Jul 2006, Dark A. wrote:

TIA
Stuart

My Dear Aunt Sally

1 - multiply
2 - divide
3 - add
4 - substract

regards.

-a

On 6/30/06, Rob B. [email protected] wrote:

a = %w(h t h h h t t t h h t h)
=> [“h”, “t”, “h”, “h”, “h”, “t”, “t”, “t”, “h”, “h”, “t”, “h”]
b = [ “h” ]
=> [“h”]
c = 7
=> 7
a = a - b*c
=> [“t”, “t”, “t”, “t”, “t”]

Keep in mind that Array#- does setlike operations:

[:h] * 1000 - [:h] #=> []

So the number of h’s above Just Doesn’t Matter.

wow, while I’ve been using irb for some things, I am now a true
believer.

I have been struggling to understand a calculation in some code .
Checking the calc in my head.

It made me forget an important Ruby and probably all programming
languages rule.

Here is the code:

left = 22
write = left/10 # I’m was thinking should return 2.2, but no it
returns just 2 (no modulus)
left = left - write*10 # based on the wrong head calculation above it
was not allowing me to
# what this value truly is.

Well, lesson learned.

Stuart

[email protected] writes:

calculation ?

regards.

-a

suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.

  • h.h. the 14th dali lama

Actually more like

1 - multiply and divide
2 - add and subtract

Steve

My Dear Aunt Sally
I was taught a close variant:
Please Excuse My Dear Aunt Sally

1 - Parenthesis
2 - Exponentiation
3 - Multiply
4 - Divide
5 - Add
6 - Subtract

BODMAS

Brackets
Other (powers etc)
Division
Multiply
Add
Subtract