std::remainder, std::remainderf, std::remainderl
定义于头文件 <cmath> |
||
(1) | ||
float remainder ( float x, float y ); double remainder ( double x, double y ); |
(直到 C++23) | |
constexpr /*floating-point-type*/ remainder ( /*floating-point-type*/ x, |
(自 C++23 起) | |
float remainderf( float x, float y ); |
(2) | (自 C++11 起) (constexpr 自 C++23 起) |
long double remainderl( long double x, long double y ); |
(3) | (自 C++11 起) (constexpr 自 C++23 起) |
SIMD 重载 (自 C++26 起) |
||
定义于头文件 <simd> |
||
template< class V0, class V1 > constexpr /*math-common-simd-t*/<V0, V1> |
(S) | (自 C++26 起) |
额外的重载 (自 C++11 起) |
||
定义于头文件 <cmath> |
||
template< class Integer > double remainder ( Integer x, Integer y ); |
(A) | (constexpr 自 C++23 起) |
std::remainder
的重载作为参数类型。(自 C++23 起)
S) SIMD 重载对 v_x 和 v_y 执行元素级的
std::remainder 。
|
(自 C++26 起) |
A) 为所有整数类型提供了额外的重载,它们被视为 double。
|
(自 C++11 起) |
此函数计算的除法运算 x / y 的 IEEE 浮点余数精确地是值 x - quo * y,其中值 quo 是最接近精确值 x / y 的整数值。当 |quo - x / y| = ½ 时,选择偶数值作为 quo。
与 std::fmod 相比,不保证返回值的符号与 x 相同。
如果返回值是零,则它将与 x 具有相同的符号。
内容 |
[edit] 参数
x, y | - | 浮点值或整数值 |
[edit] 返回值
如果成功,则返回除法 x / y 的 IEEE 浮点余数,如上定义。
如果发生域错误,则返回实现定义的值(如果支持,则为 NaN)。
如果由于下溢而发生范围错误,则返回正确的结果。
如果 y 为零,但未发生域错误,则返回零。
[edit] 错误处理
错误按照 math_errhandling 中的规定报告。
如果 y 为零,则可能发生域错误。
如果实现支持 IEEE 浮点算术 (IEC 60559),
- 当前的 舍入模式 无效。
- FE_INEXACT 永不引发,结果始终是精确的。
- 如果 x 是 ±∞ 且 y 不是 NaN,则返回 NaN 并引发 FE_INVALID。
- 如果 y 是 ±0 且 x 不是 NaN,则返回 NaN 并引发 FE_INVALID。
- 如果任一参数是 NaN,则返回 NaN。
[edit] 注解
POSIX 要求,如果 x 是无穷大或 y 为零,则会发生域错误。
std::fmod,而不是 std::remainder
,对于将浮点类型静默包装为无符号整数类型很有用:(0.0 <= (y = std::fmod(std::rint(x), 65536.0))) ? y : 65536.0 + y 在范围 [
-0.0,
65535.0]
内,这对应于 unsigned short,但是 std::remainder(std::rint(x), 65536.0) 在范围 [
-32767.0,
+32768.0]
内,这超出了 signed short 的范围。
额外的重载不需要完全按照 (A) 的形式提供。它们只需要足以确保对于它们的第一个参数 num1 和第二个参数 num2
|
(直到 C++23) |
如果 num1 和 num2 具有算术类型,则 std::remainder(num1, num2) 具有与 std::remainder(static_cast</*common-floating-point-type*/>(num1), 如果没有具有最高级别和子级别的浮点类型,则 重载解析 不会导致从提供的重载中产生可用的候选项。 |
(自 C++23 起) |
[edit] 示例
#include <cfenv> #include <cmath> #include <iostream> // #pragma STDC FENV_ACCESS ON int main() { std::cout << "remainder(+5.1, +3.0) = " << std::remainder(5.1, 3) << '\n' << "remainder(-5.1, +3.0) = " << std::remainder(-5.1, 3) << '\n' << "remainder(+5.1, -3.0) = " << std::remainder(5.1, -3) << '\n' << "remainder(-5.1, -3.0) = " << std::remainder(-5.1, -3) << '\n'; // special values std::cout << "remainder(-0.0, 1.0) = " << std::remainder(-0.0, 1) << '\n' << "remainder(5.1, Inf) = " << std::remainder(5.1, INFINITY) << '\n'; // error handling std::feclearexcept(FE_ALL_EXCEPT); std::cout << "remainder(+5.1, 0) = " << std::remainder(5.1, 0) << '\n'; if (fetestexcept(FE_INVALID)) std::cout << " FE_INVALID raised\n"; }
可能的输出
remainder(+5.1, +3.0) = -0.9 remainder(-5.1, +3.0) = 0.9 remainder(+5.1, -3.0) = -0.9 remainder(-5.1, -3.0) = 0.9 remainder(-0.0, 1.0) = -0 remainder(5.1, Inf) = 5.1 remainder(+5.1, 0) = -nan FE_INVALID raised
[edit] 参见
(C++11) |
计算整数除法的商和余数 (函数) |
(C++11)(C++11) |
浮点除法运算的余数 (函数) |
(C++11)(C++11)(C++11) |
带符号的余数以及除法运算的最后三位 (函数) |
C 文档 关于 remainder
|