# Newbie! Programming Calculations

**URL:** <https://rubytalk.org/t/newbie-programming-calculations/26343>\
**Category:** ruby-talk\
**Created:** [1 April 2006 23:38 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343 "2006-04-01T23:38:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Indifferent](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@Indifferent](https://rubytalk.org/u/Indifferent)\
**Post date:** [1 April 2006 23:38 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343/1 "2006-04-01T23:38:12Z")

</div>

Im new to programming, im learning ruby.

Here is the guide I am working through:  
[http://pine.fm/LearnToProgram/?Chapter=01](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!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeppe\_Jakobsen](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Jeppe\_Jakobsen](https://rubytalk.org/u/Jeppe_Jakobsen)\
**Post date:** [1 April 2006 23:55 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343/2 "2006-04-01T23:55:23Z")

</div>

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\>:
> 
> > Im new to programming, im learning ruby.
> > 
> > Here is the guide I am working through:  
> > [http://pine.fm/LearnToProgram/?Chapter=01](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!
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> "winners never quit, quitters never win"

---

<div class="post-metadata">

**Author:** ![Collins\_Justin](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@Collins\_Justin](https://rubytalk.org/u/Collins_Justin)\
**Post date:** [2 April 2006 00:05 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343/3 "2006-04-02T00:05:06Z")

</div>

Indifferent wrote:

> Im new to programming, im learning ruby.
> 
> Here is the guide I am working through:  
> [http://pine.fm/LearnToProgram/?Chapter=01](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!

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter](https://avatars.discourse-cdn.com/v4/letter/t/7feea3/32.png) [@Tim\_Hunter](https://rubytalk.org/u/Tim_Hunter)\
**Post date:** [2 April 2006 00:13 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343/4 "2006-04-02T00:13:44Z")

</div>

Indifferent wrote:

> Im new to programming, im learning ruby.
> 
> Here is the guide I am working through:  
> [http://pine.fm/LearnToProgram/?Chapter=01](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!

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.

---

<div class="post-metadata">

**Author:** ![Indifferent](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@Indifferent](https://rubytalk.org/u/Indifferent)\
**Post date:** [2 April 2006 00:41 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343/5 "2006-04-02T00:41:14Z")

</div>

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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Indifferent](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@Indifferent](https://rubytalk.org/u/Indifferent)\
**Post date:** [2 April 2006 00:13 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343/6 "2006-04-02T00:13:58Z")

</div>

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:  
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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jeppe\_Jakobsen](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Jeppe\_Jakobsen](https://rubytalk.org/u/Jeppe_Jakobsen)\
**Post date:** [2 April 2006 02:11 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343/7 "2006-04-02T02:11:06Z")

</div>

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 \<yascene@gmail.com\>:
> 
> > 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?
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> "winners never quit, quitters never win"

---

<div class="post-metadata">

**Author:** ![Jan\_K](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@Jan\_K](https://rubytalk.org/u/Jan_K)\
**Post date:** [2 April 2006 02:43 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343/8 "2006-04-02T02:43:45Z")

</div>

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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
260,000 hours / 24 = 10,833.33 days  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
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

> **···**
>
> 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?

---

<div class="post-metadata">

**Author:** ![Collins\_Justin](https://avatars.discourse-cdn.com/v4/letter/c/e19adc/32.png) [@Collins\_Justin](https://rubytalk.org/u/Collins_Justin)\
**Post date:** [2 April 2006 00:51 UTC](https://rubytalk.org/t/newbie-programming-calculations/26343/9 "2006-04-02T00:51:40Z")

</div>

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:  
> 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:

> **[Order of operations](https://en.wikipedia.org/wiki/Operator_precedence)**
>
> In mathematics and computer programming, the order of operations is a collection of rules that reflect conventions about which operations to perform first in order to evaluate a given mathematical expression.
> These rules are formalized with a ranking of the operators. The rank of an operator is called its precedence, and an operation with a higher precedence is performed before operations with lower precedence. Calculators generally perform operations with the same precedence from left to rig...

-Justin

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
