命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本运算
remainder
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指数函数
(C++11)
(C++11)

(C++11)
(C++11)
幂函数
(C++11)
(C++11)
三角函数
双曲函数
(C++11)
(C++11)
(C++11)

误差和伽马函数
(C++11)
(C++11)
(C++11)
(C++11)
最近整数浮点运算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮点数操作函数
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分类和比较
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型
(C++11)
(C++11)
(C++11)
宏常量
分类
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定义于头文件 <cmath>
(1)
float       remainder ( float x, float y );

double      remainder ( double x, double y );

long double remainder ( long double x, long double y );
(直到 C++23)
constexpr /*floating-point-type*/

            remainder ( /*floating-point-type*/ x,

                        /*floating-point-type*/ y );
(自 C++23 起)
float       remainderf( float x, float y );
(2) (自 C++11 起)
(constexpr 自 C++23 起)
long double remainderl( long double x, long double y );
(3) (自 C++11 起)
(constexpr 自 C++23 起)
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) (constexpr 自 C++23 起)
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 具有相同的符号。

内容

[edit] 参数

x, y - 浮点值或整数值

[edit] 返回值

如果成功,则返回除法 x / y 的 IEEE 浮点余数,如上定义。

如果发生域错误,则返回实现定义的值(如果支持,则为 NaN)。

如果由于下溢而发生范围错误,则返回正确的结果。

如果 y 为零,但未发生域错误,则返回零。

[edit] 错误处理

错误按照 math_errhandling 中的规定报告。

如果 y 为零,则可能发生域错误。

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

  • 当前的 舍入模式 无效。
  • FE_INEXACT 永不引发,结果始终是精确的。
  • 如果 x 是 ±∞ 且 y 不是 NaN,则返回 NaN 并引发 FE_INVALID
  • 如果 y 是 ±0 且 x 不是 NaN,则返回 NaN 并引发 FE_INVALID
  • 如果任一参数是 NaN,则返回 NaN。

[edit] 注解

POSIX 要求,如果 x 是无穷大或 y 为零,则会发生域错误。

std::fmod,而不是 std::remainder,对于将浮点类型静默包装为无符号整数类型很有用:(0.0 <= (y = std::fmod(std::rint(x), 65536.0))) ? y : 65536.0 + y 在范围 [-0.065535.0] 内,这对应于 unsigned short,但是 std::remainder(std::rint(x), 65536.0) 在范围 [-32767.0+32768.0] 内,这超出了 signed short 的范围。

额外的重载不需要完全按照 (A) 的形式提供。它们只需要足以确保对于它们的第一个参数 num1 和第二个参数 num2

  • 如果 num1num2 的类型为 long double,则 std::remainder(num1, num2) 具有与 std::remainder(static_cast<long double>(num1),
                   static_cast<long double>(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</*common-floating-point-type*/>(num1),
               static_cast</*common-floating-point-type*/>(num2))
相同的效果,其中 /*common-floating-point-type*/ 是浮点类型,在 浮点转换等级浮点转换子等级 中具有最高的级别和子级别,介于 num1num2 的类型之间,整数类型的参数被认为具有与 double 相同的浮点转换等级。

如果没有具有最高级别和子级别的浮点类型,则 重载解析 不会导致从提供的重载中产生可用的候选项。

(自 C++23 起)

[edit] 示例

#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

[edit] 参见

计算整数除法的商和余数
(函数) [编辑]
(C++11)(C++11)
浮点除法运算的余数
(函数) [编辑]
(C++11)(C++11)(C++11)
带符号的余数以及除法运算的最后三位
(函数) [编辑]
C 文档 关于 remainder