This is a pre-release version (latest). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy latest / numpy / real_if_close

_ArrayFunctionDispatcher

numpy:real_if_close

source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/lib/_type_check_impl.py :500

Signature

def   real_if_close ( a tol = 100 )

Summary

If input is complex with all imaginary parts close to zero, return real parts.

Extended Summary

"Close to zero" is defined as tol * (machine epsilon of the type for a).

Parameters

a : array_like

Input array.

tol : float

Tolerance in machine epsilons for the complex part of the elements in the array. If the tolerance is <=1, then the absolute tolerance is used.

Returns

out : ndarray

If a is real, the type of a is used for the output. If a has complex elements, the returned type is float.

Notes

Machine epsilon varies from machine to machine and between data types but Python floats on most platforms have a machine epsilon equal to 2.2204460492503131e-16. You can use 'np.finfo(float).eps' to print out the machine epsilon for floats.

Examples

import numpy as np
np.finfo(float).eps
np.real_if_close([2.1 + 4e-14j, 5.2 + 3e-15j], tol=1000)
np.real_if_close([2.1 + 4e-13j, 5.2 + 3e-15j], tol=1000)

See also

angle
imag
real

Aliases

  • numpy.real_if_close