HomeLinuxPython Math Trunc

Python Math Trunc


The “math” library in Python supplies varied mathematical features and strategies, together with trigonometric features, exponential features, and so forth. A few of the common strategies are “math.exp()”, “math.flooring()”, “math.atan()”, and so forth. The “math.trunc()” methodology is used to truncate the fractional, or decimal a part of the given floating-point numbers and returns its integer half.

This Python weblog presents an in-depth information on Python’s “math.trunc()” methodology by overlaying the next elements:

What’s the “math.trunc()” Methodology in Python?

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

Syntax

 
On this syntax, “num” factors to the worth that must be truncated.

Now, let’s check out the below-given examples utilizing the Python “math.trunc()” methodology.

Instance 1: Making use of the “math.trunc()” Methodology to Truncate a Optimistic Float Quantity

The under code is used to truncate the optimistic float quantity utilizing the “math.trunc()” methodology:

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

 
Within the above code snippet, the “math.trunc()” methodology takes the initialized floating-point quantity as its argument and returns the integer worth of the handed quantity.

Output


As seen, the handed floating level quantity has been truncated, thereby omitting its fractional half.

Instance 2: Making use of the “math.trunc()” Methodology to Truncate a Damaging Float Quantity

The next code block is used to truncate a unfavourable float level quantity:

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

 
Right here, the “math.trunc()” methodology is used to truncate the fractional a part of the initialized unfavourable float quantity handed as its argument, thereby returning its integer half.

Output


This output implies that the initialized unfavourable floating-point quantity has been truncated appropriately.

Instance 3: Making use of the “math.trunc()” Methodology to Truncate a Complicated Quantity

This instance applies the mentioned methodology to truncate a posh quantity as an alternative:

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

 
Within the above code snippet, the “math.trunc()” methodology takes the initialized advanced quantity as its argument together with the “actual” attribute to return the integer worth from the true a part of the handed advanced quantity.

Output


As analyzed, the true half(integer) of the advanced quantity has been retrieved.

Nonetheless, we will additionally truncate the imaginary half(fractional) of the floating-point advanced numbers through the “imag” attribute. Right here is an instance:

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

 
On this code, the “math.trunc()” methodology takes the advanced quantity related to the “imag” attribute to truncate the imaginary half(fractional) of the advanced quantity.

Output


Right here, the imaginary a part of the given advanced quantity has been truncated and the integer worth of the actual half i.e., “imaginary” has been retrieved.

Conclusion

The “math.trunc()” methodology of the “math” module in Python is used to truncate the fractional or decimal a part of the given floating-point quantity. This methodology is simple because it takes a single argument, i.e., float, advanced, and returns its integer and actual half, respectively. On this Python information, we introduced a whole overview of the Python “math.trunc()” methodology utilizing quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments