Python trunc – Python NumPy trunc() Function

NumPy trunc() Function:

Python trunc: The trunc() function of the NumPy module is a math function that returns the truncated value of an array’s elements. The nearest integer i which is closer to zero than x, is the trunc of the scalar x. This basically says that this function ignores the fractional part of the signed number x.

Syntax:

numpy.trunc(x, out=None)

Parameters

x: This is required. It is an array (array-like) given as input.

out: This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value: 

If out=None returns an array that contains the truncated values. A reference to out is returned if an output array is given.

NumPy trunc() Function in Python

Example1

Here is an example for 1D Arrays

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the trunc() function of the numpy module to get the truncated values of the given array elements. Here it removes the fractional part.
  • Store it in another variable.
  • Print the truncated values of the given array elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([9.321, -5.58, 8.8, -3.2, 12.123])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the above given array as an argument to the trunc() function of the 
# numpy module to get the truncated values of the given array elements.
# Here it removes the fractional part.
# Store it in another variable.
rslt = np.trunc(gvn_arry)
# Print the truncated values of the given array elements.
print("The truncated values of the given array elements:")
print(rslt)

Output:

The above given array is:
[ 9.321 -5.58 8.8 -3.2 12.123]
The truncated values of the given array elements:
[ 9. -5. 8. -3. 12.]

Example2

Here is an example for 2-Dimensional Arrays

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list (2-Dimensional) as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the trunc() function of the numpy module to get the truncated values of the given array elements. Here it removes the fractional part.
  • Store it in another variable.
  • Print the truncated values of the given array elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list(2-Dimensional) as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[-2.3, 6.6],[4.5, -8.9]])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the above given array as an argument to the trunc() function of the 
# numpy module to get the truncated values of the given array elements.
# Here it removes the fractional part.
# Store it in another variable.
rslt = np.trunc(gvn_arry)
# Print the truncated values of the given array elements.
print("The truncated values of the given array elements:")
print(rslt)

Output:

The above given array is:
[[-2.3 6.6]
 [ 4.5 -8.9]]
The truncated values of the given array elements:
[[-2. 6.]
 [ 4. -8.]]