menu

Arithmetic

import { Arithmetic } from "destack:math";

Explicit overflow behaviors for builtin integer arithmetic.

language/library/src/math/arithmetic.ds:6:276
1export extension Arithmetic {2    checkedAdd(other: T): T | undefined;3    checkedSubtract(other: T): T | undefined;4    checkedMultiply(other: T): T | undefined;5    checkedDivide(other: T): T | undefined;6    checkedRemainder(other: T): T | undefined;7    checkedDivideEuclidean(other: T): T | undefined;8    checkedRemainderEuclidean(other: T): T | undefined;9    checkedNegate(): T | undefined;10    checkedAbs(): T | undefined;11    checkedPower(exponent: uint32): T | undefined;12    checkedShiftLeft(amount: uint32): T | undefined;13    checkedShiftRight(amount: uint32): T | undefined;14    overflowingAdd(other: T): (T, boolean);15    overflowingSubtract(other: T): (T, boolean);16    overflowingMultiply(other: T): (T, boolean);17    overflowingDivide(other: T): (T, boolean);18    overflowingRemainder(other: T): (T, boolean);19    overflowingDivideEuclidean(other: T): (T, boolean);20    overflowingRemainderEuclidean(other: T): (T, boolean);21    overflowingNegate(): (T, boolean);22    overflowingAbs(): (T, boolean);23    overflowingPower(exponent: uint32): (T, boolean);24    overflowingShiftLeft(amount: uint32): (T, boolean);25    overflowingShiftRight(amount: uint32): (T, boolean);26    wrappingAdd(other: T): T;27    wrappingSubtract(other: T): T;28    wrappingMultiply(other: T): T;29    wrappingDivide(other: T): T;30    wrappingRemainder(other: T): T;31    wrappingDivideEuclidean(other: T): T;32    wrappingRemainderEuclidean(other: T): T;33    wrappingNegate(): T;34    wrappingAbs(): T;35    wrappingPower(exponent: uint32): T;36    wrappingShiftLeft(amount: uint32): T;37    wrappingShiftRight(amount: uint32): T;38    saturatingAdd(other: T): T;39    saturatingSubtract(other: T): T;40    saturatingMultiply(other: T): T;41    saturatingNegate(): T;42    saturatingAbs(): T;43    saturatingPower(exponent: uint32): T;44    uncheckedAdd(other: T): T;45    uncheckedSubtract(other: T): T;46    uncheckedMultiply(other: T): T;47    uncheckedDivide(other: T): T;48    uncheckedRemainder(other: T): T;49    uncheckedShiftLeft(amount: uint32): T;50    uncheckedShiftRight(amount: uint32): T;51}

Members

checkedAdd(other: T): T | undefined

Return this + other, or undefined on overflow.

checkedSubtract(other: T): T | undefined

Return this - other, or undefined on overflow.

checkedMultiply(other: T): T | undefined

Return this * other, or undefined on overflow.

checkedDivide(other: T): T | undefined

Return this / other, or undefined on division by zero or overflow.

checkedRemainder(other: T): T | undefined

Return this % other, or undefined on division by zero or overflow.

checkedDivideEuclidean(other: T): T | undefined

Return the Euclidean quotient, or undefined on division by zero or overflow.

checkedRemainderEuclidean(other: T): T | undefined

Return the Euclidean remainder, or undefined on division by zero or overflow.

checkedNegate(): T | undefined

Return -this, or undefined on overflow.

checkedAbs(): T | undefined

Return the absolute value, or undefined on overflow.

checkedPower(exponent: uint32): T | undefined

Return this ** exponent, or undefined on overflow.

checkedShiftLeft(amount: uint32): T | undefined

Shift left, or undefined when the shift amount is out of range.

checkedShiftRight(amount: uint32): T | undefined

Shift right, or undefined when the shift amount is out of range.

overflowingAdd(other: T): (T, boolean)

Return wrapped this + other with an overflow flag.

overflowingSubtract(other: T): (T, boolean)

Return wrapped this - other with an overflow flag.

overflowingMultiply(other: T): (T, boolean)

Return wrapped this * other with an overflow flag.

overflowingDivide(other: T): (T, boolean)

Return wrapped this / other with an overflow flag.

overflowingRemainder(other: T): (T, boolean)

Return wrapped this % other with an overflow flag.

overflowingDivideEuclidean(other: T): (T, boolean)

Return the wrapped Euclidean quotient with an overflow flag.

overflowingRemainderEuclidean(other: T): (T, boolean)

Return the wrapped Euclidean remainder with an overflow flag.

overflowingNegate(): (T, boolean)

Return wrapped -this with an overflow flag.

overflowingAbs(): (T, boolean)

Return the wrapped absolute value with an overflow flag.

overflowingPower(exponent: uint32): (T, boolean)

Return wrapped this ** exponent with an overflow flag.

overflowingShiftLeft(amount: uint32): (T, boolean)

Shift left with an out-of-range flag.

overflowingShiftRight(amount: uint32): (T, boolean)

Shift right with an out-of-range flag.

wrappingAdd(other: T): T

Return this + other with modular arithmetic.

wrappingSubtract(other: T): T

Return this - other with modular arithmetic.

wrappingMultiply(other: T): T

Return this * other with modular arithmetic.

wrappingDivide(other: T): T

Return this / other, wrapping the one overflowing case.

wrappingRemainder(other: T): T

Return this % other, wrapping the one overflowing case.

wrappingDivideEuclidean(other: T): T

Return the Euclidean quotient, wrapping the one overflowing case.

wrappingRemainderEuclidean(other: T): T

Return the Euclidean remainder, wrapping the one overflowing case.

wrappingNegate(): T

Return -this with modular arithmetic.

wrappingAbs(): T

Return the absolute value with modular arithmetic.

wrappingPower(exponent: uint32): T

Return this ** exponent with modular arithmetic.

wrappingShiftLeft(amount: uint32): T

Shift left with a masked shift amount.

wrappingShiftRight(amount: uint32): T

Shift right with a masked shift amount.

saturatingAdd(other: T): T

Return this + other, clamping at the bounds.

saturatingSubtract(other: T): T

Return this - other, clamping at the bounds.

saturatingMultiply(other: T): T

Return this * other, clamping at the bounds.

saturatingNegate(): T

Return -this, clamping at the bounds.

saturatingAbs(): T

Return the absolute value, clamping at the bounds.

saturatingPower(exponent: uint32): T

Return this ** exponent, clamping at the bounds.

uncheckedAdd(other: T): T

Add without overflow checks.

uncheckedSubtract(other: T): T

Subtract without overflow checks.

uncheckedMultiply(other: T): T

Multiply without overflow checks.

uncheckedDivide(other: T): T

Divide without division checks.

uncheckedRemainder(other: T): T

Return a remainder without division checks.

uncheckedShiftLeft(amount: uint32): T

Shift left without checking the shift amount.

uncheckedShiftRight(amount: uint32): T

Shift right without checking the shift amount.