Technology3 min read

Python Math Functions- fsum(), modf(), and trunc()

Abhishek Ahlawat

By Abhishek Ahlawat โ€” Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#Python#Python Math Module

Summarize this article using AI

We have seen numerous math functions in the previous posts. Continuing on the same lines, we will explore the following math functions.

  1. fsum

  2. modf

  3. trunc

Before using these functions, the math library needs to be imported using 'import math' statement and the math library has to be referenced using the dot operator.


1. Python fsum() Method

This is a built-in method in Python that takes one argument and returns the sum between a specific range or runs over an iterable and returns the sum of the data elements present in it.

Syntax of fsum() method

math.fsum(iterable)

This iterable could be a list, array or any number which indicates a range. The resultant is a floating-point number.


Time for an example:

import math 

print(math.fsum(range(12))) # passing a number
my_list_int = [12,56, 89] 

print(math.fsum(my_list_int)) # passing an iterable, i.e a list
my_list_fl = [2.45, 6.89, 7.40] # passing floating point list
print(math.fsum(my_list_fl)) 

Output:

66.0
157.0
16.740000000000002



2. Python modf() Method

The modf() method is a built-in method in the math library in python, which returns the fractional and integral (integer) part of a number. The result is returned as a tuple with 2 elements. The integer part is returned as a floating-point number and the sign of both the items in the tuple is the same.
This method takes in one argument, which is the number whose fractional and integral part needs to be extracted.

Syntax of modf() method

math.modf(number)

This number should be a floating-point number. If anything else is passed, it throws a TypeError.


Time for an example:

import math 

print("math.modf(112.56) : ", math.modf(100.12)) # modf with positive number
print("math.modf(-112.56) : ", math.modf(-100.72))  # modf with a negative value
print("math.modf(12) : ", math.modf(2))

Output:

math.modf(112.56) :  (0.12000000000000455, 100.0)
math.modf(-112.56) :  (-0.7199999999999989, -100.0)
math.modf(12) :  (0.0, 2.0)

When a different value is passed instead of a number, it generates a TypeError. It has been demonstrated below:

import math 

print("math.modf(100.12) : ", math.modf('100.12')) # modf with a string

Output:

TypeError: must be real number, not str



Passing data elements of a list to the modf() method

my_list = [112.56, -112.56, 12, 0]
for i in my_list:
    print(math.modf(i))

Output:

(0.5600000000000023, 112.0)
(-0.5600000000000023, -112.0)
(0.0, 12.0)
(0.0, 0.0)



3. Python trunc() method

The word trunc is short for truncate, and in python trunc() is a built-in method that behaves like the ceil method for negative numbers and like a floor method for positive numbers. It takes the element which needs to be truncated as an argument and returns the truncated output.

Syntax of trunc() method

math.trunc(number)

Time for an example:

import math    

print(math.trunc(12.75))  # work as floor method   
print(math.trunc(12.35))  # work as floor method   

print(math.floor(12.75))  # floor method
print(math.ceil(12.75))   # ceil method
print("\n")
print(math.trunc(-12.75)) # work as ceil method
print(math.floor(-12.75)) # floor  method
print(math.ceil(-12.75))  # ceil method

Output:

12
12
12
13

-12
-13
-12



Conclusion

In this post, we saw a handful of math functions that come in handy while building applications. In the upcoming post, we will cover some other math functions, so stay tuned.

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next