remainder, remainderf, remainderl

计算两个浮点值的商的余数(四舍五入到最接近的整数值)。

语法

double remainder( double x, double y );
float remainderf( float x, float y );
long double remainderl( long double x, long double y );
#define remainder(X, Y) // Requires C11 or higher

float remainder( float x, float y ); /* C++ only */
long double remainder( long double x, long double y ); /* C++ only */

参数

x
分子。

y
分母。

返回值

x / y 的浮点余数。 如果 y 的值为 0.0,则 remainder 返回静态 NaN。 有关 printf 系列表现静态 NaN 形式的信息,请参阅 printf_printf_lwprintf_wprintf_l

注解

remainder 函数计算 x / y 的浮点余数 r,以便 x = n * y + r,其中 n 是值中最接近 x / y 的整数,而 n|n - x / y| = 1/2 时为偶数。 当 r = 0 时,rx 的符号相同。

由于 C++ 允许重载,因此你可以调用采用并返回 remainderfloat 值的 long double 重载。 在 C 程序中,除非使用 <tgmath.h> 宏调用此函数,否则 remainder 始终采用两个 double 参数并返回 double

如果使用 <tgmath.h>remainder() 宏,则参数的类型将决定选择哪个版本的函数。 有关详细信息,请参阅泛型类型数学

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

要求

功能 必需的标头 (C) 必需的标头 (C++)
remainder, remainderf, remainderl <math.h> <cmath> 或 <math.h>
remainder <tgmath.h>

有关兼容性信息,请参阅兼容性

示例

// crt_remainder.c
// This program displays a floating-point remainder.

#include <math.h>
#include <stdio.h>

int main( void )
{
   double w = -10.0, x = 3.0, z;

   z = remainder(w, x);
   printf("The remainder of %.2f / %.2f is %f\n", w, x, z);
}
The remainder of -10.00 / 3.00 is -1.000000

另请参阅

数学和浮点支持
ldivlldiv
imaxdiv
fmodfmodf
remquoremquofremquol