Newbie! Programming Calculations

Im new to programming, im learning ruby.

Here is the guide I am working through:
http://pine.fm/LearnToProgram/?Chapter=01

At the bottom of the page there are these sample calculations:
puts 5 * (12-8) + -15 (is that + minus15 or does the + and the - mean
something combined? same for the one below with * and -52)
puts 98 + (59872 / (13*8)) * -52

What do they do? What are the brackets for?
I sort of remember doing something simular to this when I was using
excel in a course i did. Is the number is the brackets calculated first?
Can someone explain this to me? Or give me a link were I can find more
information.

Extreamly confused newbie! Please help!

Ruby follow the laws of math. That means the a + multiplied by a -
(which is
really what ± means, but it’s just not written that way) will result in
a
-. That leads to: ±15 = -15.

Here is the a thumb rule for remembering what the parentheses (brackets)
are
for, that I learned in one of my first years with math:
When a calculation is inside a parenthesis it is saying: “Calculate me
first!”

When doing really advanced calculations, it’s a good idea to put a lot
of
parentheses so that the computer and you agree on which mathematical
operations are to be done first.

If you want more info, on this particular subject, you should try
googling
for math rather than ruby.

2006/4/2, Indifferent [email protected]:

Indifferent wrote:

Im new to programming, im learning ruby.

Here is the guide I am working through:
Numbers - Learn to Program

At the bottom of the page there are these sample calculations:
puts 5 * (12-8) + -15 (is that + minus15 or does the + and the - mean
something combined? same for the one below with * and -52)
puts 98 + (59872 / (13*8)) * -52

What do they do? What are the brackets for?
I sort of remember doing something simular to this when I was using
excel in a course i did. Is the number is the brackets calculated first?
Can someone explain this to me? Or give me a link were I can find more
information.

Extreamly confused newbie! Please help!

Just like in math, the parentheses indicate what happens first. The
inner-most parentheses are calculated first.

An expression like:

98 + 59872 / 13 * 8 * -52

is evaluated as:

59872 / 13 = 4605

4605 * 8 = 36840

36840 * -52 = -1915680

-1915680 + 98 = -1915582

Compared to:

98 + (59872 / (13*8)) * -52

Which is evaluated as:

13 * 8 = 104

59872 / 104 = 575

575 * -52 = -29900

-29900 + 98 = -29802

The difference in order of evaluation is due to the parentheses.

Hopefully that helps…

-Justin

Indifferent wrote:

What do they do? What are the brackets for?
I sort of remember doing something simular to this when I was using
excel in a course i did. Is the number is the brackets calculated first?
Can someone explain this to me? Or give me a link were I can find more
information.

Extreamly confused newbie! Please help!

When you have an expression that uses more than one arithmetic operator,
such as 6 + 12 / 3, it’s not always obvious which operations should be
performed first. Should we add 6 and 12 first, or divide 12 by 3 and
then add 6?

Ruby follows a couple of very simple rules in this case.

Rule 1: First do all the multiplications and divisions, then all the
additions and subtractions.

Rule 2: If there are more than 1 multiplication or division, or more
than one addition or subtraction, do them from left to right.

So, for 6 + 12 / 3, Ruby divides 12 by 3 first, which gives it 4, and
then adds 6 and 4, so the result is 10.

Now it’s possible that you may want Ruby to actually add 6 and 12 and
then divide the result by 3, so Ruby gives you a way to tell it to do it
that way. To do this, enclose 6 + 12 in parentheses: (6 + 12) / 3. Ruby
will do the addition, getting 18, and then divide 18 / 3, giving 6.

You can nest the parentheses, in which case the most deeply nested
expressions are evaluated first: ((3+5)/2)+6 means “add 3 and 5, divide
by 2, and add 6”.

For rule 2, given the expression 24 / 2 * 3, since division and
multiplication are equally “important,” Ruby just goes left to right.
First it divides 24 / 2, giving 12, then multiplies by 3, giving 36.
Similarly, for 12 + 3 - 5, Ruby adds 12 and 3, giving 15, and then
subtracts 5, giving 10.

