Newbie! Programming Calculations

I

Indifferent

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!
 
J

Jeppe Jakobsen

------=_Part_15655_12582878.1143935721102
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

Here is the a thumb rule for remembering what the parentheses (brackets) ar=
e
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.
 
J

Justin Collins

Indifferent said:
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!

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
 
I

Indifferent

Justin said:
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?
 
I

Indifferent

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

Justin Collins

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

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


-Justin
 
J

Jeppe Jakobsen

------=_Part_16151_3766229.1143943865247
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 certai=
n
numbers. These number will help you find his age through division.

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

Jan_K

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top