# Forum: Ruby Newbie! Programming Calculations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2006-04-02 01:38
```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.

on 2006-04-02 01:55
```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 <yascene@gmail.com>:```
on 2006-04-02 02:04
```Indifferent wrote:
> 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.
>

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```
on 2006-04-02 02:13
```Justin Collins 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 :)

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

Why is -52 calculation done after the brackets are closed?
How come the +98 was done last?```
on 2006-04-02 02:14
```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.
>
>

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

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

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

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

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.```
on 2006-04-02 02:41
```So are these calculations correct:

how many hours are in a year? = puts (7* (60*24))  *52
how many minutes are in a decade? = puts (7* (60*24)) *52 *10
how many seconds old are you? = puts 7* (*24(60*60)) *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?```
on 2006-04-02 02:51
```Indifferent wrote:
> Justin Collins 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 :)
>
> One more thing:
> 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)

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

Some more general information here:

http://en.wikipedia.org/wiki/Operator_precedence

-Justin```
on 2006-04-02 04:12
```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

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

2006/4/2, Indifferent <yascene@gmail.com>:```
on 2006-04-02 04:46
```On Sun, 2 Apr 2006 09:41:14 +0900, Indifferent <yascene@gmail.com>
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```
This topic is locked and can not be replied to.