Python math.modf() function
In numerical calculations, sometimes we need toseparate the integer part and fractional part of a number. For example, extract 3 and 0.14 from 3.14.
math.modf()is a function specifically designed for this operation. It returns a tuple containing the number'sfractional partandinteger part。
Word Definition: modfis an abbreviation for "modular fractional", meaning "fractional part".
Basic Syntax and Parameters
Syntax
import math math.modf(x)
Parameter Description
- Parameter:
x- numeric value (integer or float)
Return Value
- Returns a tuple
(fractional, integer) fractional: fractional part, sign is the same as xinteger: integer part (truncated toward zero)
Example
Example 1: Basic Usage
Example
import math
result = math.modf(3.14159)
print("math.modf(3.14159) =", result)
fractional, integer = math.modf(3.14159)
print(f"Fractional part: {fractional}, integer part: {integer}")
result = math.modf(3.14159)
print("math.modf(3.14159) =", result)
fractional, integer = math.modf(3.14159)
print(f"Fractional part: {fractional}, integer part: {integer}")
Output:
math.modf(3.14159) = (0.14158999999999988, 3.0) 小数部分: 0.14158999999999988, 整数部分: 3.0
Example 2: Handling Negative Numbers
Example
import math
print("math.modf(-3.14) =", math.modf(-3.14))
print("math.modf(-0.5) =", math.modf(-0.5))
fractional, integer = math.modf(-3.14)
print(f"Fractional part: {fractional}, integer part: {integer}")
print("math.modf(-3.14) =", math.modf(-3.14))
print("math.modf(-0.5) =", math.modf(-0.5))
fractional, integer = math.modf(-3.14)
print(f"Fractional part: {fractional}, integer part: {integer}")
Output:
math.modf(-3.14) = (-0.14000000000000012, -3.0) math.modf(-0.5) = (-0.5, -0.0) 小数部分: -0.14, 整数部分: -3.0
Example 3: Handling Integers
Example
import math
print("math.modf(5) =", math.modf(5))
print("math.modf(0) =", math.modf(0))
print("math.modf(-8) =", math.modf(-8))
print("math.modf(5) =", math.modf(5))
print("math.modf(0) =", math.modf(0))
print("math.modf(-8) =", math.modf(-8))
Output:
math.modf(5) = (0.0, 5.0) math.modf(0) = (0.0, 0.0) math.modf(-8) = (-0.0, -8.0)
Example 4: Practical Application - Separating Amount
Example
import math
# Split amount: yuan, jiao, fen
price = 12.58
fractional, integer = math.modf(price)
yuan = int(integer)
jiao = int(fractional * 10)
fen = int((fractional * 10 - jiao) * 10)
print(f"Price {price} yuan = {yuan} yuan {jiao} jiao {fen} fen")
# Time conversion: hours to hours:minutes:seconds
hours = 73.5
fractional_hours, whole_hours = math.modf(hours)
total_minutes = fractional_hours * 60
fractional_minutes, whole_minutes = math.modf(total_minutes)
seconds = fractional_minutes * 60
print(f"{hours} hours = {int(whole_hours)} hours {int(whole_minutes)} minutes {int(seconds)} seconds")
# Split amount: yuan, jiao, fen
price = 12.58
fractional, integer = math.modf(price)
yuan = int(integer)
jiao = int(fractional * 10)
fen = int((fractional * 10 - jiao) * 10)
print(f"Price {price} yuan = {yuan} yuan {jiao} jiao {fen} fen")
# Time conversion: hours to hours:minutes:seconds
hours = 73.5
fractional_hours, whole_hours = math.modf(hours)
total_minutes = fractional_hours * 60
fractional_minutes, whole_minutes = math.modf(total_minutes)
seconds = fractional_minutes * 60
print(f"{hours} hours = {int(whole_hours)} hours {int(whole_minutes)} minutes {int(seconds)} seconds")
Output:
价格 12.58 元 = 12 元 5 角 7 分 73.5 小时 = 73 时 30 分 0 秒
Notes
- The returned integer part is a float type, such as 3.0 instead of 3
- The integer part uses "truncation toward zero", which is different from floor
- Floating-point numbers have precision errors, so results may have slight deviations
Other Extensions
Python math module