Re: Newbie! Programming Calculations
- From: Justin Collins <collinsj@xxxxxxxxxxxx>
- Date: Sun, 2 Apr 2006 09:05:06 +0900
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.
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/.
.
- Follow-Ups:
- Re: Newbie! Programming Calculations
- From: Indifferent
- Re: Newbie! Programming Calculations
- References:
- Newbie! Programming Calculations
- From: Indifferent
- Newbie! Programming Calculations
- Prev by Date: Re: (Static) Constructors/Destructors in Ruby
- Next by Date: MONTHNAMES
- Previous by thread: Re: Newbie! Programming Calculations
- Next by thread: Re: Newbie! Programming Calculations
- Index(es):
Relevant Pages
|