The modf
function breaks a double
argument into integral and fractional parts; for example, given 3.75
it returns 0.75
and stores 3.0
in the object pointed to by its second argument.
The question is, what should happen if you call it with a value that's too big to fit in any integer type?
If it returned an int
result, or even a long long
or intmax_t
result, it would have to deal with overflow somehow, which would likely require adding an extra parameter to distinguish valid results from overflows.
By returning a double
result, overflow is not possible; for very large arguments, it can just return the argument value and set the fractional part to 0.0
. It simplifies the function considerably. (If you want to convert the result to an integer you can do so -- but you should check the result against the bounds of the integer type you're using.)
On modern systems double
is typically 64 bits, and can represent integers up to about 253 exactly. If you call modf
with a value greater than 253, then the double
value itself can't necessarily hold an exact integer value; having modf
return even a 64-bit integer wouldn't provide any extra precision.
A long double
, depending on the implementation, might be able to hold a wider range of exact integer values than even the widest integer type; on such a system, making modfl
return an integer would lose precision relative to having it return long double
.
So having modf
(and modff
and modfl
) return an integer rather than a floating-point value would lose range without any corresponding gain in precision.
double
is not inherently less precise thanint
. In a typical case,double
can represent every integer in the range -2e53..2e53 precisely.