What is the rint function in NumPy?

The rint function is used to round numerical values to the nearest integer. It comes as part of NumPy, which is a library of the high-level coding language, Python.

Syntax

numpy.rint(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'rint'>

A universal function (ufunc) is a function that operates on ndarrays in an element-by-element fashion. rint is a universal function.

Parameters

The rint function accepts the following arguments:

  • x - array-like structure on which the rint function will be applied.

  • out (optional) - the function’s output is stored at this location.

  • where (optional)- if set as true, a universal function is calculated at this position.

  • casting (optional)- enables the user to decide how the data will be cast. If set as same_kind, safe casting will take place.

  • order (optional)- determines the memory layout of the output. For example, if set as K, the function reads data in the order it is written in memory.

  • dtype(optional) - the data type of the array.

  • subok (optional) - to pass subclasses, subok must be set as true.

Return value

The rint function returns the corresponding integer values in a ndarray. If the optional parameter out is provided, it returns a reference.

If x is a scalar, the return value is also a scalar.

Example

The example below demonstrates how to apply the rint function on an array containing 4 values.

import numpy as np
print(np.rint([3.56, 3.46, -3.46, -3.56]))
Copyright ©2024 Educative, Inc. All rights reserved