Namespaces
Variants

remainder, remainderf, remainderl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
remainder
(C99)
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <math.h>
float remainderf ( float x, float y ) ;
(1) (C99 起)
double remainder ( double x, double y ) ;
(2) (C99 起)
long double remainderl ( long double x, long double y ) ;
(3) (C99 起)
定义于头文件 <tgmath.h>
#define remainder( x, y )
(4) (C99 起)
1-3) 计算浮点除法运算 x / y 的 IEEE 余数。
4) 类型泛型宏:若任一参数类型为 long double ,则调用 remainderl 。否则,若任一参数具有整数类型或类型为 double ,则调用 remainder 。否则调用 remainderf

此函数计算的除法运算 x / y 的IEEE浮点余数,精确等于值 x - n * y ,其中值 n 是最接近精确值 x / y 的整数值。当 |n-x/y| = ½ 时,值 n 取偶数。

fmod() 不同,返回值不保证与 x 具有相同的符号。

如果返回值为 0 ,其符号将与 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 为零,则发生定义域错误。

fmod ,而非 remainder ,可用于将浮点类型静默包装为无符号整数类型: ( 0.0 <= ( y = fmod ( rint ( x ) , 65536.0 ) ) ? y : 65536.0 + y ) 的值位于 [ - 0.0 , 65535.0 ] 范围内,对应 unsigned short 类型;而 remainder ( rint ( x ) , 65536.0 ) 的值位于 [ - 32767.0 , + 32768.0 ] 范围内,该范围超出了 signed short 类型的表示范围。

示例

#include <fenv.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
    printf("remainder(+5.1, +3.0) = %.1f\n", remainder(5.1, 3));
    printf("remainder(-5.1, +3.0) = %.1f\n", remainder(-5.1, 3));
    printf("remainder(+5.1, -3.0) = %.1f\n", remainder(5.1, -3));
    printf("remainder(-5.1, -3.0) = %.1f\n", remainder(-5.1, -3));
    // 特殊值
    printf("remainder(-0.0, 1.0) = %.1f\n", remainder(-0.0, 1));
    printf("remainder(+5.1, Inf) = %.1f\n", remainder(5.1, INFINITY));
    // 错误处理
    feclearexcept(FE_ALL_EXCEPT);
    printf("remainder(+5.1, 0) = %.1f\n", remainder(5.1, 0));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

输出:

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.0
remainder(-0.0, 1.0) = -0.0
remainder(+5.1, Inf) = 5.1
remainder(+5.1, 0) = -nan
    FE_INVALID raised

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.10.2 余数函数 (p: TBD)
  • 7.25 类型泛型数学 <tgmath.h> (p: TBD)
  • F.10.7.2 余数函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.10.2 余数函数 (p: 185-186)
  • 7.25 泛型数学 <tgmath.h> (p: 272-273)
  • F.10.7.2 余数函数 (p: 385)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.10.2 余数函数 (p: 254-255)
  • 7.25 泛型数学 <tgmath.h> (p: 373-375)
  • F.10.7.2 余数函数 (p: 529)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.10.2 余数函数(第235页)
  • 7.22 泛型数学 <tgmath.h>(第335-337页)
  • F.9.7.2 余数函数(第465页)

参见

计算整数除法的商和余数
(函数)
(C99) (C99)
计算浮点除法运算的余数
(函数)
(C99) (C99) (C99)
计算有符号余数以及除法运算的最后三位
(函数)
C++ documentation for remainder