Re: Divide by 0? Was: Re: Introduction Letter
On Tue, Mar 01, 2005 at 08:13:46AM +, Fergal Daly wrote: 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' I don't know, but I do know that having the interpreter crap out is not helpful to most of us simpletons who find phrases like core dumped not especially user friendly. Maybe if there is a divide by zero you could set one of the cussword variables ([EMAIL PROTECTED]@#$!) in addition to returning undef or inf, whatever. Then if you mathematically correct types don't like the result you can make your own, or go back to crapping out by kill(SIGBUS,$$). I suppose I could try to create a use divide 0/undef/inf/crap pragma. Then you could do whatever you want. You'd still get a surprise if you ever forgot it though.. Austin
Re: Divide by 0? Was: Re: Introduction Letter
Austin Schutz wrote: I suppose I could try to create a use divide 0/undef/inf/crap pragma. Then you could do whatever you want. You'd still get a surprise if you ever forgot it though.. I think that's the best answer. Not a good idea for most programs, wonderful idea for math programs - which can simply say use infinity; or something along those lines at the top. BTW-I'm reading Perl 6 Now, and I believe there was mention there about Perl 6 having support for infinity and possibly other equally strange notions. I'm at work, though, so I don't have access to the book right now. -ofer
Divide by 0? Was: Re: Introduction Letter
On Tue, Mar 01, 2005 at 11:43:31AM +1100, Andrew Savige wrote: running this Perl program: use strict; sub div_by_zero { exec(./a.out $_[0]); die should not be here } defined(my $pid = fork()) or die fork: $!; if ($pid == 0) { warn child, my pid $$\n; div_by_zero(0); # sig 8 # div_by_zero(); # sig 11 exit; } warn parent, my pid $$\n; waitpid($pid, 0); my $rv = $? 8; my $sig = $? 127; warn $$: rv=$rv sig=$sig\n; produces: parent, my pid 12091 child, my pid 12092 12091: rv=0 sig=8 Replacing div_by_zero() above with: sub div_by_zero { 5 / shift } produced: parent, my pid 12133 child, my pid 12134 Illegal division by zero at g2.pl line 2. 12133: rv=255 sig=0 This is not related to the original topic, but I've always wondered this: In math a number divided by 0 is undefined. Why is it that in a language which has an undefined value does the interpreter poop out rather than just having the intuitively obvious behavior of returning undef? Is that really by design, or just a legacy quirk they're afraid to fix? Austin
Re: Divide by 0? Was: Re: Introduction Letter
Austin Schutz wrote: This is not related to the original topic, but I've always wondered this: In math a number divided by 0 is undefined. Why is it that in a language which has an undefined value does the interpreter poop out rather than just having the intuitively obvious behavior of returning undef? Is that really by design, or just a legacy quirk they're afraid to fix? Austin Which would you prefer? $ perl -le '$x=1/0; print $x+1' Illegal division by zero at -e line 1. or $ perl -le '$x=1/0; print $x+1' 1 It only makes sense if undef in any arithmetic operation always gives undef, which means that all variables have to be explicitly initialized to zero before you can perform any arithmetic on them. That breaks tons of useful idioms and generally adds programming overhead. You shouldn't confuse undefined meaning not definable (math) with not yet defined (Perl). David
Re: Divide by 0? Was: Re: Introduction Letter
# The following was supposedly scribed by # David Golden # on Monday 28 February 2005 07:07 pm: Which would you prefer? $ perl -le '$x=1/0; print $x+1' Illegal division by zero at -e line 1. or $ perl -le '$x=1/0; print $x+1' 1 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 --Eric -- Everything should be made as simple as possible, but no simpler. -- Albert Einstein - http://scratchcomputing.com -