Regarding -15, or -52, consider that most arithmetic operations are
“binary,” that is, the operation produces a result by combining two
numbers. However, we can say that a number is negative by preceding it
with a minus sign. The minus sign is called a “unary” operator. That is,
it produces its result from only one number.

Since a unary minus sign negates the number it precedes, 25 + =15 adds a
negative 15 to 25, which is the same as subtracting 15 from 25.

Justin C. wrote:

Just like in math, the parentheses indicate what happens first. The
inner-most parentheses are calculated first.

An expression like:

98 + 59872 / 13 * 8 * -52

is evaluated as:

59872 / 13 = 4605

4605 * 8 = 36840

36840 * -52 = -1915680

-1915680 + 98 = -1915582

Compared to:

98 + (59872 / (13*8)) * -52

Which is evaluated as:

13 * 8 = 104

59872 / 104 = 575

575 * -52 = -29900

-29900 + 98 = -29802

The difference in order of evaluation is due to the parentheses.

Hopefully that helps…

-Justin

Cool thank you for the explanations guys :slight_smile:

One more thing:
In your example
98 + (59872 / (13*8)) * -52

Why is -52 calculation done after the brackets are closed?
How come the +98 was done last?

So are these calculations correct:

how many hours are in a year? = puts (7* (6024)) 52
how many minutes are in a decade? = puts (7
(60
24)) *52 10
how many seconds old are you? = puts 7
(24(6060)) *20 (that wrong)
how many chocolates do you hope to eat in your life? (joke?) :S

If I am 936 million seconds old, how old am I? (no clue)

Anyone care to clear it up for me?

Indifferent wrote:

Justin C. wrote:

Just like in math, the parentheses indicate what happens first. The
inner-most parentheses are calculated first.

An expression like:

98 + 59872 / 13 * 8 * -52

is evaluated as:

59872 / 13 = 4605

4605 * 8 = 36840

36840 * -52 = -1915680

-1915680 + 98 = -1915582

Compared to:

98 + (59872 / (13*8)) * -52

Which is evaluated as:

13 * 8 = 104

59872 / 104 = 575

575 * -52 = -29900

-29900 + 98 = -29802

The difference in order of evaluation is due to the parentheses.

Hopefully that helps…

-Justin

Cool thank you for the explanations guys :slight_smile:

One more thing:
In your example
98 + (59872 / (13*8)) * -52

Why is -52 calculation done after the brackets are closed?
How come the +98 was done last?

There is a thing (which other posts here mention) called operator
precedence. Some operators come before others. If the operators have the
same precedence, then it goes left to right.

Here’s an abbreviated precedence order for Ruby (from the Pickaxe, I
can’t find it online) from highest to lowest (first to last):

** (exponent)

    • (unary plus and minus, positive and negative)
      
  • / % (multiplication, division, modulo)
    • (addition, subtraction)
      

So that’s why -52 is ‘negative fifty-two’ and then it is multiplied. And
why + 98 comes last.

Some more general information here:

-Justin

On Sun, 2 Apr 2006 09:41:14 +0900, Indifferent [email protected]
wrote:

If I am 936 million seconds old, how old am I? (no clue)

Anyone care to clear it up for me?

Just start converting up:

seconds → minutes → hours → days → weeks → years

936,000,000 seconds / 60 = 15,600,000 minutes

15,600,000 minutes / 60 = 260,000 hours

260,000 hours / 24 = 10,833.33 days

10,833.33 days / 7 = 1547.61 weeks

1547.61 weeks / 52 = 29.76 years

which equals:

936,000,000 / 60 / 60 /24 / 7 / 52 = 29.76

Or if you want to be a little bit more precise

936,000,000 / 60 / 60 /24 / 365.25 = 29.66

You don’t really need to make parentheses when you are only doing
multiplication, because it does not matter in which order you do them.

Here is a clue:
You just found out how many seconds you are old, by multiplying with
certain
numbers. These number will help you find his age through division.

Example:
936000000/60
This will give you his age in minutes.

2006/4/2, Indifferent [email protected]: