- numeric[meta header]
- function template[meta id-type]
- std[meta namespace]
- cpp26[meta cpp]
namespace std {
template<class T>
constexpr T div_sat(T x, T y) noexcept;
}
飽和除算 x / y
を計算する。
T
は符号付き整数型または符号無し整数型であること。
y != 0
T
が符号付き整数型かつx ==
numeric_limits<T>::min()
&& y == -1
のとき、numeric_limits<T>::max()
を返す- そうでなければ、
x / y
を返す
投げない
事前条件を満たすこと
#include <cstdint>
#include <numeric>
#include <print>
int main()
{
// 10 / 3 = 3
std::println("{}", std::div_sat(10, 3));
// -128 * -1 = 128 -> 127(2**7-1)
std::int8_t x = -128, y = -1;
std::println("{}", std::div_sat(x, y));
}
- std::div_sat[color ff0000]
- std::println[link /reference/print/println.md]
3
127
- C++26
- Clang: ??
- GCC: ??
- ICC: ??
- Visual C++: ??