Return the scalar type of highest precision of the same kind as the input in Python

The np.maximum_sctype() function in NumPy returns the scalar type with the highest precision for a given data type kind. This is useful when you need to ensure maximum accuracy in calculations by upgrading to the most precise available type.

Syntax

numpy.maximum_sctype(dtype)

Parameters

dtype: Input data type. Can be a dtype object, Python type, or string representation convertible to a dtype.

Basic Examples

Let's see how maximum_sctype() works with different data types ?

import numpy as np

# Integer types - returns highest precision integer
print("int ?", np.maximum_sctype(int))
print("np.int32 ?", np.maximum_sctype(np.int32))

# Unsigned integer types
print("np.uint8 ?", np.maximum_sctype(np.uint8))

# Float types
print("float ?", np.maximum_sctype(float))
print("'f4' ?", np.maximum_sctype('f4'))

# Complex types  
print("complex ?", np.maximum_sctype(complex))
int ? <class 'numpy.int64'>
np.int32 ? <class 'numpy.int64'>
np.uint8 ? <class 'numpy.uint64'>
float ? <class 'numpy.float64'>
'f4' ? <class 'numpy.float64'>
complex ? <class 'numpy.complex128'>

String Type Code Examples

You can also use NumPy's type code strings ?

import numpy as np

# Using string type codes
print("'i2' (int16) ?", np.maximum_sctype('i2'))
print("'i4' (int32) ?", np.maximum_sctype('i4')) 
print("'i8' (int64) ?", np.maximum_sctype('i8'))
print("'u1' (uint8) ?", np.maximum_sctype('u1'))
print("'f8' (float64) ?", np.maximum_sctype('f8'))
'i2' (int16) ? <class 'numpy.int64'>
'i4' (int32) ? <class 'numpy.int64'>
'i8' (int64) ? <class 'numpy.int64'>
'u1' (uint8) ? <class 'numpy.uint64'>
'f8' (float64) ? <class 'numpy.float64'>

Practical Use Case

Here's how to use it for ensuring maximum precision in calculations ?

import numpy as np

# Original array with lower precision
arr = np.array([1.1, 2.2, 3.3], dtype=np.float32)
print("Original dtype:", arr.dtype)

# Get maximum precision type for this kind
max_type = np.maximum_sctype(arr.dtype)
print("Maximum precision type:", max_type)

# Convert to highest precision
high_precision_arr = arr.astype(max_type)
print("New dtype:", high_precision_arr.dtype)
Original dtype: float32
Maximum precision type: <class 'numpy.float64'>
New dtype: float64

Key Points

Input Kind Maximum Precision Type Use Case
Integer int64 Large number calculations
Unsigned Integer uint64 Non-negative large numbers
Float float64 High-precision decimals
Complex complex128 Scientific computations

Conclusion

The np.maximum_sctype() function helps upgrade data types to their highest precision variants within the same kind. This ensures maximum accuracy in numerical computations when precision is critical.

Updated on: 2026-03-26T19:17:14+05:30

217 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements