#include <math.h> double modf(double y, double *p);
modf
separates an argument of type double
into fractional and
integer parts.
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
.
modf
is implemented as a built-in function unless it is undefined by an
#undef
statement.
#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); }
ceil
, floor
, fmod
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.