Home » Python

math.modf() method with example in Python

Python math.modf() method: Here, we are going to learn about the math.modf() method with example in Python.
Submitted by IncludeHelp, on April 17, 2019

Python math.modf() method

math.modf() method is a library method of math module, it is used to get the fractional part and integer part of a number. It accepts a number (integer or float) and returns a tuple that contains fractional part and integer part of the number.

Note:

  • The integer part of the number also returns in float
  • If the number is an integer, the fractional part will be 0.0
  • If the number is a negative number, both of the parts will be negative
  • If anything is passed except the number, the method returns a type error "TypeError: a float is required"

Syntax of math.modf() method:

    math.modf(n)

Parameter(s): n – an integer or a float number.

Return value: tuple – it returns a tuple that contains fractional part and integer part of the number n.

Example:

    Input:
    a = 10.23

    # function call
    print(math.modf(a))

    Output:
    (0.23000000000000043, 10.0)

Python code to demonstrate example of math.modf() method

# Python code demonstrate example of 
# math.modf() method

# importing math module
import math

# numbers 
a = 10
b = 10.23
c = -10
d = -10.23

# printing the fractional and integer part 
# of the numbers by using math.modf()

print("modf(a): ", math.modf(a))
print("modf(b): ", math.modf(b))
print("modf(c): ", math.modf(c))
print("modf(d): ", math.modf(d))

Output

modf(a):  (0.0, 10.0)
modf(b):  (0.23000000000000043, 10.0)
modf(c):  (-0.0, -10.0)
modf(d):  (-0.23000000000000043, -10.0)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




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.