C++ cmath modf() Function
Example
Split the number 3.75 into an integer part and a decimal part:
double intPart;
double decPart = modf(3.75, &intPart);
cout << intPart << "\n";
cout << decPart;
Definition and Usage
The modf() function splits a number into an integer part and a decimal part. Both parts have the same sign as x.
The function returns the decimal part, and writes the integer part to the double that y points to. y is a pointer (it stores the memory address of a variable). In the example, &intPart gives the memory address of intPart, so the function can change it.
The modf function is defined in the <cmath> header file.
Tip: To get only the integer part, you can also use trunc().
Syntax
modf(double x, double* y)
The function also works with float and long double values. Then y must point to a variable of the same type.
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The number to split. |
| y | Required. A pointer to a double. The integer part is written to this double. |
Technical Details
| Returns: | A double value with the decimal part of x. |
|---|
Related Pages
Read more about math in our Math Tutorial.