Re: Newbie! Programming Calculations



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/.


.



Relevant Pages

  • Re: How can i create this formula
    ... you need to implement the Order of Operations for calculations. ... Arithmetic operations are preformed in the following order: ... chosen cell + 50 in parentheses so it calculates first: ... MVP FAQ site: http://mvps.org/ ...
    (microsoft.public.office.misc)
  • Re: Calculating sell price from several fields access 2003
    ... Search for Operator Precedence in the VBA help to determine the order of calculations. ... You may need to add parentheses around some of your expression if it is not calculating the way you wish. ... I have fields of cost_price, Ex_rate, Freight_Factor as percent, Margin as percent ...
    (microsoft.public.access.queries)
  • Re: Need recommendations: bad historical romances
    ... Neil Barnes wrote: ... You have your economist's hat on ... and assume that the evaluation is being made. ... Calculations, certain. ...
    (rec.arts.sf.composition)