fmod, fmodf, fmodl

计算浮点余数。

语法

double fmod(
   double x,
   double y
);
float fmod(
   float x,
   float y
);  // C++ only
long double fmod(
   long double x,
   long double y
);  // C++ only
float fmodf(
   float x,
   float y
);
long double fmodl(
   long double x,
   long double y
);

#define fmod(X, Y) // Requires C11 or higher

参数

xy
浮点值。

返回值

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

备注

fmod 函数计算 x / y 的浮点余数 f,以便 x = i * y + f(其中 i 是一个整数),f 具有与 x 相同的符号,且 f 的绝对值小于 y 的绝对值。

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

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

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

要求

函数 必需的标头
fmod, fmodf, fmodl <math.h>
fmod <tgmath.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_fmod.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 = fmod( w, x );
   printf( "The remainder of %.2f / %.2f is %f\n", w, x, z );
}
The remainder of -10.00 / 3.00 is -1.000000

另请参阅

数学和浮点支持
ceilceilfceill
fabsfabsffabsl
floorfloorffloorl
_CIfmod