On Mar 1, 2005, at 12:51 PM, Eric Wilhelm wrote:

Sure.  I was just (sort of) playing devil's advocate by pointing out
that rules are meant to be broken.  If your computer science professor
told you to "never divide by zero" and you blindly followed his advice,
you would be going to a lot more trouble to compare the slopes of
vertical lines than is needed.  (apologies for my previous
transposition of dy for dx)

use bigint;
my @slopes;
foreach my $line (@lines) {
  my @d = map({$line->[1][$_] - $line->[0][$_]} 0..1);
  push(@slopes, $d[1]/$d[0]);
}
my $steepest = (sort({$slopes[$a] <=> $slopes[$b]} 0..$#lines))[-1];

As far as direction goes, in this case it is inherent in the conventions
of "slope".

If you want a robust way to compare whether the slopes of two lines are equal, you shouldn't be dividing at all:


sub slopes_equal {
  my ($line1, $line2) = @_;
  ($line1->[1][0] - $line1->[1][1])*($line2->[0][0] - $line2->[0][1])
    ==
  ($line2->[1][0] - $line2->[1][1])*($line1->[0][0] - $line1->[0][1]);
}

In other words, cross-multiply. The technique can be extended to a slopes_cmp() function in a fairly straightforward way if you want to compare slopes without dividing.

My point goes beyond this example, though: I think the reason you feel like you should be able to divide by zero in this case is that there's a point discontinuity (an exception case) at zero that you feel like should be fairly easy to fill in. Usually the best way to deal with such things, though, is to eliminate that discontinuity in the first place.

Note that some versions of perl actually do support the notion of infinity. Witness perl 5.8.1 on OS X:

% perl -le 'print 2**2560'
inf
% perl -le 'print -2**2560'
-inf
% perl -le 'print 2**2560 / 3**2560'
nan
% perl -le 'print 2/0'
Illegal division by zero at -e line 1.


If you allow positive and negative infinity as part of your numerical system, then division by zero moves from "undefined" to "indeterminate", so I suppose one could argue that division by zero should be 'nan' rather than throwing an exception. Presumably this was some backward-compatibility thing.



If you don't specify the approach direction, the limit is
undefined

Which of course is not equal to undef(). Don't get me wrong, I'm not lobbying for pi==3 here.

Right. If you lobby for making 1/0 be undef(), then in order for it to make any sense you have to make computation with undef() be "viral" like "nan" and not silently convert the undef() to zero. And I think it would surprise an awful lot of people more than exception-on-division-by-zero would. =)




See also http://mathforum.org/dr.math/faq/faq.divideby0.html .  The
explanations get progressively more advanced as you go down the page.

Yes. It's complicated and I'm not advocating that we rewrite the elementary arithmetic books.

Nah, it's not that complicated. I'm just pointing out that the commonly-heard notion that "1/0 is infinity" isn't true unless you want to conflate positive & negative infinity, or restrict all your variables to positive numbers, or whatever.


 -Ken



Reply via email to