命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | 数值‎ | 数学
 
 
 
常用数学函数
函数
基本操作
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 /*浮点类型*/

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

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

            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-unqualified 浮点类型提供了 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 的 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.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 起)

[编辑] 示例

#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

[编辑] 参阅

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