From: Charles Mills Date: 2004-09-02T03:40:43+09:00 Subject: Re: invisible Infinity On Sep 1, 2004, at 11:10 AM, gabriele renzi wrote: > Hi gurus and nubys, > > I wonder, if you divide a Float by zero, you get Infinity: > >> a=10.0/0 > => Infinity > > > and you can even write code using Infinity like: > >> 0.upto(a) do |x| p x end > 0 > 1 > 2 > 3 > ... > > 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 } > > but if you try to access it directly you can't: > >> Infinity > 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.