# Arithmetic

`import { Arithmetic } from "destack:math";`

Explicit overflow behaviors for builtin integer arithmetic.

```ds title="Arithmetic"
export extension Arithmetic {
    checkedAdd(other: T): T | undefined;
    checkedSubtract(other: T): T | undefined;
    checkedMultiply(other: T): T | undefined;
    checkedDivide(other: T): T | undefined;
    checkedRemainder(other: T): T | undefined;
    checkedDivideEuclidean(other: T): T | undefined;
    checkedRemainderEuclidean(other: T): T | undefined;
    checkedNegate(): T | undefined;
    checkedAbs(): T | undefined;
    checkedPower(exponent: uint32): T | undefined;
    checkedShiftLeft(amount: uint32): T | undefined;
    checkedShiftRight(amount: uint32): T | undefined;
    overflowingAdd(other: T): (T, boolean);
    overflowingSubtract(other: T): (T, boolean);
    overflowingMultiply(other: T): (T, boolean);
    overflowingDivide(other: T): (T, boolean);
    overflowingRemainder(other: T): (T, boolean);
    overflowingDivideEuclidean(other: T): (T, boolean);
    overflowingRemainderEuclidean(other: T): (T, boolean);
    overflowingNegate(): (T, boolean);
    overflowingAbs(): (T, boolean);
    overflowingPower(exponent: uint32): (T, boolean);
    overflowingShiftLeft(amount: uint32): (T, boolean);
    overflowingShiftRight(amount: uint32): (T, boolean);
    wrappingAdd(other: T): T;
    wrappingSubtract(other: T): T;
    wrappingMultiply(other: T): T;
    wrappingDivide(other: T): T;
    wrappingRemainder(other: T): T;
    wrappingDivideEuclidean(other: T): T;
    wrappingRemainderEuclidean(other: T): T;
    wrappingNegate(): T;
    wrappingAbs(): T;
    wrappingPower(exponent: uint32): T;
    wrappingShiftLeft(amount: uint32): T;
    wrappingShiftRight(amount: uint32): T;
    saturatingAdd(other: T): T;
    saturatingSubtract(other: T): T;
    saturatingMultiply(other: T): T;
    saturatingNegate(): T;
    saturatingAbs(): T;
    saturatingPower(exponent: uint32): T;
    uncheckedAdd(other: T): T;
    uncheckedSubtract(other: T): T;
    uncheckedMultiply(other: T): T;
    uncheckedDivide(other: T): T;
    uncheckedRemainder(other: T): T;
    uncheckedShiftLeft(amount: uint32): T;
    uncheckedShiftRight(amount: uint32): T;
}
```

[language/library/src/math/arithmetic.ds:6:276](https://github.com/destack-sh/destack/blob/main/language/library/src/math/arithmetic.ds#L6-L276)

## 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.
