Hi: The gist of my question: is there a way to *portably* store and retrieve Infinity in an ActiveRecord float column? Following is an amusing saga, but it doesn''t really answer the question. Step 1: As was mentioned in http://www.ruby-forum.com/topic/74141, you can''t simply write a Ruby infinite value. Here are the results in Rails 3 with the sqlite 1.3.3 gem:>> r = TestRec.create(:x => (1.0/0.0))ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: Infinity: INSERT INTO "test_recs" ("x") VALUES (Infinity) Step 2: So if storing infinity isn''t possible, how about a really big value, like Float::MAX?>> r = TestRec.create(:x => Float::MAX)=> #<TestRec id: 1, x: 1.7976931348623157e+308>>> r.x == Float::MAX=> true Step 3: That looks promising, but we''re looking at the in-memory version of the record. We should reload r to get the value from the database:>> r.reload=> #<TestRec id: 1, x: Infinity>>> r.x == Float::MAX=> false>> r.x.infinite?=> 1 Whoa! x has changed from Float::MAX to Infinity. So it appears that if you want to store Infinity in a DB column, you need to store Float::MAX instead. At least for sqlite. Naturally, I don''t trust this as a general solution. Can I assume it will work this way across other DBs? If not, is there a generally portable way to store Infinity? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fearless Fool wrote in post #980885:> The gist of my question: is there a way to *portably* store and retrieve > Infinity in an ActiveRecord float column?Okay, first some clarifications: Infinity is not value and cannot be represented by a float (lower case). What is actually happening is that the Float class has been given the ability to represent Infinity and -Infinity. This is a representation of a concept (similar to nil) not a value. Databases are generally capable of representing nil (or rather NULL), but have no means of representing Infinity (AFAIK). Regardless of Ruby''s somewhat odd implementation; (1.0 / 0.0) is not equal to infinity. Anything divided by zero is undefined. Any real number could represent the result, hence "undefined." Some languages will crash (or throw an exception) on such an operation. I suppose Ruby implemented this in the Float class to provide a way for the language to represent the abstract concept of Infinity. This behavior is, AFAIK, unique to Ruby. So I suppose I''m saying that there may not be a portable way to represent Infinity in relational databases. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 10 Feb 2011, at 20:20, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Fearless Fool wrote in post #980885: >> The gist of my question: is there a way to *portably* store and retrieve >> Infinity in an ActiveRecord float column? > > Okay, first some clarifications: > > Infinity is not value and cannot be represented by a float (lower case). > What is actually happening is that the Float class has been given the > ability to represent Infinity and -Infinity. This is a representation of > a concept (similar to nil) not a value. Databases are generally capable > of representing nil (or rather NULL), but have no means of representing > Infinity (AFAIK). > > Regardless of Ruby''s somewhat odd implementation; (1.0 / 0.0) is not > equal to infinity. Anything divided by zero is undefined. Any real > number could represent the result, hence "undefined." Some languages > will crash (or throw an exception) on such an operation. I suppose Ruby > implemented this in the Float class to provide a way for the language to > represent the abstract concept of Infinity. > > This behavior is, AFAIK, unique to Ruby. So I suppose I''m saying that > there may not be a portable way to represent Infinity in relational > databases.IEEE floats handle infinity ( http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html ). Definitely not a rubyism Fred> > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Quoting Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > On 10 Feb 2011, at 20:20, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > Fearless Fool wrote in post #980885: > >> The gist of my question: is there a way to *portably* store and retrieve > >> Infinity in an ActiveRecord float column? > > > > Okay, first some clarifications: > > > > Infinity is not value and cannot be represented by a float (lower case).[snip]> > IEEE floats handle infinity ( http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html ). Definitely not a rubyism >And Not a Number (NaN). IIRC, square root of -1 is NaN. Jeffrey -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker wrote in post #980944:> Fearless Fool wrote in post #980885:> Okay, first some clarifications: > > Regardless of Ruby''s somewhat odd implementation; (1.0 / 0.0) is not > equal to infinity.Oy! Now you''ve stepped in it. http://en.wikipedia.org/wiki/Division_by_zero#In_computer_arithmetic -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #980965:> On 10 Feb 2011, at 20:20, Robert Walker <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> of representing nil (or rather NULL), but have no means of representing >> there may not be a portable way to represent Infinity in relational >> databases. > > IEEE floats handle infinity ( > http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html ). > Definitely not a rubyismI stand corrected. I did not realize that it did. Thanks. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Where it stands: Infinity can be represented in IEEE floating point numbers. Infinity can be represented in Ruby, as evidenced by>> (1.0/0.0).infinite?=> 1 Infinity can be represented in sqlite (and probably other databases): sqlite> select * from test_recs; 1|Inf| Yet it appears that there''s no portable way to store a Ruby infinity in a database using AR. Does anyone disagree? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.