Open In App

modf() in C/C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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)

Time Complexity: O(1)

Space Complexity: O(1)

  • If anything other then a float, double or integer number is passed, it returns a type error. 

CPP




// 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:

  1. Passing double(float) value as an argument: 

CPP




// 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;
}


  1. OUTPUT
Integer Value = 5
Fraction Value = 0.06
  1. Passing integer value as an argument: 

CPP




// 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;
}


  1. 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.



Last Updated : 14 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments