modf() in C/C++

18 Apr, 2018
In C++, modf() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions. All the functions available in this library take double as an argument and return double as the result.
modf() function breaks the given argument into two parts, one is integer and the other one is fractional. Integer part is stored in the memory address pointed by the pointer which is passed as second argument in the function and the fractional part is returned by the function.
Syntax:
double modf(k, &p)

Parameters:
k: It is value which is going to be breaked into two parts.
p: It is pointer which points to the integer part of k after breaking.
Return:
The function returns the fractional value of k.
Error:
No particular error raised by this function.
Exceptions associated with this function:
If anything other then a float, double or integer number is passed, it returns a type error.// CPP program to demonstrate// exception of this function#include <bits/stdc++.h>using namespace std;  int main(){    // Take any value    double p, fraction;    string k = "5.06";      // Breaks k into two parts    fraction = modf(k, &p);      cout << "Integer Value = " << p          << endl << "Fraction Value = "          << fraction << endl;      return 0;}Output:prog.cpp:13:23: error: no matching function for call to 'modf(std::__cxx11::string&, double*)'
  fraction = modf(k, &p);
.....
/usr/include/c++/5/cmath:395:3: note: candidate: float std::modf(float, float*)
   modf(float __x, float* __iptr)

// CPP program to demonstrate// exception of this function#include <bits/stdc++.h>using namespace std;  int main(){    // Take any value    double p, fraction;    string k = "5.06";      // Breaks k into two parts    fraction = modf(k, &p);      cout << "Integer Value = " << p          << endl << "Fraction Value = "          << fraction << endl;      return 0;}
Output:
prog.cpp:13:23: error: no matching function for call to 'modf(std::__cxx11::string&, double*)'
  fraction = modf(k, &p);
.....
/usr/include/c++/5/cmath:395:3: note: candidate: float std::modf(float, float*)
   modf(float __x, float* __iptr)

Note: k and p must be of same data type.
Examples:
Passing double(float) value as an argument:// CPP program to demonstrate// modf() function#include <bits/stdc++.h>using namespace std;  int main(){    // Take any value    double k = 5.06, p, fraction;      // Breaks k into two parts    fraction = modf(k, &p);      cout << "Integer Value = " << p          << endl << "Fraction Value = "          << fraction << endl;      return 0;}OUTPUTInteger Value = 5
Fraction Value = 0.06
Passing integer value as an argument:// CPP program to demonstrate// modf() function#include <bits/stdc++.h>using namespace std;  int main(){    // Taking positive value    double k = 8, p, fraction;      // Breaks k into two parts    fraction = modf(k, &p);      cout << k << " =>";    cout << "\tInteger Value = " << p          << endl << "\tFraction Value = "          << fraction << endl;      // Taking negative value    k = -8;    fraction = modf(k, &p);      cout << k << " =>";    cout << "\tInteger Value = " << p          << endl << "\tFraction Value = "         << fraction << endl;      return 0;}OUTPUT8  =>    Integer Value = 8
    Fraction Value = 0
-8 =>    Integer Value = -8
    Fraction Value = -0

Passing double(float) value as an argument:// CPP program to demonstrate// modf() function#include <bits/stdc++.h>using namespace std;  int main(){    // Take any value    double k = 5.06, p, fraction;      // Breaks k into two parts    fraction = modf(k, &p);      cout << "Integer Value = " << p          << endl << "Fraction Value = "          << fraction << endl;      return 0;}OUTPUTInteger Value = 5
Fraction Value = 0.06

// CPP program to demonstrate// modf() function#include <bits/stdc++.h>using namespace std;  int main(){    // Take any value    double k = 5.06, p, fraction;      // Breaks k into two parts    fraction = modf(k, &p);      cout << "Integer Value = " << p          << endl << "Fraction Value = "          << fraction << endl;      return 0;}
OUTPUT
Integer Value = 5
Fraction Value = 0.06

Passing integer value as an argument:// CPP program to demonstrate// modf() function#include <bits/stdc++.h>using namespace std;  int main(){    // Taking positive value    double k = 8, p, fraction;      // Breaks k into two parts    fraction = modf(k, &p);      cout << k << " =>";    cout << "\tInteger Value = " << p          << endl << "\tFraction Value = "          << fraction << endl;      // Taking negative value    k = -8;    fraction = modf(k, &p);      cout << k << " =>";    cout << "\tInteger Value = " << p          << endl << "\tFraction Value = "         << fraction << endl;      return 0;}OUTPUT8  =>    Integer Value = 8
    Fraction Value = 0
-8 =>    Integer Value = -8
    Fraction Value = -0

// CPP program to demonstrate// modf() function#include <bits/stdc++.h>using namespace std;  int main(){    // Taking positive value    double k = 8, p, fraction;      // Breaks k into two parts    fraction = modf(k, &p);      cout << k << " =>";    cout << "\tInteger Value = " << p          << endl << "\tFraction Value = "          << fraction << endl;      // Taking negative value    k = -8;    fraction = modf(k, &p);      cout << k << " =>";    cout << "\tInteger Value = " << p          << endl << "\tFraction Value = "         << fraction << endl;      return 0;}
OUTPUT
8  =>    Integer Value = 8
    Fraction Value = 0
-8 =>    Integer Value = -8
    Fraction Value = -0

Note: As in above code positive value gives positive output and negative value gives negative output. Hence, both fractional and integer have the same sign as the given value in the argument.
C-Library
CPP-Library
cpp-math
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Multidimensional Arrays in C / C++
Function Pointer in C
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
Priority Queue in C++ Standard Template Library (STL)