Namespaces
Variants

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

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <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 起)
(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 未限定浮点类型提供 std::remainder 的重载,作为参数类型。 (since C++23)
S) SIMD 重载对 v_x v_y 执行逐元素的 std::remainder 运算。
(其定义参见 math-common-simd-t 。)
(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 满足以下条件:

  • num1 num2 具有类型 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 ) )
  • 否则,若 num1 num2 具有类型 float ,则 std :: remainder ( num1, num2 ) 的效果等同于 std :: remainder ( static_cast < float > ( num1 ) ,
    static_cast < float > ( num2 ) )
(C++23 前)

num1 num2 具有算术类型,则 std :: remainder ( num1, num2 ) 的效果等同于 std :: remainder ( static_cast < /*common-floating-point-type*/ > ( num1 ) ,
static_cast < /*common-floating-point-type*/ > ( num2 ) )
,其中 /*common-floating-point-type*/ num1 num2 类型之间具有最高 浮点转换等级 和最高 浮点转换子等级 的浮点类型,整数类型的实参被认为具有与 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 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 文档 for remainder