# 27 Algorithms library [algorithms]

## 27.10 Generalized numeric operations [numeric.ops]

### 27.10.17 Saturation arithmetic [numeric.sat]

#### 27.10.17.1 Arithmetic functions [numeric.sat.func]

In the following descriptions, an arithmetic operation is performed as a mathematical operation with infinite range and then it is determined whether the mathematical result fits into the result type.
```template<class T> constexpr T add_sat(T x, T y) noexcept; ```
Constraints: T is a signed or unsigned integer type ([basic.fundamental]).
Returns: If is representable as a value of type T, ; otherwise, either the largest or smallest representable value of type T, whichever is closer to the value of .
```template<class T> constexpr T sub_sat(T x, T y) noexcept; ```
Constraints: T is a signed or unsigned integer type ([basic.fundamental]).
Returns: If is representable as a value of type T, ; otherwise, either the largest or smallest representable value of type T, whichever is closer to the value of .
```template<class T> constexpr T mul_sat(T x, T y) noexcept; ```
Constraints: T is a signed or unsigned integer type ([basic.fundamental]).
Returns: If x ×y is representable as a value of type T, x ×y; otherwise, either the largest or smallest representable value of type T, whichever is closer to the value of x ×y.
```template<class T> constexpr T div_sat(T x, T y) noexcept; ```
Constraints: T is a signed or unsigned integer type ([basic.fundamental]).
Preconditions: y != 0 is true.
Returns: If T is a signed integer type and x == numeric_limits<T>​::​min() && y == -1 is true, numeric_limits<T>​::​max(), otherwise, x / y.
Remarks: A function call expression that violates the precondition in the Preconditions element is not a core constant expression ([expr.const]).

#### 27.10.17.2 Casting [numeric.sat.cast]

```template<class R, class T> constexpr R saturate_cast(T x) noexcept; ```
Constraints: R and T are signed or unsigned integer types ([basic.fundamental]).
Returns: If x is representable as a value of type R, x; otherwise, either the largest or smallest representable value of type R, whichever is closer to the value of x.