C++ std::modf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::modf(x, &whole) splits a number in two: the whole part and the fractional part. For 3.75 the whole part is 3 and the fractional part is 0.75.

The function returns the fractional part and stores the whole part in a variable that you pass by address. Both parts carry the sign of the original number, so -3.75 gives -3 and -0.75. Both values are double.

std::modf() is declared in <cmath> and is part of the group Rounding. Rounding functions turn a fractional number into a whole number. They differ in which way they go for negative numbers and for ties such as 2.5.

Syntax

Include <cmath> and call std::modf. Start with these lines.

C++
double whole;
double frac = std::modf(3.75, &whole);   // frac = 0.75, whole = 3

float  wf;
float  ff = std::modf(-1.5f, &wf);       // ff = -0.5f, wf = -1
CallWhat it does
std::modf(3.75, &whole)Returns 0.75 and stores 3 in whole.
std::modf(-3.75, &whole)Returns -0.75 and stores -3. Both parts are negative.
std::modf(5.0, &whole)Returns 0 and stores 5. A whole number has no fraction.

Pass the address of a variable of the same type as x. A double needs a double*, and a float needs a float*.

Library declarations
C++
float       modf(float x, float* iptr);
double      modf(double x, double* iptr);
long double modf(long double x, long double* iptr);

Parameters

ParameterDescription
xRequired. The number to split. It can be float, double, or long double.
iptrRequired. A pointer to a variable of the same type. The whole part is stored here, with the sign of x.

Return Value

Return ValueDescription
Fractional partThe part after the decimal point, with the same sign as x. Its size is always less than 1.
0Returned when x is a whole number or infinity (with the sign of x).
NaNIf x is NaN, both the result and *iptr are NaN.

C++ Examples

Example 1 — Split Several Numbers

Print the whole and fractional part of positive, negative, whole, and small numbers.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {3.75, -3.75, 5.0, 0.25};

    for (double v : values) {
        double whole;
        double frac = std::modf(v, &whole);
        std::cout << v << " -> whole " << whole << ", fraction " << frac << '\n';
    }
    return 0;
}

How It Works

  1. modf() returns the fraction and writes the whole part into whole, which is why the variable is declared first and passed with &.
  2. 3.75 splits into 3 and 0.75. For -3.75 both parts are negative: -3 and -0.75.
  3. 5.0 has no fractional part, so the fraction is 0. 0.25 is smaller than one, so the whole part is 0.

Example 2 — Dollars and Cents from a Decimal

Split an amount of money. The whole part is the dollars, and the fraction times 100, rounded, is the cents.

C++
#include <cmath>
#include <iostream>

int main() {
    double amounts[] = {12.34, 7.05, 0.99};

    for (double amount : amounts) {
        double dollars;
        double frac = std::modf(amount, &dollars);
        long cents = std::lround(frac * 100.0);
        std::cout << amount << " -> " << dollars << " dollars " << cents << " cents\n";
    }
    return 0;
}

How It Works

  1. modf() gives the dollars in dollars and the fraction as its return value.
  2. The fraction of 12.34 is 0.34, but it is stored as 0.3399999.... Multiplying by 100 gives 33.99..., and lround() fixes it to 34.
  3. 7.05 gives 7 dollars and 5 cents, and 0.99 gives 0 dollars and 99 cents. Rounding is needed for every one.

Example 3 — Small Negative Numbers and Special Values

Check the sign of the parts for a small negative number, and see what modf() does with infinity and NaN.

C++
#include <cmath>
#include <iostream>
#include <limits>

int main() {
    double whole;
    double frac = std::modf(-0.5, &whole);
    std::cout << "modf(-0.5): whole = " << whole << ", frac = " << frac << '\n';

    double inf = std::numeric_limits<double>::infinity();
    frac = std::modf(inf, &whole);
    std::cout << "modf(inf): whole = " << whole << ", frac = " << frac << '\n';

    frac = std::modf(std::nan(""), &whole);
    std::cout << "modf(NaN): whole is NaN: " << std::isnan(whole)
              << ", frac is NaN: " << std::isnan(frac) << '\n';
    return 0;
}

How It Works

  1. For -0.5 the whole part is zero, but it keeps the minus sign of the input. It prints as -0, and the fraction is -0.5.
  2. For infinity, the whole part is infinity itself, and the fraction is 0. An infinite number has no decimal part.
  3. For NaN, both the returned fraction and the stored whole part are NaN. std::isnan() prints 1 for each of them.

Frequently Asked Questions

std::modf(x, &whole) splits x into two parts. It returns the fractional part and stores the whole part in the variable you give it. Both parts keep the sign of x.
The fractional part of x, with the same sign as x. The whole part is stored through the pointer argument.
It returns the fractional part of the number. The whole part is stored in the variable that the second argument points to. Both parts have the same sign as the input.
modf() splits a number into whole and fractional parts. fmod(x, y) gives the remainder of x divided by y. modf(x, &w) is like fmod(x, 1.0) for the fraction, but it also gives you the whole part.
Include <cmath> and write std::modf(). That always works and picks the C++ overloads. The global name ::modf often works too, but the standard does not promise it, and it may only take a double.

Next: nan()

Keep going through the <cmath> functions. Creates a quiet NaN (not a number) value.

nan() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful