NumPy spacing()

The numpy.spacing() function returns the distance between a given number and the nearest adjacent floating-point number, which is useful for understanding numerical precision and floating-point representation.

Syntax

</>
Copy
numpy.spacing(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeValues to find the spacing of (i.e., the difference between the given value and its nearest floating-point neighbor).
outndarray, None, or tuple of ndarray and None, optionalOptional output array where the result is stored. If None, a new array is created.
wherearray_like, optionalBoolean mask specifying which elements should be computed. Elements where where=False retain their original value.
castingstr, optionalDefines the casting behavior when computing the spacing.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalDefines the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array or a scalar representing the spacing of values in x. If x is a scalar, a scalar is returned.


Examples

1. Finding Spacing of a Single Value

Here, we compute the spacing of a single floating-point number.

</>
Copy
import numpy as np

# Define a single floating-point number
value = 1.0

# Compute the spacing
spacing_value = np.spacing(value)

# Print the result
print("Spacing of 1.0:", spacing_value)

Output:

Spacing of 1.0: 2.220446049250313e-16

The output represents the smallest positive number that can be added to 1.0 to produce a different floating-point value.

2. Finding Spacing for an Array of Values

We compute the spacing values for multiple floating-point numbers.

</>
Copy
import numpy as np

# Define an array of numbers
values = np.array([0.1, 1.0, 10.0, 100.0])

# Compute the spacing for each value
spacing_values = np.spacing(values)

# Print the results
print("Input values:", values)
print("Spacing values:", spacing_values)

Output:

Input values: [  0.1   1.   10.  100. ]
Spacing values: [1.38777878e-17 2.22044605e-16 1.77635684e-15 1.42108547e-14]

3. Using the out Parameter

Storing the output in a pre-allocated array instead of creating a new one.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([1.0, 2.0, 3.0, 4.0])

# Create an output array
output_array = np.empty_like(values)

# Compute spacing and store the result in output_array
np.spacing(values, out=output_array)

# Print the results
print("Computed spacing values:", output_array)

Output:

Computed spacing values: [2.22044605e-16 4.44089210e-16 4.44089210e-16 8.88178420e-16]

4. Using the where Parameter

Computing spacing only for selected elements based on a condition.

</>
Copy
import numpy as np

# Define an array of values
values = np.array([0.1, 1.0, 10.0, 100.0])

# Define a mask (compute spacing only where mask is True)
mask = np.array([True, False, True, False])

# Compute spacing values where mask is True
result = np.spacing(values, where=mask)

# Print the results
print("Computed spacing values with mask:", result)

Output:

Computed spacing values with mask: [1.38777878e-17 0.00000000e+00 1.77635684e-15 0.00000000e+00]

The spacing values are computed only for elements where mask=True. The other values remain unchanged.