Short version: using equality with floats isn’t a foot idea (not just
in this case - in general)
Fred
Should I just convert the floats into strings instead perhaps?
I doubt that would help - that’s happening anyway when rails generates
the query. == Is fundamentally dangerous with floats, almost all the
time you want to be searching in a range.
I doubt that would help - that’s happening anyway when rails
generates
the query. == Is fundamentally dangerous with floats, almost all the
time you want to be searching in a range.
Thanks. What would be the correct measure for me to take?
instead of x == y, you typically want x => y - delta && x <= y + delta
for some suitable value of delta
I doubt that would help - that’s happening anyway when rails generates
the query. == Is fundamentally dangerous with floats, almost all the
time you want to be searching in a range.
Thanks. What would be the correct measure for me to take?
I ended up taking the chicken way out, converting my floats to strings
in new columns and adapting my find conditions to look for those
instead. Works like charm. Thanks anyways, I wonder why it didnt work.
I ended up taking the chicken way out, converting my floats to strings
in new columns and adapting my find conditions to look for those
instead. Works like charm. Thanks anyways, I wonder why it didnt work.
As indicated by Frederick that’s just the nature of a float data type.
It doesn’t matter whether it’s Ruby, SQL, Java or C. It is never safe to
compare equality on floating point values due to the inherent nature of
how they are stored.
Example: You may be seeing 11.967 but that value is going to get
converted to an IEEE floating point hexadecimal value. Depending on the
loss of precision it is quite possible to have 11.967 == 11.967 =>
false. Depending on how the two values get translated to and from the
IEEE floating point hexadecimal values. 11.967 might actually be stored
in the hex equivalent of 11.9669999999999.
There are a few techniques for dealing with this problem:
Given:
x = 11.967
y = 11.966999999999 ← loss of precision due to hex<->decimal
conversion
Compare based on a range (as suggested in a prior post)
11.9670 <= x < 11.968.
Compare string representations of the floating point values:
x == y
=> false
(“%.3f” % x) == (“%.3f” % y)
=> true
Store values using a fixed point data type (such as DECIMAL in
MySQL):
lng DECIMAL(5,3)
x → lng
=> 11.967
y → lng
=> 11.967 ← rounded based on the rules defined in the database
Making it safe to compare equality on the two values
Store fixed decimal values as integers in the database and convert
them when displaying them. I only mention this technique because it is
an option for storing currency values. Rather than store dollars, store
cents instead and convert to dollars with displaying the values on the
view.
$1.60 → 160 (in the database)
In your case you have a slightly combined approach in that you are
storing the value in the database as a string. This technique will also
work as long as you ensure proper rounding from Float to String.
Option #3 above would be the approach I would personally take in this
situation. It allows storing numeric decimal values while preserving a
given precision.