On Mon, Feb 28, 2005 at 07:55:36PM -0600, Eric Wilhelm wrote:
> I like the one where you get the mathematically-correct (or at least 
> mathematically-useful) infinity.
> 
>   $perl -le 'use bigint; $x = 1/0; print $x+1'
>   inf
> 
>   $perl -le 'use bigint; $x = 1/0; print 1/$x'
>   0

and what should these print?

$perl -le 'use bigint; $x = 2/0; print $x*0'

$perl -le 'use bigint; $x = 1/0; print ($x+1)-$x'

$perl -le 'use bigint; $x = 1/0; print ($x*2)/$x'


Allowing inf might make some sense in an interactive calculator but it's a
bad idea in a programming langague.

For example there's no way to evaluate $x/$y when both are inf or even $x *
0 when $x is inf. Perl would have to die with an Inf error. It's bad enough
trying to find the real source of a divide by zero error but for an Inf
error you might have to find 2 occurrences of divide by zero, figure out
which one is wrong and then go fix a divide by zero error. It's also
possible that neither of them is wrong and that both infs are supposed to be
there in which you have a big refactoring job ahead.

There is actually a system of arithmetic which allows infinitely large and
small values (it provides an infinite number of infinitely large/small
numbers) and produces "sensible" results for any expression, no matter if
the values involved are finite or inifintely large/small. In this system (x
* 2) / x is always 2 for any x, except x = 0 - even in this system division
by zero is not allowed.

http://mathforum.org/dr.math/faq/analysis_hyperreals.html

has a good explanation.

F

Reply via email to