What is the numpy.spacing() function in NumPy?

Overview

The numpy.spacing() function in NumPy is used to compute the distance between an input value x and the nearest adjacent number.

Syntax

numpy.spacing(x, /, out=None, *, where=True)
The syntax for the numpy.spacing() function

Parameter value

The numpy.spacing() function takes the following parameter values.

  • x: This represents the input value whose spacing is to be returned and is a required parameter.
  • out: This represents the location where the result is stored and is an optional parameter. 
  • where: This is the condition over which the input is being broadcasted. At a given location where this condition is True, the resulting array will be set to the ufunc result. Otherwise, the resulting array will retain its original value. This is an optional parameter.
  • **kwargs: This represents other keyword arguments and is an optional parameter.

Return value

It returns a scalar value which represents the distance between an input value x and the nearest adjacent number.

Example

import numpy as np
# implementing the numpy.spacing() function
print(np.spacing(5))

Explanation

  • Line 1: We import the numpy module.
  • Line 3: We implement the numpy.spacing function to determine the distance between 5 and the nearest adjacent number. We print the result to the console.

Free Resources