- 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 fractional and integral parts of a value in Numpy
To return the fractional and integral parts of a value, use the numpy.modf() method in Python Numpy. The fractional and integral parts are negative if the given number is negative.
The out is a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
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 fractional and integral parts of a value, use the numpy.modf() method in Python Numpy −
print("Returning the fractional and integral parts...
")
Check for float −
print("Result? ", np.modf(0.1)) print("
Result? ", np.modf(-0.7))
Check for int and inf −
print("
Result? ", np.modf(5)) print("
Result? ", np.modf(-np.inf))
Check for nan and inf −
print("
Result? ", np.modf(np.nan)) print("
Result? ", np.modf(np.inf))
Check for log −
print("
Result? ", np.modf(np.log(1))) print("
Result? ", np.modf(np.log(2)))
Example
import numpy as np # To return the fractional and integral parts of a value, use the numpy.modf() method in Python Numpy print("Returning the fractional and integral parts...
") # Check for float print("Result? ", np.modf(0.1)) print("
Result? ", np.modf(-0.7)) # Check for int and inf print("
Result? ", np.modf(5)) print("
Result? ", np.modf(-np.inf)) # Check for nan and inf print("
Result? ", np.modf(np.nan)) print("
Result? ", np.modf(np.inf)) # Check for log print("
Result? ", np.modf(np.log(1))) print("
Result? ", np.modf(np.log(2)))
Output
Returning the fractional and integral parts... Result? (0.1, 0.0) Result? (-0.7, -0.0) Result? (0.0, 5.0) Result? (-0.0, -inf) Result? (nan, nan) Result? (0.0, inf) Result? (0.0, 0.0) Result? (0.6931471805599453, 0.0)