invisible Infinity

G

gabriele renzi

Hi gurus and nubys,

I wonder, if you divide a Float by zero, you get Infinity:=> Infinity


and you can even write code using Infinity like:0
1
2
3
...

but if you try to access it directly you can't:NameError: uninitialized constant Infinity
from (irb):4

Should we be able to access Infinity directly? It seem a strange
unexplicable behaviour to me, that it exists but is not normally accessible.
 
C

Charles Mills

Hi gurus and nubys,

I wonder, if you divide a Float by zero, you get Infinity:
=> Infinity


and you can even write code using Infinity like:

a = 10.0/0 => Infinity
b = 10.0/0 => Infinity
a < b => false
c = 2**1024 ....
c.to_f
(irb):10: warning: Bignum out of Float range
=> Infinity

...so your example won't go forever. Eventually it will hit 'Infinity'
and stop. (I think)
So maybe having access to Infinity is a bad thing??
-Charlie

If y is a double rb_big_cmp(x, y) converts x to a double (infinity if
the Bignum is greator than max double) and calls rb_dbl_cmp()

numeric.c:
02323 static VALUE
02324 int_upto(from, to)
02325 VALUE from, to;
02326 {
02327 if (FIXNUM_P(from) && FIXNUM_P(to)) {
02328 long i, end;
02329
02330 end = FIX2LONG(to);
02331 for (i = FIX2LONG(from); i <= end; i++) {
02332 rb_yield(LONG2FIX(i));
02333 }
02334 }
02335 else {
02336 VALUE i = from, c;
02337
02338 while (!(c = rb_funcall(i, '>', 1, to))) {
02339 rb_yield(i);
02340 i = rb_funcall(i, '+', 1, INT2FIX(1));
02341 }
02342 if (NIL_P(c)) rb_cmperr(i, to);
02343 }
02344 return from;
02345 }

00691 VALUE
00692 rb_dbl_cmp(a, b)
00693 double a, b;
00694 {
00695 if (isnan(a) || isnan(b)) return Qnil;
00696 if (a == b) return INT2FIX(0);
00697 if (a > b) return INT2FIX(1);
00698 if (a < b) return INT2FIX(-1);
00699 return Qnil;
00700 }
 
B

Brian Schroeder

Hi gurus and nubys,

I wonder, if you divide a Float by zero, you get Infinity:
=> Infinity


and you can even write code using Infinity like:
0
1
2
3
..

but if you try to access it directly you can't:
NameError: uninitialized constant Infinity
from (irb):4

Should we be able to access Infinity directly? It seem a strange
unexplicable behaviour to me, that it exists but is not normally
accessible.


I also was surprised, that Infinity is no predefined constant, as I used
it in some algorithm where I needed to fill an Array with infinity, then
iteratively reducing the values. In these cases infinity is much better
than nil as a special value because it can be compared.

So I declared 1.0/0.0 as Infinity and ready. But I regard it as against
the pols.

Greetings,

Brian
 
M

Mohammad Khan

If you take a look in numeric.c


/*
* call-seq:
* flt.to_s => string
*
* Returns a string containing a representation of self. As well as a
* fixed or exponential form of the number, the call may return
* ``<code>NaN</code>'', ``<code>Infinity</code>'', and
* ``<code>-Infinity</code>''.
*/

static VALUE
flo_to_s(flt)
VALUE flt;
{
char buf[32];
char *fmt = "%.15g";
double value = RFLOAT(flt)->value;
double avalue, d1, d2;

if (isinf(value))
return rb_str_new2(value < 0 ? "-Infinity" : "Infinity");
else if(isnan(value))
return rb_str_new2("NaN");

avalue = fabs(value);
if (avalue == 0.0) {
fmt = "%.1f";
}
else if (avalue < 1.0e-3) {
d1 = avalue;
while (d1 < 1.0) d1 *= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
}
else if (avalue >= 1.0e15) {
d1 = avalue;
while (d1 > 10.0) d1 /= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
else fmt = "%.16e";
}
else if ((d1 = modf(value, &d2)) == 0) {
fmt = "%.1f";
}
sprintf(buf, fmt, value);

return rb_str_new2(buf);
}

Infinity and -Infinity is String and it is just a representation.
It is definitely, not a Constant.

I am wrong, correct me please.
 
M

Mohammad Khan

If you take a look in numeric.c


/*
* call-seq:
* flt.to_s => string
*
* Returns a string containing a representation of self. As well as a
* fixed or exponential form of the number, the call may return
* ``<code>NaN</code>'', ``<code>Infinity</code>'', and
* ``<code>-Infinity</code>''.
*/

