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:
Policy | Interface | Description |
---|---|---|
Returning Option | CheckedOp | When an overflow occurs in an integer operation, None is returned. |
Saturating | SaturatingOp | If 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 exception | ThrowingOp | When an overflow occurs in an integer operation, an exception is thrown. |
Most significant bit truncation | WrappingOp | When 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
Name | Description |
---|---|
CheckedOp | Returns None when an overflow occurs in an integer operation. |
SaturatingOp | Performs saturation processing when an overflow occurs in an integer operation. |
ThrowingOp | Throws an exception when an overflow occurs in an integer operation. |
WrappingOp | When 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
Name | Description |
---|---|
OvershiftException | Specifies an exception thrown when the number of shifted bits exceeds the number of operand bits in a shift operation. |
UndershiftException | Specifies an exception thrown when the number of shifted bits is less than 0 in a shift operation. |