Skip to content

Implement IEEE P754 Divide By Zero Rules & $*DBZ-Suppress Option #515

@librasteve

Description

@librasteve

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:

  1. Regardless of dividend magnitude
  2. <42/0>.nude already correctly gives (1,0) retain this
  3. In the case of Rat & FatRat, the DBZ is only thrown when the value is used (eg. stringified for output)
  4. In the case of Int eg 1/0 makes 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions