std.overflow Package

Function Description

The overflow package provides the capability of processing overflows in integer operations.

In an integer operation, an overflow occurs if the operation result is greater than the maximum value of a type or less than the minimum value of a type. By default, an exception is thrown when an overflow occurs.

The overflow package provides four overflow processing policies and defines corresponding interfaces as follows:

PolicyInterfaceDescription
Returning OptionCheckedOpWhen an overflow occurs in an integer operation, None is returned.
SaturatingSaturatingOpIf the operation result is greater than the MAX value of the target type, the MAX value is returned. If the operation result is less than the MIN value of the target type, the MIN value is returned.
Throwing exceptionThrowingOpWhen an overflow occurs in an integer operation, an exception is thrown.
Most significant bit truncationWrappingOpWhen an overflow occurs in an integer operation, the most significant bits exceeding the number of bits of the target type in the operation result are truncated.

The overflow package implements these interfaces for all integer types through extension. Users can implement the overflow interfaces for other types in the same way.

API List

Interface

NameDescription
CheckedOpReturns None when an overflow occurs in an integer operation.
SaturatingOpPerforms saturation processing when an overflow occurs in an integer operation.
ThrowingOpThrows an exception when an overflow occurs in an integer operation.
WrappingOpWhen an overflow occurs in an integer operation, the most significant bits exceeding the number of bits of the target type in the operation result are truncated.

Exception Class

NameDescription
OvershiftExceptionSpecifies an exception thrown when the number of shifted bits exceeds the number of operand bits in a shift operation.
UndershiftExceptionSpecifies an exception thrown when the number of shifted bits is less than 0 in a shift operation.