- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return the truncated value of the inputs in Numpy
To return the truncated value of the input, use the numpy.trunc() method in Python Numpy. The function returns the truncated value of each element in x. This is a scalar if x is a scalar. The truncated value of the scalar x is the nearest integer i which is closer to zero than x is. In short, the fractional part of the signed number x is discarded.
The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.
Steps
At first, import the required library −
import numpy as np
To return the truncated value of the input, use the numpy.trunc() method in Python Numpy Check trunc() for float −
print("Result? ", np.trunc(55.8)) print("
Result? ", np.trunc(-599.2))
Check trunc() for inf −
print("
Result? ", np.trunc(-np.inf))
Check trunc() for nan and inf −
print("
Result? ", np.trunc(np.nan)) print("
Result? ", np.trunc(np.inf))
Check trunc() for log −
print("
Result? ", np.trunc(np.log(1))) print("
Result? ", np.trunc(np.log(2)))
Example
import numpy as np # To return the truncated value of the input, use the numpy.trunc() method in Python Numpy print("Returning the trunc value...
") # Check trunc() for float print("Result? ", np.trunc(55.8)) print("
Result? ", np.trunc(-599.2)) # Check trunc() for inf print("
Result? ", np.trunc(-np.inf)) # Check trunc() for nan and inf print("
Result? ", np.trunc(np.nan)) print("
Result? ", np.trunc(np.inf)) # Check trunc() for log print("
Result? ", np.trunc(np.log(1))) print("
Result? ", np.trunc(np.log(2)))
Output
Returning the trunc value... Result? 55.0 Result? -599.0 Result? -inf Result? nan Result? inf Result? 0.0 Result? 0.0
- Related Articles
- Return the truncated value of the array elements in Numpy
- Return the truncated value of a specific array element in Numpy
- Return the ceil of the inputs in Numpy
- Return the truncated value of the array elements and store the result in a new location in Numpy
- Return the largest integer smaller or equal to the division of the inputs in Numpy
- Return the ceil value of the array elements in Numpy
- Logarithm of the sum of exponentiations of the inputs in Numpy
- Return the absolute value of a masked Array in NumPy
- Using return value of cin to take unknown number of inputs in C++
- Logarithm of the sum of exponentiations of the inputs in base-2 in Numpy
- Return the common filling value of two masked arrays in Numpy
- Return the fractional and integral parts of a value in Numpy
- Return numbers spaced evenly on a geometric progression but with negative inputs in Numpy
- Return numbers spaced evenly on a geometric progression but with complex inputs in Numpy
- Return the default fill value for the argument object in Numpy
