Return mantissa and exponent as a pair for a specific tuple element 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 mantissa and exponent as a pair of a given tuple, use the index value in the numpy.frexp() method in Python Numpy.

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.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

Steps

At first, import the required library −

import numpy as np

Create a tuple −

myTuple = (27.3, 12.9, 166.8, -14.8, 78,6, -19.8)


Display the tuple −

print("Tuple...\n", myTuple)

Length of the tuple −

print("\nTuple length...\n", len(myTuple))


Type of the tuple −

print("\nTuple type...\n", type(myTuple))

Return mantissa and exponent as a pair of a given tuple −

print("\nReturn mantissa and exponent as a pair of a given tuple...\n",np.frexp(myTuple))


To return mantissa and exponent as a pair of a given tuple, use the index value in the numpy.frexp() method −

print("\nResult (specific element)...\n",np.frexp(myTuple[5]))

Example

import numpy as np

# Create a tuple
myTuple = (27.3, 12.9, 166.8, -14.8, 78,6, -19.8)

# Display the tuple
print("Tuple...\n", myTuple)

# Length of the tuple
print("\nTuple length...\n", len(myTuple))

# Type of the tuple
print("\nTuple type...\n", type(myTuple))

# Return mantissa and exponent as a pair of a given tuple
print("\nReturn mantissa and exponent as a pair of a given tuple...\n",np.frexp(myTuple))

# To return mantissa and exponent as a pair of a given tuple, use the index value in the numpy.frexp() method in Python Numpy
print("\nResult (specific element)...\n",np.frexp(myTuple[5]))

Output

Tuple...
(27.3, 12.9, 166.8, -14.8, 78, 6, -19.8)

Tuple length...
7

Tuple type...
<class 'tuple'>

Return mantissa and exponent as a pair of a given tuple...
(array([ 0.853125 , 0.80625 , 0.6515625, -0.925 , 0.609375 , 0.75 , -0.61875 ]), array([5, 4, 8, 4, 7, 3, 5], dtype=int32))

Result (specific element)...
(0.75, 3)
Updated on 08-Feb-2022 07:30:57