# Return the fractional and integral parts of a value in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

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...\n")


Check for float −

print("Result? ", np.modf(0.1))
print("\nResult? ", np.modf(-0.7))

Check for int and inf −

print("\nResult? ", np.modf(5))
print("\nResult? ", np.modf(-np.inf))

Check for nan and inf −

print("\nResult? ", np.modf(np.nan))
print("\nResult? ", np.modf(np.inf))

Check for log −

print("\nResult? ", np.modf(np.log(1)))
print("\nResult? ", 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...\n")

# Check for float
print("Result? ", np.modf(0.1))
print("\nResult? ", np.modf(-0.7))

# Check for int and inf
print("\nResult? ", np.modf(5))
print("\nResult? ", np.modf(-np.inf))

# Check for nan and inf
print("\nResult? ", np.modf(np.nan))
print("\nResult? ", np.modf(np.inf))

# Check for log
print("\nResult? ", np.modf(np.log(1)))
print("\nResult? ", 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)