On Monday, January 7, 2013 3:27:58 PM UTC-8, Sean Mackesey wrote:
>
> I have a database entry (with model class `Number`) that I can't seem to 
> match against.  It has a field called "value", and the value appears to be 
> 5.1.  But I can't seem to match it via Sequel's `first` and `where` 
> methods.  There are other entries in the table that I have no trouble 
> matching by the `value` field.  What's going on?
>
> [37] pry(#<CountInference::Optimization::NumMarkerLoader>)> Number[17]
> => #<Number @values={:id=>17, :raw=>"5.1p#CA1:SR:do/CCK+#CB+", 
> :value=>5.1, :std=>0.0, :type=>""}>
> [38] pry(#<CountInference::Optimization::NumMarkerLoader>)> 
> Number[17].value
> => 5.1
> [39] pry(#<CountInference::Optimization::NumMarkerLoader>)> 
> Number[17].value == 5.1
> => true
> [40] pry(#<CountInference::Optimization::NumMarkerLoader>)> 
> Number.first(value: 5.1)
> => nil
> [42] pry(#<CountInference::Optimization::NumMarkerLoader>)> 
> Number.where(value: 5.1).all
> => []
>

The value column is probably floating point (real or double precision 
database type).  You should not use equality when dealing with floating 
point numbers, in ruby, SQL, or any other programming language.  If you 
don't understand how to use floating point numbers, Google should be able 
to help you.

Try something like:

  Number.where(value: (5.09999999999)..(5.100000000001)).all

Alternatively, if you want to use equality and can change the database 
type, switch to a decimal/numeric type instead of a floating point type.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sequel-talk/-/fZgNQ_OElPEJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to