std::remainder, std::remainderf, std::remainderl

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

double      remainder (double x, double y );

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

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

                        /* 浮点数类型 */ y );
(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>

            remainder (const V0& v_x, const V1& v_y );
(S) (C++26 起)
额外重载(C++11 起)
在标头 <cmath> 定义
template<class Integer >
double      remainder ( Integer x, Integer y );
(A) (C++23 起为 constexpr)
1-3) 计算浮点数除法运算 x / y 的 IEEE 余数。标准库提供所有以无 cv 限定的浮点数类型作为各形参的类型的 std::remainder 重载。(C++23 起)
S) SIMD 重载对 v_xv_y 实施逐元素 std::remainder
(参见 math-common-simd-t 的定义。)
(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.065535.0) 内,它对应 unsignedshort ,但 std::remainder(std::rint(x), 65536.0) 在范围 [-32767.0+32768.0) 内,它在 signedshort 的范围外。

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

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

如果 num1num2 具有算术类型,那么 std::remainder(num1, num2)std::remainder(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<<"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)
除法运算的有符号余数和最后三个二进制位
(函数)[编辑]
remainder 的 C 文档
close