static VALUE
flo_to_s(flt)
VALUE flt;
{
char buf[32];
char *fmt = "%.15g";
double value = RFLOAT(flt)->value;
double avalue, d1, d2;

if (isinf(value))
return rb_str_new2(value < 0 ? "-Infinity" : "Infinity");
else if(isnan(value))
return rb_str_new2("NaN");

avalue = fabs(value);
if (avalue == 0.0) {
fmt = "%.1f";
}
else if (avalue < 1.0e-3) {
d1 = avalue;
while (d1 < 1.0) d1 *= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
}
else if (avalue >= 1.0e15) {
d1 = avalue;
while (d1 > 10.0) d1 /= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
else fmt = "%.16e";
}
else if ((d1 = modf(value, &d2)) == 0) {
fmt = "%.1f";
}
sprintf(buf, fmt, value);

return rb_str_new2(buf);
}

Infinity and -Infinity is String and it is just a representation.
It is definitely, not a Constant.

I am wrong, correct me please.

I meant,
If I am wrong, correct me please.
 
K

Kristof Bastiaensen

If you take a look in numeric.c


/*
* call-seq:
* flt.to_s => string
*
* Returns a string containing a representation of self. As well as a *
fixed or exponential form of the number, the call may return *
``<code>NaN</code>'', ``<code>Infinity</code>'', and *
``<code>-Infinity</code>''.
*/

static VALUE
flo_to_s(flt)
VALUE flt;
{
char buf[32];
char *fmt = "%.15g";
double value = RFLOAT(flt)->value;
double avalue, d1, d2;

if (isinf(value))
return rb_str_new2(value < 0 ? "-Infinity" : "Infinity");
else if(isnan(value))
return rb_str_new2("NaN");

avalue = fabs(value);
if (avalue == 0.0) {
fmt = "%.1f";
}
else if (avalue < 1.0e-3) {
d1 = avalue;
while (d1 < 1.0) d1 *= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
}
else if (avalue >= 1.0e15) {
d1 = avalue;
while (d1 > 10.0) d1 /= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
else fmt = "%.16e";
}
else if ((d1 = modf(value, &d2)) == 0) {
fmt = "%.1f";
}
sprintf(buf, fmt, value);

return rb_str_new2(buf);
}
}
Infinity and -Infinity is String and it is just a representation. It is
definitely, not a Constant.

I am wrong, correct me please.

I meant,
If I am wrong, correct me please.

I am not sure if this is what you meant, but Infinity is not a string:

(10.0/0).class
=> Float

(10.0/0).to_s.class
=> String

Regards,
KB
 
M

Mohammad Khan

If you take a look in numeric.c


/*
* call-seq:
* flt.to_s => string
*
* Returns a string containing a representation of self. As well as a *
fixed or exponential form of the number, the call may return *
``<code>NaN</code>'', ``<code>Infinity</code>'', and *
``<code>-Infinity</code>''.
*/

static VALUE
flo_to_s(flt)
VALUE flt;
{
char buf[32];
char *fmt = "%.15g";
double value = RFLOAT(flt)->value;
double avalue, d1, d2;

if (isinf(value))
return rb_str_new2(value < 0 ? "-Infinity" : "Infinity");
else if(isnan(value))
return rb_str_new2("NaN");

avalue = fabs(value);
if (avalue == 0.0) {
fmt = "%.1f";
}
else if (avalue < 1.0e-3) {
d1 = avalue;
while (d1 < 1.0) d1 *= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
}
else if (avalue >= 1.0e15) {
d1 = avalue;
while (d1 > 10.0) d1 /= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
else fmt = "%.16e";
}
else if ((d1 = modf(value, &d2)) == 0) {
fmt = "%.1f";
}
sprintf(buf, fmt, value);

return rb_str_new2(buf);
}
}
Infinity and -Infinity is String and it is just a representation. It is
definitely, not a Constant.

I am wrong, correct me please.

I meant,
If I am wrong, correct me please.

I am not sure if this is what you meant, but Infinity is not a string:

(10.0/0).class
=> Float

(10.0/0).to_s.class
=> String

irb(main):001:0> a = 10.0/0
=> Infinity
irb(main):002:0> b = -20.0/0
=> -Infinity

I said, Infinity and -Infinity is just a String representation of Float,
not a Constant.

regards
Mohammad
 
K

Kristof Bastiaensen

On Wed, 2004-09-01 at 15:22, Mohammad Khan wrote:
If you take a look in numeric.c


/*
* call-seq:
* flt.to_s => string
*
* Returns a string containing a representation of self. As well as
a *
fixed or exponential form of the number, the call may return *
``<code>NaN</code>'', ``<code>Infinity</code>'', and *
``<code>-Infinity</code>''.
*/

static VALUE
flo_to_s(flt)
VALUE flt;
{
char buf[32];
char *fmt = "%.15g";
double value = RFLOAT(flt)->value;
double avalue, d1, d2;

if (isinf(value))
return rb_str_new2(value < 0 ? "-Infinity" : "Infinity");
else if(isnan(value))
return rb_str_new2("NaN");

avalue = fabs(value);
if (avalue == 0.0) {
fmt = "%.1f";
}
else if (avalue < 1.0e-3) {
d1 = avalue;
while (d1 < 1.0) d1 *= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
}
else if (avalue >= 1.0e15) {
d1 = avalue;
while (d1 > 10.0) d1 /= 10.0;
d1 = modf(d1, &d2);
if (d1 == 0) fmt = "%.1e";
else fmt = "%.16e";
}
else if ((d1 = modf(value, &d2)) == 0) {
fmt = "%.1f";
}
sprintf(buf, fmt, value);

return rb_str_new2(buf);
}
}
Infinity and -Infinity is String and it is just a representation. It
is definitely, not a Constant.

