From: Justin Collins Date: 2006-04-02T09:05:06+09:00 Subject: 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/.