modf() Function with Example in C++

C++ modf() function: Here, we are going to learn about the modf() function with example of cmath header in C++ programming language.
Submitted by IncludeHelp, on May 20, 2020

C++ modf() function

modf() function is a library function of cmath header, it is used to get the fractional and integral parts of the given number. It accepts two arguments, the first argument is the value (float, double, or long double), and the second argument is a pointer to store an integral part, and it returns the fractional part and assigns an integral part in the pointer assigned as the second argument.

Syntax of modf() function:

C++11:

     double modf (double x, double* integral_part);
      float modf (float x, float* integral_part);
long double modf (long double x, long double* integral_part);
     double modf (T x, double* integral_part);

Parameter(s):

  • x, integral_partx represents the value to break into the fractional and integral part, integral_part represents the pointer to store an integral part.

Return value:

It returns the fractional part of the given value x.

Example:

    Input:
    float x = 123.456f;
    float fpart = 0.0f;
    float ipart = 0.0f;
    
    Function call:
    fpart = modf(x, &ipart);
    
    Output:
    ipart = 123
    fpart = 0.456001

C++ code to demonstrate the example of modf() function

// C++ code to demonstrate the example of
// modf() function

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float value = 0.0f;
    float fpart = 0.0f;
    float ipart = 0.0f;

    value = 123.456f;
    fpart = modf(value, &ipart);
    cout << "value: " << value << endl;
    cout << "fpart: " << fpart << endl;
    cout << "ipart: " << ipart << endl;
    cout << endl;

    value = 123.0f;
    fpart = modf(value, &ipart);
    cout << "value: " << value << endl;
    cout << "fpart: " << fpart << endl;
    cout << "ipart: " << ipart << endl;
    cout << endl;

    value = 0.123f;
    fpart = modf(value, &ipart);
    cout << "value: " << value << endl;
    cout << "fpart: " << fpart << endl;
    cout << "ipart: " << ipart << endl;
    cout << endl;

    value = -123.456f;
    fpart = modf(value, &ipart);
    cout << "value: " << value << endl;
    cout << "fpart: " << fpart << endl;
    cout << "ipart: " << ipart << endl;
    cout << endl;

    return 0;
}

Output

value: 123.456
fpart: 0.456001
ipart: 123

value: 123
fpart: 0
ipart: 123

value: 0.123
fpart: 0.123
ipart: 0

value: -123.456
fpart: -0.456001
ipart: -123

Reference: C++ modf() function







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.