In C++, std::modf(x, &whole)splits a number in two: the whole part and the fractional part. For 3.75 the whole part is 3 and the fractional part is 0.75.
The function returns the fractional part and stores the whole part in a variable that you pass by address. Both parts carry the sign of the original number, so -3.75 gives -3 and -0.75. Both values are double.
std::modf() is declared in <cmath> and is part of the group Rounding. Rounding functions turn a fractional number into a whole number. They differ in which way they go for negative numbers and for ties such as 2.5.
Syntax
Include <cmath> and call std::modf. Start with these lines.
For -0.5 the whole part is zero, but it keeps the minus sign of the input. It prints as -0, and the fraction is -0.5.
For infinity, the whole part is infinity itself, and the fraction is 0. An infinite number has no decimal part.
For NaN, both the returned fraction and the stored whole part are NaN. std::isnan() prints 1 for each of them.
Frequently Asked Questions
std::modf(x, &whole) splits x into two parts. It returns the fractional part and stores the whole part in the variable you give it. Both parts keep the sign of x.
The fractional part of x, with the same sign as x. The whole part is stored through the pointer argument.
It returns the fractional part of the number. The whole part is stored in the variable that the second argument points to. Both parts have the same sign as the input.
modf() splits a number into whole and fractional parts. fmod(x, y) gives the remainder of x divided by y. modf(x, &w) is like fmod(x, 1.0) for the fraction, but it also gives you the whole part.
Include <cmath> and write std::modf(). That always works and picks the C++ overloads. The global name ::modf often works too, but the standard does not promise it, and it may only take a double.