- Trending Categories
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)
- Related Articles
- Return the fractional and integral parts of array values in Numpy
- Extract the fractional and integral parts of a specific array value in Numpy
- Multiply the fractional part of two Numpy arrays with a scalar value
- Return the absolute value of a masked Array in NumPy
- Return the truncated value of the inputs in Numpy
- Return the truncated value of a specific array element in Numpy
- Return mantissa and exponent as a pair of a given value in Numpy
- Return the truncated value of the array elements in Numpy
- Return the ceil value of the array elements in Numpy
- Divide a scalar value into every element of a masked Array and return the floor value in NumPy
- Divide every element of a masked Array into a scalar value and return the floor value in NumPy
- Return the next floating-point value after a value towards another value in Numpy
- How to find the fractional and integer parts of x in a two-item tuple in Python?
- Return the common filling value of two masked arrays in Numpy
- Return the truncated value of the array elements and store the result in a new location in Numpy
