std::remainder, std::remainderf, std::remainderl
定义于头文件 <cmath> |
||
(1) | ||
float remainder ( float x, float y ); double remainder ( double x, double y ); |
(直至 C++23) | |
constexpr /*浮点类型*/ remainder ( /*浮点类型*/ x, |
(C++23 起) | |
float remainderf( float x, float y ); |
(2) | (C++11 起) (C++23 起为 constexpr) |
long double remainderl( long double x, long double y ); |
(3) | (C++11 起) (C++23 起为 constexpr) |
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) | (C++23 起为 constexpr) |
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 相同。
目录 |
[编辑] 参数
x, y | - | 浮点数或整数值 |
[编辑] 返回值
如果成功,返回如上定义的除法 x / y 的 IEEE 浮点余数。
如果发生域错误,则返回实现定义的值 (支持 NaN 时返回 NaN)。
如果由于下溢导致范围错误,则返回正确结果。
如果 y 为零,但未发生域错误,则返回零。
[编辑] 错误处理
错误按 math_errhandling 指定的方式报告。
如果 y 为零,则可能发生域错误。
如果实现支持 IEEE 浮点运算 (IEC 60559),
- 当前 舍入模式 无效。
- FE_INEXACT 从不引发,结果始终精确。
- 如果 x 为 ±∞ 且 y 不是 NaN,则返回 NaN 并引发 FE_INVALID。
- 如果 y 为 ±0 且 x 不是 NaN,则返回 NaN 并引发 FE_INVALID。
- 如果任一参数为 NaN,则返回 NaN。
[编辑] 注意
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 起) |
[编辑] 示例
#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
[编辑] 参阅
(C++11) |
计算整数除法的商和余数 (函数) |
(C++11)(C++11) |
浮点除法运算的余数 (函数) |
(C++11)(C++11)(C++11) |
带符号余数以及除法运算的最后三位 (函数) |
C 文档 为 remainder
|