Can you portably store infinity via AR?

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?

Fearless F. 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.

On 10 Feb 2011, at 20:20, Robert W. [email protected] 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 rubyism

Fred

Robert W. wrote in post #980944:

Fearless F. 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

Frederick C. wrote in post #980965:

On 10 Feb 2011, at 20:20, Robert W. [email protected] 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 rubyism

I stand corrected. I did not realize that it did. Thanks.

Quoting Frederick C. [email protected]:

On 10 Feb 2011, at 20:20, Robert W. [email protected] wrote:

Fearless F. 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

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?

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