6.11.130 modf Function

Splits a double precision floating-point value into fractional and integer parts.

Include

<math.h>

Prototype

double modf(double x, double * pint);

Arguments

x
double precision floating-point value
pint
pointer to where the integer part should be stored

Return Value

Returns the signed fractional part and pint points to the integer part.

Remarks

The absolute value of the fractional part is in the range of 0 (inclusive) to 1 (exclusive). No domain or range error will occur.

Example

See the notes at the beginning of this chapter or section for information on using printf() or scanf() (and other functions reading and writing the stdin or stdout streams) in the example code.

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

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

  x = 0.707;
  y = modf(x, &n);
  printf("For %f the fraction is %f\n ", x, y);
  printf("  and the integer is %0.f\n\n", n);

  x = -15.2121;
  y = modf(x, &n);
  printf("For %f the fraction is %f\n ", x, y);
  printf("  and the integer is %0.f\n\n", n);
}

Example Output

For 0.707000 the fraction is 0.707000
   and the integer is 0

For -15.212100 the fraction is -0.212100
   and the integer is -15