×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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

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.

Sample Input and Output

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

Example

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.