-Infinity for Doule type column

Hi list.

I’m writing a program that stores a lot of Floats into MySQL database.
Simplified version of the program use the following form of class.

class Val < ActiveRecord::BASE
end

And Vals table contains one column:

num double

One of my data contains -Infinity for num and when I try to
Val.new
Val.num = <- Here goes -Inifinity
Val.save!

Then the program crashes:
“/usr/local/stow/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/connection_adapters/abstract_adapter.rb:120:in
log': Mysql::Error: Unknown column 'Infinity' in 'field list': INSERT INTO full_doc_indices (etf,Id,phonemes,filename,end_time,start_time`) VALUES(-Infinity, NULL, ‘IH N T IH’, ‘BN99EN_2’,
4952.77044, 4939.515) (ActiveRecord::StatementInvalid)”

Obviously, ActiveRecord is using “-Infinity” literally, and then MySQL
treats “-Infinity” as column name as the message shows. How can I handle
-Infinity then? I really have to store -Infinity anyway.

Thanks.

Sincerely,
Minkoo S.

Ruby does not seem to have a good way of representing infinity in
floats. “(1.0/0.0).inifinite?” is more about checking an overflow.

One solution would be to create another column in the table called
inifinity that could be 0 for not infinite, 1 for positive infinity,
and 2 for negative infinity. You would only check the actual Val
column if infinity was 0.

You would then write a method in your model that converts from your
value to the two database columns. Lookup up facade columns for one
way to do this.

        - dan


Dan K. mailto:[email protected]
http://www.dankohn.com/ tel:+1-415-233-1000

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs