What is the numpy.trunc() function in Numpy?
Overview
The numpy.trunc() function in NumPy is used to return the nearest integer i of elements in an input array x. In other words, the fractional part of each signed value of each element in x is discarded.
Syntax
numpy.trunc(x, /, out=None, *, where=True)
Parameters
The numpy.trunc() function takes the following parameter values:
x(required): This represents the input array.out(optional): This represents the location where the result is stored.where(optional): This is the condition over which the input is being broadcast. At a given location where this condition isTrue, the resulting array will be set to theufuncresult. Otherwise, the resulting array will retain its original value.**kwargs(optional): This represents other keyword arguments.
Return value
The numpy.trunc() function returns a truncated value of each element in the input array x.
Example
import numpy as np# creating an arrayx = np.array([-1.5, 10.3, -6.6, 0.9, 180.05])# obtaining the truncated valuesmyarray = np.trunc(x)print(x)print(myarray)
Explanation
- Line 1: We import the
numpymodule. - Line 4: We create an array
xusing thearray()method. - Line 7: We implement the
np.trunc()function on the array. The result is assigned to a variablemyarray. - Line 9: We print the input array
x. - Line 10: We print the variable
myarray.