modf()
Published Dec 13, 2022
Contribute to Docs
The modf()
function splits a given number into an integral and a fractional part.
Syntax
modf(number, integralPart)
The modf()
function takes two parameters:
number
, a floating point value (double
,float
orlong double
) to split into integral and fractional parts.integralPart
, a pointer tonumber
that stores the integral part. It will have the same type and sign asnumber
.
The modf()
function returns the fractional part of number
with the same sign as number
.
Example
This example uses the modf()
function to return the fractional part of 6.02214
:
#include <iostream>#include <cmath>int main() {double num1 = 6.02214;double intPart;double fractPart;fractPart = modf(num1, &intPart);std::cout << num1 << " = " << intPart << " + " << fractPart << "\n";return 0;}
This example results in the following output:
6.02214 = 6 + 0.02214
Codebyte Example
This example uses the modf()
function to take an argument of 1.602176634
and return its fractional part while also storing the value of the integral part of the argument into integral
:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.