- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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