Repository navigation
Preserve signed imaginary zero in complex sqrt #1433
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -2323,9 +2323,9 @@ namespace xsimd | |
| real_batch ze(0.); | ||
|
|
||
| return select(y == ze, | ||
| select(x == ze, | ||
| batch_type(ze, ze), | ||
| select(x < ze, batch_type(ze, sqrt_x), batch_type(sqrt_x, ze))), | ||
| select(x < ze, | ||
| batch_type(ze, copysign(sqrt_x, y)), | ||
|
Contributor
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. mmmh, that part looks strange, why do you change the sign of
Author
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. Here sqrt_x is sqrt(abs(x)). In the y == 0 && x < 0 branch, it becomes the magnitude of the imaginary component, so it does not need to be zero. For example, sqrt(complex(-4, -0)) should return (0, -2), whereas sqrt(complex(-4, +0)) should return (0, +2). The copysign(sqrt_x, y) selects the corresponding side of the negative-real-axis branch cut. Without it, both cases would return a positive imaginary component. The change preserves the nonnegative real component; it applies the sign of y to the imaginary component in this branch. |
||
| batch_type(sqrt_x, y)), | ||
| select(x == ze, | ||
| select(y > ze, batch_type(sqrt_hy, sqrt_hy), batch_type(sqrt_hy, -sqrt_hy)), | ||
| resg)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how this also should make the code denser by avoiding a
select.The logic looks good too.