I am wrong, correct me please.

I meant,
If I am wrong, correct me please.
I am not sure if this is what you meant, but Infinity is not a string:

(10.0/0).class
=> Float

(10.0/0).to_s.class
=> String

irb(main):001:0> a = 10.0/0
=> Infinity
irb(main):002:0> b = -20.0/0
=> -Infinity

I said, Infinity and -Infinity is just a String representation of Float,
not a Constant.

So it is not what you meant then :)
It wasn't clear to me you where talking about irb output.
Sorry for the confusion.
 
F

Florian Gross

Mohammad said:
I said, Infinity and -Infinity is just a String representation of Float,
not a Constant.

Yes, that's exactly the objection of the original poster. He wants it to
be a constant so it can be accessed easily.
regards
Mohammad

More regards,
Florian Gross
 
M

Mark Hubbart

If you take a look in numeric.c

<snip code>

Infinity and -Infinity is String and it is just a representation.
It is definitely, not a Constant.

I am wrong, correct me please.

What is displayed are just string representations of the value, but
they are actually valid float values. Floats can be any floating point
number, within a certain range, or: positive or negative Infinity
(±Infinity), positive or negative Zero (±0.0), or not a number (NaN).

They are all valid float values, but there is no way to access them,
aside from calculating them. I'm not sure how often they would be
useful, but it does seem strange that Infinity and NaN are the only
float values that you can't state directly.

cheers,
Mark
 
Y

Yukihiro Matsumoto

Hi,

In message "invisible Infinity"

|Should we be able to access Infinity directly? It seem a strange
|unexplicable behaviour to me, that it exists but is not normally accessible.

Considering non IEEE platform, there's no portable way to generate
Inifinity and NaN, I think. Teach me if I'm wrong.

matz.
 
A

Ara.T.Howard

Hi,

In message "invisible Infinity"

|Should we be able to access Infinity directly? It seem a strange
|unexplicable behaviour to me, that it exists but is not normally accessible.

Considering non IEEE platform, there's no portable way to generate
Inifinity and NaN, I think. Teach me if I'm wrong.

matz.

is it an impossible idea to map the various Nan's to some singleton like
InfinityClass - for which Infinity would be the only instance, similar to
FalseClass and TrueClass? we could also have NegativeInfinity as well.
operators for these singletons would be defined as

class << Infinity
def < other; false; end
def > other; other == self ? false : true; end
end
class << NegativeInfinity
def > other; false; end
def < other; other == self ? false : true; end
end

the reason i say this is that the notion of infinity is useful for many
things, like Time and Date, not just Numeric.

perhaps this is too hard to make generic - but it would be a very useful
paradigm to have at one's disposal.

kind regards.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
M

Mohammad Khan

Just curious,

inf = 10.0/0

1.upto(inf) do | i |
# do something
end


Is this an endless loop? Logically it should be.

Mohammad





Hi,

In message "invisible Infinity"

|Should we be able to access Infinity directly? It seem a strange
|unexplicable behaviour to me, that it exists but is not normally accessible.

Considering non IEEE platform, there's no portable way to generate
Inifinity and NaN, I think. Teach me if I'm wrong.

matz.

is it an impossible idea to map the various Nan's to some singleton like
InfinityClass - for which Infinity would be the only instance, similar to
FalseClass and TrueClass? we could also have NegativeInfinity as well.
operators for these singletons would be defined as

class << Infinity
def < other; false; end
def > other; other == self ? false : true; end
end
class << NegativeInfinity
def > other; false; end
def < other; other == self ? false : true; end
end

the reason i say this is that the notion of infinity is useful for many
things, like Time and Date, not just Numeric.

perhaps this is too hard to make generic - but it would be a very useful
paradigm to have at one's disposal.

kind regards.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
N

nobu.nokada

Hi,

At Thu, 2 Sep 2004 11:10:21 +0900,
the reason i say this is that the notion of infinity is useful for many
things, like Time and Date, not just Numeric.

Isn't it Eternity rather than Infinity? :)
 
A

Austin Ziegler

At Thu, 2 Sep 2004 11:10:21 +0900,

Isn't it Eternity rather than Infinity? :)

No, Eternity is the amount of time one has to wait when one is in line
behind fifteen people at the grocery store with a cashier that doesn't
speak one's native language, everyone having a full cart, and everyone
wanting to pay by cheque.

-austin
 
A

Ara.T.Howard

Hi,

At Thu, 2 Sep 2004 11:10:21 +0900,


Isn't it Eternity rather than Infinity? :)


i like that!

class << Time

Eternity = ::Infinity
NegativeEternity = ::NegativeInfinity

end

i got the 'infinity' nomenclature from bi-temporal database terminology and
postgres.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top