Skip to content

[RFC] Supporting checked arithmetic for division-related operations #221

@kimikage

Description

@kimikage

While there is room for some improvement and discussion, I have prepared the implementations for checked arithmetic on addition and multiplication (cf. #41 (comment)).

However, the circumstances surrounding division-related operations are more complicated.

Checked arithmetic for /

Since the result of the / with integer arguments is a floating point number, the checked version of / is not defined in Base.Checked. Unlike * and mul, / and div are different operations. So, checked_div is for div(÷) and not for /.
Shall we define {checked/saturating/wrapping}_fdiv? ("f" stands for "fractional", not "float" 😄).

Default arithmetic

Unlike addition and subtraction, the operations involved in integer division are basically "checked" arithmetic. It is not clear whether we should follow the integer style or the floating point style (which does not throw an exception). However, I personally think the checked arithmetic is a better choice for the default, since FixedPoint numbers cannot represent Inf or NaN.

Division by zero in wrapping arithmetic

There are several options for the result of dividing by zero. Considering the distinction from saturating arithmetic, I think it is a good idea to return zero.

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