Python math.modf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.modf(x) splits x into two parts and returns the tuple (fractional_part, integer_part), where both elements are floats carrying the sign of x.

For example, math.modf(-3.75) returns (-0.75, -3.0). Notice the fractional part comes first and the whole part second.

math.modf() is a function in Python's standard math module: import it with import math and call math.modf(...), or import the name directly with from math import modf.

Syntax

Import math and unpack the two-float tuple.

python
import math

frac_part, int_part = math.modf(x)
CallWhat it does
math.modf(3.75)Returns (0.75, 3.0).
math.modf(-3.75)Returns (-0.75, -3.0) (both parts keep negative sign).
math.modf(float("inf"))Returns (0.0, inf).
math.modf(-0.0)Returns (-0.0, -0.0).

Both elements are floats (not ints); if you only need an integer whole part, call math.trunc().

Function signatures
python
math.modf(x, /)
# returns: (frac, whole) tuple of two floats with the sign of x

Parameters

ParameterDescription
xRequired positional number (int or float).

Return Value

Return ValueDescription
tupleA 2-tuple (fractional_part, integer_part) of two floats.

Python Examples

Example 1 — Split Positive and Negative Floats Into Fraction and Whole

Unpack math.modf() across mixed values and inspect negative fractional signs.

python
import math

for val in [3.75, -3.75, 5.0, -0.25]:
    frac, whole = math.modf(val)
    print(f"{val:>5} -> frac={frac}, whole={whole}")

How It Works

  1. Fractional part comes first. Unpacking puts 0.75 in frac and 3.0 in whole.
  2. Both parts keep negative sign. Splitting -3.75 returns -0.75 and -3.0 so their sum equals the original number.
  3. Signed zero on -0.25. When whole magnitude is zero on a negative number, the integer slot preserves -0.0.

Example 2 — Convert Fractional Hours Into Hours and Minutes

Split exact dyadic durations like 2.25 and 4.75 hours into hours and minutes.

python
import math

for hours in [2.25, 4.75, 1.5]:
    frac_hr, whole_hr = math.modf(hours)
    mins = int(frac_hr * 60)
    print(f"{hours} hr -> {int(whole_hr)}h {mins}m")

How It Works

  1. Separate whole hours. whole_hr receives exact whole floats like 2.0 and 4.0.
  2. Convert fractional hours. Multiplying 0.25, 0.75, and 0.5 by sixty converts cleanly to minutes.
  3. Exact binary fractions. Powers of two like 0.25 and 0.75 have zero representation drift.

Example 3 — Inspect How modf() Splits Infinity, Negative Zero, and NaN

Test special IEEE 754 float values passed to math.modf().

python
import math

print(math.modf(float("inf")))
print(math.modf(float("-inf")))
print(math.modf(-0.0))
f_nan, i_nan = math.modf(float("nan"))
print(math.isnan(f_nan), math.isnan(i_nan))

How It Works

  1. Infinite integer part. Infinity splits into zero fraction and infinite whole part.
  2. Negative infinity keeps minus signs. Both -0.0 and -inf preserve negative sign bits.
  3. Both slots NaN. A NaN input produces (nan, nan), confirmed via math.isnan().

Frequently Asked Questions

(fractional_part, integer_part) — fraction is first, whole part is second.
Because frac + whole must reconstruct x: (-0.75) + (-3.0) == -3.75.
No, both elements in the returned tuple are floats. Wrap the second value with int() or call math.trunc() if you need an int.
Because 0.3 cannot be represented exactly in binary float, modf() returns the exact fractional difference of the stored binary value.
(0.0, inf), without raising OverflowError.

Next: math.nextafter()

Return the next floating-point value after x toward y.

nextafter() →

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.

9 people found this page helpful