modf() in C/C++


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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    OUTPUT

    Integer Value = 5
    Fraction Value = 0.06
    
  2. Passing integer value as an argument:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.