std::fmod, std::fmodf, std::fmodl

来自cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
在标头 <cmath> 定义
(1)
float       fmod (float x, float y );

double      fmod (double x, double y );

longdouble fmod (longdouble x, longdouble y );
(C++23 前)
constexpr/* 浮点数类型 */

            fmod (/* 浮点数类型 */ x,

                   /* 浮点数类型 */ y );
(C++23 起)
float       fmodf(float x, float y );
(2) (C++11 起)
(C++23 起为 constexpr)
longdouble fmodl(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>

            fmod (const V0& v_x, const V1& v_y );
(S) (C++26 起)
额外重载(C++11 起)
在标头 <cmath> 定义
template<class Integer >
double      fmod ( Integer x, Integer y );
(A) (C++23 起为 constexpr)
1-3) 计算除法运算 x / y 的浮点数余数。标准库提供所有以无 cv 限定的浮点数类型作为各形参的类型的 std::fmod 重载。(C++23 起)
S) SIMD 重载对 v_xv_y 实施逐元素 std::fmod
(参见 math-common-simd-t 的定义。)
(C++26 起)
A) 为所有整数类型提供额外重载,将它们当做 double
(C++11 起)

此函数计算的除法 x / y 的浮点数余数是 x - iquot * y 的准确值,其中 iquot 是截断小数部分的 x / y

返回值与 x 拥有相同符号,且绝对值小于 y

目录

[编辑]参数

x, y - 浮点数或整数

[编辑]返回值

在成功时返回在上文定义的除法 x / y 的浮点数余数。

如果出现定义域错误,那么返回值由实现定义(受支持的平台上是 NaN)。

如果出现下溢导致的值域错误,那么返回(舍入后的)正确结果。

[编辑]错误处理

报告 math_errhandling 中指定的错误。

如果 y 为零,那么可能发生定义域错误。

如果实现支持 IEEE 浮点数算术(IEC 60559),那么

  • 如果 x 是 ±0 且 y 非零,那么返回 ±0。
  • 如果 x 是 ±∞ 且 y 非 NaN,那么返回 NaN 并引发 FE_INVALID
  • 如果 y 是 ±0 且 x 非 NaN,那么返回 NaN 并引发 FE_INVALID
  • 如果 y 是 ±∞ 且 x 有限,那么返回 x
  • 如果任一参数是 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 的范围外。

std::fmoddouble 版本表现为如同实现如下:

double fmod(double x, double y){#pragma STDC FENV_ACCESS ONdouble result =std::remainder(std::fabs(x), y =std::fabs(y));if(std::signbit(result)) result += y;returnstd::copysign(result, x);}

当舍入 x / ystd::trunc 的参数损失过多精度时,表达式 x - trunc(x / y)* y 不一定会等于 fmod(x, y)(例如:x =30.508474576271183309y =6.1016949152542370172)。

额外重载不需要以 (A) 的形式提供。它们只需要能够对它们的第一个实参 num1 和第二个实参 num2 满足以下要求:

  • 如果 num1num2 具有 longdouble 类型,那么 std::fmod(num1, num2)std::fmod(static_cast<longdouble>(num1),
              static_cast<longdouble>(num2))
    的效果相同。
  • 否则,如果 num1 和/或 num2 具有 double 或整数类型,那么 std::fmod(num1, num2)std::fmod(static_cast<double>(num1),
              static_cast<double>(num2))
    的效果相同。
  • 否则,如果 num1num2 具有 float 类型,那么 std::fmod(num1, num2)std::fmod(static_cast<float>(num1),
              static_cast<float>(num2))
    的效果相同。
(C++23 前)

如果 num1num2 具有算术类型,那么 std::fmod(num1, num2)std::fmod(static_cast</*公共浮点数类型*/>(num1),
          static_cast</*公共浮点数类型*/>(num2))
的效果相同,其中 /*公共浮点数类型*/num1num2 的类型中浮点数转换等级浮点数转换子等级最高的浮点数类型,整数类型的实参被视为具有与 double 相等的浮点数转换等级。

如果不存在等级和子等级最高的浮点数类型,那么在重载决议时不会从提供的重载中产生可用的候选。

(C++23 起)

[编辑]示例

#include <cfenv>#include <cmath>#include <iostream>// #pragma STDC FENV_ACCESS ON   int main(){std::cout<<"fmod(+5.1, +3.0) = "<< std::fmod(5.1, 3)<<'\n'<<"fmod(-5.1, +3.0) = "<< std::fmod(-5.1, 3)<<'\n'<<"fmod(+5.1, -3.0) = "<< std::fmod(5.1, -3)<<'\n'<<"fmod(-5.1, -3.0) = "<< std::fmod(-5.1, -3)<<'\n';   // 特殊值std::cout<<"fmod(+0.0, 1.0) = "<< std::fmod(0, 1)<<'\n'<<"fmod(-0.0, 1.0) = "<< std::fmod(-0.0, 1)<<'\n'<<"fmod(5.1, Inf) = "<< std::fmod(5.1, INFINITY)<<'\n';   // 错误处理std::feclearexcept(FE_ALL_EXCEPT);std::cout<<"fmod(+5.1, 0) = "<< std::fmod(5.1, 0)<<'\n';if(std::fetestexcept(FE_INVALID))std::cout<<" 发生 FE_INVALID\n";}

可能的输出:

fmod(+5.1, +3.0) = 2.1 fmod(-5.1, +3.0) = -2.1 fmod(+5.1, -3.0) = 2.1 fmod(-5.1, -3.0) = -2.1 fmod(+0.0, 1.0) = 0 fmod(-0.0, 1.0) = -0 fmod(5.1, Inf) = 5.1 fmod(+5.1, 0) = -nan 发生 FE_INVALID

[编辑]参阅

计算整数除法的商和余数
(函数)[编辑]
(C++11)(C++11)(C++11)
除法运算的有符号余数
(函数)[编辑]
(C++11)(C++11)(C++11)
除法运算的有符号余数和最后三个二进制位
(函数)[编辑]
fmod 的 C 文档
close