Well, I certainly expected the floating point behavior that Ruby uses, since
it's also been used in every language that I've used FP in.  However, I
realized that I had no idea why, and this discussion made me curious so I went
and looked it up.  There are three things to note from what I found (there may
be other reasons that I didn't find also)

(this is all from 
What Every Computer Scientist Should Know About Floating-Point Arithmetic
David Goldberg, ACM Computing Surveys, pp. 5-48, vol. 23#1, 1991
http://cch.loria.fr/documentation/IEEE754/ACM/goldberg.pdf
p.21-22 - this is quite an informative paper)

First, there is good reason to follow the IEEE standards for floating point
arithmatic, in that a computation will (if everyone follows the standard, which
AFAIK is true) produce the same results, on every computer, in every language.
The standard defines everything including the algorithms to use.  This is a
VERY good thing.  So even if it seems that the mechanisms of floating point
computation could be improved somehow in ruby, that may not be the best choice.

Second, some reasons for having some special result as opposed to raising an
error condition.  There are quite a few computations (the reference that I
found mentions a program that finds zeros in functions) that will in the course
of execution probably divide zero by zero before completing.  This happens
presumably because the == operator is not defined for floating point numbers
(because they are not real, so their actual value is never known), so the
program can't check for it and avoid the computation.  In the old days this
would've halted execution if 0/0 was illegal.  Now that we have exceptions this
could be done better perhaps, but they weren't too common (if they existed)
when the standard was designed.  In fact, the standard borrowed both the
results of both 0/0 and x/0 from the VAX.  The reasoning may seem outdated but
for reasons described in the first point, but we are stuck with it.

Third, some reasons for there being a difference between NaN and +-Infinity,
consider a situation where f(x)->0 and g(x)->0 as x approaches some limit.
This is an arbitrary situation, and f(x)/g(x) could be anything, depending on
the functions used.  The source I found gives the example of f(x) =sin(x) and
g(x)=x.  f(x)/g(x) -> 1 as x -> 0.  However, if f(x) = 1 - cos(x), f(x)/g(x) ->
0.  So, as Goldberg says, "When thinking of 0/0 as the limiting situation of a
quotient of two very small numbers, 0/0 could represent anything."  So it gets
NaN.

However, if f(x) approaches some value c while g(x) approaches 0, when x is
approaching some value, then the f(x)/g(x) will always approach +-Infinity for
any functions f and g.  So when the denominator is 0 and the numerator is not,
then the FP calculation at least has a consistent value, unlike when both are
0.  The reason for having two infinities is that it is very important to know
the sign of an overflow quantity.  This in turn forces signed zero.


It is true that division by zero is not well defined on the real numbers, but
FP numbers of any type != real numbers.  Also, it maybe be worth noting that
not allowing division by 0 is not universal; it is a well-defined operation on
e.g. the extended complex plane (see
http://mathworld.wolfram.com/DivisionbyZero.html )

-kyle

On Thu, May 30, 2002 at 11:04:30PM +0900, Sean Russell wrote:
> Dossy wrote:
> 
> > Float allowing division by zero is certainly not of least surprise
> > to me.  ;-)
> 
> Yeah, I think it is sort of silly, too.  From what I remember of my math, 
> dividing by zero is "illegal".
> 
> However, IEEE 754 (Institute of Electrical and Electronics Engineers. IEEE 
> Standard for Binary Floating-Point Arithmetic. ANSI/IEEE Std 754-1985.) 
> defines rules for operations such as this, and it specifies that n/0 -> 
> Infinity, -n/0 -> -Infinity, and 0/0 -> NaN.  Because of this, I'm a little 
> surprised that Integer math in Ruby doesn't follow the same rules.
> 
> -- 
>  |..  5 out of 4 people have a problem with fractions.
> <|>   
> /|\   
> /|    
>  |         

-- 
http://mas.cs.umass.edu/~rawlins
--
Space is big
Space is dark
It's hard to find
A place to park.