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 ) |
longdouble remainderl(longdouble x, longdouble 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 浮点数余数。
如果出现定义域错误,那么返回值由实现定义(受支持的平台上是 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)
内,它对应 unsignedshort ,但 std::remainder(std::rint(x), 65536.0) 在范围 [
-32767.0,
+32768.0)
内,它在 signedshort 的范围外。
额外重载不需要以 (A) 的形式提供。它们只需要能够对它们的第一个实参 num1 和第二个实参 num2 满足以下要求:
| (C++23 前) |
如果 num1 和 num2 具有算术类型,那么 std::remainder(num1, num2) 和 std::remainder(static_cast</*公共浮点数类型*/>(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'; // 特殊值std::cout<<"remainder(-0.0, 1.0) = "<< std::remainder(-0.0, 1)<<'\n'<<"remainder(5.1, Inf) = "<< std::remainder(5.1, INFINITY)<<'\n'; // 错误处理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\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
[编辑]参阅
(C++11) | 计算整数除法的商和余数 (函数) |
(C++11)(C++11) | 浮点数除法运算的余数 (函数) |
(C++11)(C++11)(C++11) | 除法运算的有符号余数和最后三个二进制位 (函数) |
remainder 的 C 文档 |