Repository navigation
Preserve signed imaginary zero in complex sqrt - #1433
Open
agourakis82 wants to merge 1 commit into
Open
agourakis82 wants to merge 1 commit into
agourakis82 wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For finite real inputs on the real axis,
xsimd::sqrtdiscards the sign of the imaginary zero. For example,sqrt(complex(-4, -0))returns(0, 2)instead of(0, -2), selecting the wrong side of the branch cut. Positive real inputs and complex zero also lose an imaginary-0.A standalone consumer also demonstrates a value-level consequence: composing the vectorized root with scalar
std::exp(i * root)for(-4, -0)produces approximately 0.135335 before the fix, whereas the scalar reference produces 7.389056. After the fix the composition agrees with the reference. This is a synthetic composition that isolates the sqrt defect, not a claim of an observed failure in a downstream application.Preserve the imaginary zero for nonnegative real inputs and use
copysignfor the imaginary root of negative real inputs. The real component remains nonnegative, including for a negative real zero.The regression compares both components and their sign bits against
std::sqrtfor float and double, using mixed SIMD lanes. It covers both signs of imaginary zero with negative and positive normal values, the smallest subnormal, the largest finite value, and both real zeros. It is disabled under__FAST_MATH__, where signed-zero behavior is not guaranteed.This addresses the finite real-axis
sqrtcases from #1432. The other functions and non-real-axis cases reported there remain outside this change; this PR should not close the issue.Native validation with GCC 13.3, C++23 and doctest 2.4.9:
test_complex_power.cpppass in SSE2 and AVX2 builds.TARGET_ARCH=sse2: 434 doctest cases, 78,104 assertions; CTest 1/1.std::sqrt, while the other reported failures remain visible.These are native Linux x86-64 results. Other architectures and official CI remain to be checked.
AI assistance was used to investigate the report, prepare the patch, and run validation.