-
Notifications
You must be signed in to change notification settings - Fork 12
Description
IEEE754_2008 states
7.3 Division by zero 7.3.0
The divideByZero exception shall be signaled if and only if an exact infinite result is defined for an
operation on finite operands. The default result of divideByZero shall be an ∞ correctly signed according to
the operation:
― For division, when the divisor is zero and the dividend is a finite non-zero number, the sign of the
infinity is the exclusive OR of the operands’ signs (see 6.3).
Current Behaviour
my $res;
try {
$res = 42e0/0e0
}
say $!; #Attempt to divide 42 by zero using /
say $!.^name; #X::Numeric::DivideByZero
say $res; #(Any)
Proposed Behaviour (Num, Int and Rat)
Default behaviour is unchanged.
For the operations that can trigger a DBZ ( /, %)....
$*DBZ-Suppress = True;
say 42e0/0e0; #Inf
say -1e0/0e0; #-Inf
say 1e0/-0e0; #-Inf
say -1e0/-0e0; #Inf
say 0e0/0e0; #NaN \ regardless of dividend or divisor sign
say Inf/Inf; #NaN /
Furthermore, this model is adopted by Rat (and FatRat);
$*DBZ-Suppress = True;
#Rat & FatRat
say <42/0>; #Inf
say <-1/0>; #-Inf
say <1/-0> ; #-Inf
say <-1/-0>; #Inf
say <0/0>; #NaN \ regardless of dividend or divisor sign
say <Inf/Inf>; #NaN /
Notes:
- Regardless of dividend magnitude
- <42/0>.nude already correctly gives (1,0) retain this
- In the case of Rat & FatRat, the DBZ is only thrown when the value is used (eg. stringified for output)
- In the case of Int eg
1/0makes a Rat
There is also some valuable insight in this other problem-solving issue.
There has been a previous proposal to do this by Zoffix (includes a pointer to Larry Wall @TimToady comment)
old-design-docs