You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Writing the spec for complex number support in the Array API we found that NumPy currently fails in 2 special cases. The behavior is inconsistent with the 2014 version C99.
importnumpyasnpimportmathdefis_equal_float(x, y):
"""Test whether two floating-point numbers are equal with special consideration for zeros and NaNs. Parameters ---------- x : float First input number. y : float Second input number. Returns ------- bool Boolean indicating whether two floating-point numbers are equal. Examples -------- >>> is_equal_float(0.0, -0.0) False >>> is_equal_float(-0.0, -0.0) True """# Handle +-0:ifx==0.0andy==0.0:
returnmath.copysign(1.0, x) ==math.copysign(1.0, y)
# Handle NaNs:ifx!=x:
returny!=y# Everything else, including infinities:returnx==ydefis_equal(x, y):
"""Test whether two complex numbers are equal with special consideration for zeros and NaNs. Parameters ---------- x : complex First input number. y : complex Second input number. Returns ------- bool Boolean indicating whether two complex numbers are equal. Examples -------- >>> import numpy as np >>> is_equal(complex(np.nan, np.nan), complex(np.nan, np.nan)) True """returnis_equal_float(x.real, y.real) andis_equal_float(x.imag, y.imag)
defcompare(v, e):
actual=np.tanh(v)
print('Value: {value}'.format(value=str(v)))
print('Actual: {actual}'.format(actual=str(actual)))
print('Expected: {expected}'.format(expected=str(e)))
print('Equal: {is_equal}'.format(is_equal=str(is_equal(actual, e))))
print('\n')
# Case 1v=complex(0.0, np.inf)
e=complex(0.0, np.nan)
compare(v, e) # returns `NaN + NaN j`; however, this does match old C99 behavior (see https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1892.htm#dr_471)# Case 2v=complex(0.0, np.nan)
e=complex(0.0, np.nan)
compare(v, e) # returns `NaN + NaN j`; however, this does match old C99 behavior (see https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1892.htm#dr_471)
On most platforms we presumably use the C99 function (I am not certain about all platforms, since we do have a fallback).
If the provided math libraries fail to adhere to C99 here, I don't really think we have to make it a priority to work around the issue (unless maybe users actually run into it). We certainly can for our fallback version of course.
But, I don't really see that we should blocklist a provided version or prioritize working around the special cases for such niche use-cases.
In the current proposed changes to the array API standard adding complex number support to tanh, array libraries which compile against older versions of C are not required to patch those versions in order to address these two special cases.
After digging a little bit and trying these cases in multiple OS with different architectures I found that this corner cases are present in macOS Intel, macOS ARM and Windows x64, but the corner cases are working as expected in Linux Intel and Linux ARM so probably the issue is related with the compiler of C99.
Describe the issue:
Writing the spec for complex number support in the Array API we found that NumPy currently fails in 2 special cases. The behavior is inconsistent with the 2014 version C99.
The full list of special cases and specification is detailed in data-apis/array-api#458
cc @kgryte
Reproduce the code example:
Error message:
NumPy/Python version information:
v1.22.4
The text was updated successfully, but these errors were encountered: