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 起)
(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
的重载,作为参数类型。
(since C++23)
|
S)
SIMD 重载对
v_x
和
v_y
执行逐元素的
std::remainder
运算。
|
(since C++26) |
|
A)
为所有整数类型提供了额外的重载,这些类型被视为
double
。
|
(since 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'; // 特殊值 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 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 文档
for
remainder
|
|