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

irb says yes:

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

-Mat

···

On Jun 30, 2006, at 12:07 PM, Dark Ambient 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 ?

TIA
Stuart

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

-austin

···

On 6/30/06, Dark Ambient <sambient@gmail.com> 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 ?

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

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 Gray II

···

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

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 Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Jun 30, 2006, at 12:07 PM, Dark Ambient 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 ?

TIA
Stuart

My Dear Aunt Sally

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

regards.

-a

···

On Sat, 1 Jul 2006, Dark Ambient 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 ?

TIA
Stuart

--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

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

···

On Jun 30, 2006, at 12:07 PM, Dark Ambient 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 ?

TIA
Stuart

Dark Ambient 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.

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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

Agreed , sorry.
Stuart

···

On 6/30/06, Austin Ziegler <halostatue@gmail.com> wrote:

On 6/30/06, Dark Ambient <sambient@gmail.com> 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
--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

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

···

On 6/30/06, Austin Ziegler <halostatue@gmail.com> wrote:

On 6/30/06, Dark Ambient <sambient@gmail.com> 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
--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

My Dear Aunt Sally

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

regards.

-a

···

----- Original Message ----
From: ara.t.howard@noaa.gov
To: ruby-talk ML <ruby-talk@ruby-lang.org>
Sent: Friday, June 30, 2006 3:23:35 PM
Subject: Re: Order of precedence

On Sat, 1 Jul 2006, Dark Ambient 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 ?

TIA
Stuart

--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

Keep in mind that Array#- does setlike operations:

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

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

···

On 6/30/06, Rob Biedenharn <Rob@agileconsultingllc.com> 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"]

ara.t.howard@noaa.gov writes:

···

On Sat, 1 Jul 2006, Dark Ambient 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 ?

TIA
Stuart

My Dear Aunt Sally

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

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

···

On 6/30/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Sat, 1 Jul 2006, Dark Ambient 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 ?
>
> TIA
> Stuart

My Dear Aunt Sally

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

regards.

-a
--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

BODMAS

Brackets
Other (powers etc)
Division
Multiply
Add
Subtract

···

On 7/1/06, Steven Lumos <steven@lumos.us> wrote:

ara.t.howard@noaa.gov writes:

> On Sat, 1 Jul 2006, Dark Ambient 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 ?
>>
>> TIA
>> Stuart
>
> My Dear Aunt Sally
>
> 1 - multiply
> 2 - divide
> 3 - add
> 4 - substract
>
> 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