alcina
August 12, 2013, 1:46pm
1
Could anybody please give me an answer to the following?
puts 98 + (59872 / (13*8)) * -51
PEMDAS
Parentheses, Exponents, Multiplication, Division, Addition, Subtraction
Multiplication and Division,Addition and Subtraction done in the order
they
occur in the problem.
The answer from Ruby for the above calculation is -29227
If you calculate the same problem in Microsoft Excel and a Scientific
calculator
the answer is -29262
How is this possible and why is there a difference in the answer?
Thanks
Stefan
Subject: How does Ruby do calculations?
Date: lun 12 ago 13 01:46:10 +0200
Quoting Stefan du Plooy ([email protected] ):
occur in the problem.
The answer from Ruby for the above calculation is -29227
If you calculate the same problem in Microsoft Excel and a Scientific
calculator
the answer is -29262
How is this possible and why is there a difference in the answer?
If you just add a .0 in the right place…
puts 98 + (59872 / (13*8.0)) * -51
-29262.307692307695
In your line, all numbers were integers, so integer arithmetics was
used:
98 + (59872 / 104) * -51
98 + 575 * -51
98 + (-29325)
-29227
Carlo
How is this possible and why is there a difference in the answer?
You’re doing integer math, which can be weird, since it truncates the
result to integer values. In your example, the truncation error is
multiplied, yielding a large difference in value. Try this:
98 + (59872 / (13.0*8)) * -51
Just making one of the numbers a float will trigger floating point
evaluation. If you want even more precision, use the ‘bigdecimal’
library.
kaspar
Thank you, your answer totally cleared up the misunderstanding.
Kind regards
Stefan
If you just add a .0 in the right place…
puts 98 + (59872 / (13*8.0)) * -51
-29262.307692307695
In your line, all numbers were integers, so integer arithmetics was
used:
98 + (59872 / 104) * -51
98 + 575 * -51
98 + (-29325)
-29227
Carlo
On Tue, Aug 13, 2013 at 8:25 AM, Kaspar S.
[email protected] wrote:
evaluation. If you want even more precision, use the ‘bigdecimal’ library.
kaspar
You have to make sure they’re a float at each calculation e.g:
1/2 + 1/2.0 # => 0.5
The first 1/2 is still doing integer division, so it should be:
1/2.0 + 1/2.0 # => 1.0
Also, if you’re going to use floats, make sure you’re familiar with
their
imprecision:
7.01 - 7.00 # => 0.009999999999999787
-Josh
PEMDAS
Parentheses, Exponents, Multiplication, Division, Addition, Subtraction
Multiplication and Division,Addition and Subtraction done in the order
they
occur in the problem.
stefan8107:
On Tue, Aug 13, 2013 at 8:25 AM, Kaspar S.
[[email protected] ] (mailto:[email protected] )wrote:
evaluation. If you want even more precision, use the ‘bigdecimal’ library.
kaspar
You have to make sure they’re a float at each calculation e.g:
1/2 + 1/2.0 # => 0.5
The first 1/2 is still doing integer division, so it should be:
1/2.0 + 1/2.0 # => 1.0
Also, if you’re going to use floats, make sure you’re familiar with
their mygroundbiz
imprecision:
7.01 - 7.00 # => 0.009999999999999787
-Josh
Excellent tutorial and very well explained!