Jan Dvorak
1/10/2009 3:16:00 PM
On Saturday 10 January 2009 15:23:12 Raphael Clancy wrote:
> I've had a little time to think on this (and a little sleep ;-D) and I
> realize that I got sidetracked by the precision issue and it really
> isn't what bothers me. I know that precision has been the focus of most
> of the grumping about the IEEE spec, but keeping track of precision is
> part of using a computer. what bothers me it that using the IEEE spec
> causes Ruby to handle division by zero inconsistently and incorrectly.
> While the precision issue is part of the hardware, but the division by
> zero stuff is a design choice. I realize that the IEEE doesn't set out
> these specs arbitrarily and there must be many good reasons for this
> behavior. But, it seems inelegant to me.
It's still coming from the hardware, you pass fdiv(x,0.0) to FPU and you get
floating point number back that is either NaN or Infinity. So the "design
choice" is actually "don't do any additional checking, let the hardware handle
it".
I realize that other script languages are more consisent about throwing
exception, eg. in Python:
>>> 1.0/0.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float division
or Perl:
1.0/0.0
Illegal division by zero at - line 1.
and the same for other operations producing NaN (eg. sqrt(-1)). I don't know
how much of a good idea would be to follow the same way, but if you wish so,
you can always use monkey patching to add the checks you need, eg.:
class Float
alias :olddiv :/
def /(y)
if (y==0)
raise ZeroDivisionError
else
self.olddiv(y)
end
end
end
p 1.0/0.0
$ruby test.rb
test.rb:7:in `/': ZeroDivisionError (ZeroDivisionError)
from test.rb:14
Jan