[numeric.sat]

# 26 Algorithms library [[algorithms]](./#algorithms)

## 26.10 Generalized numeric operations [[numeric.ops]](numeric.ops#numeric.sat)

### 26.10.17 Saturation arithmetic [numeric.sat]

#### [26.10.17.1](#func) Arithmetic functions [[numeric.sat.func]](numeric.sat.func)

[1](#func-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13392)

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[.](#func-1.sentence-1)

[ð](#lib:add_sat)

`template<class T>
  constexpr T add_sat(T x, T y) noexcept;
`

[2](#func-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13404)

*Constraints*: T is a signed or unsigned integer type ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types"))[.](#func-2.sentence-1)

[3](#func-3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13408)

*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[.](#func-3.sentence-1)

[ð](#lib:sub_sat)

`template<class T>
  constexpr T sub_sat(T x, T y) noexcept;
`

[4](#func-4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13422)

*Constraints*: T is a signed or unsigned integer type ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types"))[.](#func-4.sentence-1)

[5](#func-5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13426)

*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[.](#func-5.sentence-1)

[ð](#lib:mul_sat)

`template<class T>
  constexpr T mul_sat(T x, T y) noexcept;
`

[6](#func-6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13440)

*Constraints*: T is a signed or unsigned integer type ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types"))[.](#func-6.sentence-1)

[7](#func-7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13444)

*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[.](#func-7.sentence-1)

[ð](#lib:div_sat)

`template<class T>
  constexpr T div_sat(T x, T y) noexcept;
`

[8](#func-8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13458)

*Constraints*: T is a signed or unsigned integer type ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types"))[.](#func-8.sentence-1)

[9](#func-9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13462)

*Preconditions*: y != 0 is true[.](#func-9.sentence-1)

[10](#func-10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13466)

*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[.](#func-10.sentence-1)

[11](#func-11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13472)

*Remarks*: A function call expression
that violates the precondition in the *Preconditions* element
is not a core constant expression ([[expr.const]](expr.const "7.7 Constant expressions"))[.](#func-11.sentence-1)

#### [26.10.17.2](#cast) Casting [[numeric.sat.cast]](numeric.sat.cast)

[ð](#lib:saturate_cast)

`template<class R, class T>
  constexpr R saturate_cast(T x) noexcept;
`

[1](#cast-1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13488)

*Constraints*: R and T are signed or unsigned integer types ([[basic.fundamental]](basic.fundamental "6.9.2 Fundamental types"))[.](#cast-1.sentence-1)

[2](#cast-2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13492)

*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[.](#cast-2.sentence-1)
