modf -- Floating-Point Conversion: Fraction-Integer Split


SYNOPSIS
#include <math.h>
double modf(double y, double *p);
DESCRIPTION
modf
separates an argument of type double
into fractional and
integer parts.
RETURN VALUE
modf
returns the fractional part of the argument y
with the same
sign as y
. The integer part of y
, expressed as a floating-point
number, is stored in the location referenced by the pointer p
.
IMPLEMENTATION
modf
is implemented as a built-in function unless it is undefined by an
#undef
statement.
EXAMPLE
#include <math.h>
#include <stdio.h>
main()
{
double weight;
double intweight;
float limit = 0.5;
puts("Enter a weight");
scanf("%lf",&weight);
/* Check to see if weight is negative. */
if (weight < 0) {
puts("Weight can not be a negative number");
exit(1);
}
/* Test whether fractional part equals or exceeds limit. */
if (modf(weight, &intweight) >= limit)
weight = intweight + 1; /* If yes, add 1 to weight. */
else
weight = intweight; /* Otherwise, round down weight. */
printf("Your weight rounded off to the nearest pound is %fn",
weight);
}
RELATED FUNCTIONS
ceil
, floor
, fmod
SEE ALSO
Mathematical Functions