Ask HN: Why Ruby doesn’t know how to do maths?
I'm not an expert in maths but the following operation gives a different result in ruby than in any other language or calculator I've tried:
Ruby: (289 / 30 * 30) - (149 / 30 * 30)
150
Rest of the world: (289 / 30 * 30) - (149 / 30 * 30)
140
An explanation is greatly appreciated
7 comments
[ 3.1 ms ] story [ 27.2 ms ] threadRuby does know how to do math and is doing precisely what you told it to.
Ruby behaves like SQL in that appending a decimal to the values results in non-integer maths being employed.
MS-SQL itself is slightly out (I assume due to floating point inaccuracy)
SELECT (289.0 / 30.0 * 30.0) - (149.0 / 30.0 * 30.0) 140.0000100