Hacker News new | ask | show | jobs
by ajimix 2550 days ago
thanks for the clear explanation. It's strange to me as a newbie in ruby that this can happen and I think that can lead to errors if you don't take that into account when doing calculations with numbers... Good to know
2 comments

NB - you get the same result in MS-SQL (and for exactly the same reason).

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

The distinction between integers and floating point variables is the same in most mainstream languages and is not specific to Ruby.