Python

Python Math Trunc

The “math” library in Python provides various mathematical functions and methods, including trigonometric functions, exponential functions, etc. Some of the popular methods are “math.exp()”, “math.floor()”, “math.atan()”, etc. The “math.trunc()” method is used to truncate the fractional, or decimal part of the given floating-point numbers and returns its integer part.

This Python blog offers an in-depth guide on Python’s “math.trunc()” method by covering the following aspects:

What is the “math.trunc()” Method in Python?

The “math.trunc()” method of the “math” module is used to return the integer part of a floating-point number, thereby truncating its fractional part. For instance, if we have a floating-point number “3.14159”, the “math.trunc()” method will return it as “3”.

Syntax

math.trunc(num)

 
In this syntax, “num” points to the value that needs to be truncated.

Now, let’s take a look at the below-given examples using the Python “math.trunc()” method.

Example 1: Applying the “math.trunc()” Method to Truncate a Positive Float Number

The below code is used to truncate the positive float number using the “math.trunc()” method:

import math
float_number = 3.14159
print(math.trunc(float_number))

 
In the above code snippet, the “math.trunc()” method takes the initialized floating-point number as its argument and returns the integer value of the passed number.

Output


As seen, the passed floating point number has been truncated, thereby omitting its fractional part.

Example 2: Applying the “math.trunc()” Method to Truncate a Negative Float Number

The following code block is used to truncate a negative float point number:

import math
float_number = -3.14159
print(math.trunc(float_number))

 
Here, the “math.trunc()” method is used to truncate the fractional part of the initialized negative float number passed as its argument, thereby returning its integer part.

Output


This output implies that the initialized negative floating-point number has been truncated appropriately.

Example 3: Applying the “math.trunc()” Method to Truncate a Complex Number

This example applies the discussed method to truncate a complex number instead:

import math
complex_number = 3.456 + 45.2j
print(math.trunc(complex_number.real))

 
In the above code snippet, the “math.trunc()” method takes the initialized complex number as its argument along with the “real” attribute to return the integer value from the real part of the passed complex number.

Output


As analyzed, the real part(integer) of the complex number has been retrieved.

However, we can also truncate the imaginary part(fractional) of the floating-point complex numbers via the “imag” attribute. Here is an example:

import math
complex_number = 3.456 + 45.52j
print(math.trunc(complex_number.imag))

 
In this code, the “math.trunc()” method takes the complex number associated with the “imag” attribute to truncate the imaginary part(fractional) of the complex number.

Output


Here, the imaginary part of the given complex number has been truncated and the integer value of the particular part i.e., “imaginary” has been retrieved.

Conclusion

The “math.trunc()” method of the “math” module in Python is used to truncate the fractional or decimal part of the given floating-point number. This method is straightforward as it takes a single argument, i.e., float, complex, and returns its integer and real part, respectively. In this Python guide, we presented a complete overview of the Python “math.trunc()” method using numerous examples.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.