-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-120010: fix invalid (nan+nanj) results in _Py_c_prod() #120287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
399e3e3
324c602
fcafe33
5509a2d
0e98116
338176d
83456ef
219c0cb
e368f23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Correct invalid corner cases (resulted in ``(nan+nanj)`` output) in complex | ||
multiplication, e.g. ``(1e300+1j)*(nan+infj)``. Patch by Sergey B Kirpichev. | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,11 +53,66 @@ _Py_c_neg(Py_complex a) | |
} | ||
|
||
Py_complex | ||
_Py_c_prod(Py_complex a, Py_complex b) | ||
_Py_c_prod(Py_complex z, Py_complex w) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be ok now. This doesn't require a mixed-mode variant. Yet, maybe I should change variable names back for consistency with the rest? Current naming here follows to the C standard. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @serhiy-storchaka what do you think about renaming of arguments? I think mapping local variables "abcd" -> "tuvw" will hot hurt readability and then we can preserve original argument names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am fine with both variants. Preserving names from the old code can make the diff smaller, but in this case the new code is too different from the old code. Using names from the C standard examples helps comparing our code with the original code. The latter looks more important here. |
||
{ | ||
Py_complex r; | ||
r.real = a.real*b.real - a.imag*b.imag; | ||
r.imag = a.real*b.imag + a.imag*b.real; | ||
double a = z.real, b = z.imag, c = w.real, d = w.imag; | ||
double ac = a*c, bd = b*d, ad = a*d, bc = b*c; | ||
|
||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
r.real = ac - bd; | ||
r.imag = ad + bc; | ||
|
||
/* Recover infinities that computed as nan+nanj. See e.g. the C11, | ||
Annex G.5.2, routine _Cmultd(). */ | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (isnan(r.real) && isnan(r.imag)) { | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int recalc = 0; | ||
|
||
if (isinf(a) || isinf(b)) { /* z is infinite */ | ||
/* "Box" the infinity and change nans in the other factor to 0 */ | ||
a = copysign(isinf(a) ? 1.0 : 0.0, a); | ||
b = copysign(isinf(b) ? 1.0 : 0.0, b); | ||
if (isnan(c)) { | ||
c = copysign(0.0, c); | ||
} | ||
if (isnan(d)) { | ||
d = copysign(0.0, d); | ||
} | ||
recalc = 1; | ||
} | ||
if (isinf(c) || isinf(d)) { /* w is infinite */ | ||
/* "Box" the infinity and change nans in the other factor to 0 */ | ||
c = copysign(isinf(c) ? 1.0 : 0.0, c); | ||
d = copysign(isinf(d) ? 1.0 : 0.0, d); | ||
if (isnan(a)) { | ||
a = copysign(0.0, a); | ||
} | ||
if (isnan(b)) { | ||
b = copysign(0.0, b); | ||
} | ||
recalc = 1; | ||
} | ||
if (!recalc && (isinf(ac) || isinf(bd) || isinf(ad) || isinf(bc))) { | ||
/* Recover infinities from overflow by changing nans to 0 */ | ||
if (isnan(a)) { | ||
a = copysign(0.0, a); | ||
} | ||
if (isnan(b)) { | ||
b = copysign(0.0, b); | ||
} | ||
if (isnan(c)) { | ||
c = copysign(0.0, c); | ||
} | ||
if (isnan(d)) { | ||
d = copysign(0.0, d); | ||
} | ||
recalc = 1; | ||
} | ||
if (recalc) { | ||
r.real = Py_INFINITY*(a*c - b*d); | ||
r.imag = Py_INFINITY*(a*d + b*c); | ||
} | ||
} | ||
|
||
return r; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.