modf, modff, modfl

将浮点值拆分为小数部分和整数部分。

语法

double modf( double x, double * intptr );
float modff( float x, float * intptr );
long double modfl( long double x, long double * intptr );
float modf( float x, float * intptr );  // C++ only
long double modf( long double x, long double * intptr );  // C++ only

参数

x
浮点值。

intptr
指向存储整数部分的指针。

返回值

此函数返回 x 的有符号小数部分。 无错误返回。

注解

modf 函数将浮点值 x 拆分为小数和整数部分,每个部分的符号与 x 相同。 返回 x 的有符号小数部分。 整数部分作为浮点值存储在 intptr 中。

modf 具有使用流式处理 SIMD 扩展 2 (SSE2) 的实现。 有关使用 SSE2 实现的信息和限制,请参阅_set_SSE2_enable

C++ 允许重载,因此可以调用采用并返回 floatlong double 参数的 modf 重载。 在 C 程序中,modf 始终采用两个双精度值并返回一个双精度值。

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

要求

例程 必需的标头
modf, modff, modfl C: <math.h>

C++: , <cmath><math.h>

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

示例

// crt_modf.c

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

int main( void )
{
   double x, y, n;

   x = -14.87654321;      /* Divide x into its fractional */
   y = modf( x, &n );     /* and integer parts            */

   printf( "For %f, the fraction is %f and the integer is %.f\n",
           x, y, n );
}
For -14.876543, the fraction is -0.876543 and the integer is -14

另请参阅

数学和浮点支持
frexp
ldexp