This project is archived and is in readonly mode.
dangerous float operations
-
Tomash
Update: 8.95 is also a "magical number" inside ActiveRecord, i.e. balance=8.95 sets balance field to 894. Other numbers "around" seem OK.
>> a.balance=(8.95) => 8.95 >> a.balance => 894 >> a.balance=(8.93) => 8.93 >> a.balance => 893 -
isak
There is no bug, floating point numbers are inaccurate by design.
The only reason people muck around with cents for money values is to avoid the floating point issue. This approach really hasn't been viable since 1.2, when ActiveRecord finally got support for decimal columns, but it seems like many pick it up from outdated tutorials floating around on the web.
-
Jim Lindley
Tomasz,
There's more info on the details here, but isak is right - storage of floating point numbers in binary representation is less accurate then you'd think.
-
Frederick Cheung
A classic paper on this is http://docs.sun.com/source/806-3...
-
Tomash
I know, I know. We're using INT columnt because of that and convert numbers on-the-fly by overloading amount and amount= methods in our models. But I've just realised it's a bad approach and we should just represent them as ints also internally, doing the "conversion" only on the view level.
Sorry for the hassle, You can close it as invalid/wontfix/reporter_incompetent ;)
-
Tarmo Tänav
- State changed from new to invalid
- Tag changed from activerecord, bug to activerecord, bug