Repository navigation
A compiled function takes the side of its branch cut evaluation takes - #2240
Merged
Merged
Conversation
System.Numerics.Complex computes -0 as a part of many real values. cos(2) is (cos 2, -sin 2 * sinh 0), which is (-0.416, -0), and conj(-2) is (-2, -0). Complex.Log, Complex.Sqrt and the inverse functions read a -0 as the lower side of the negative real axis. The library has no signed zero, so a real number is on the upper side. So in both compilers, ln(cos(x)) at 2 was -0.8767 - pi i where evaluation gives -0.8767 + pi i, and sqrt(conj(x)) at -4 was -2i. ComplexBranches.Unsigned reads each zero part as +0, by adding +0.0, which turns -0 into +0 and changes no other number. It is taken at the entry of every compiled function with a cut, in FastExpression and in MathAllMethods: - the logarithm; - a power that is not whole, in RealProducts.Power; - arcsin, arccos, arctan, arccotan, arcsec and arccosec; - arg; - the exponential and logarithmic integrals; - lambertw. ComplexBranches.Arcsin already conjugated on its cut, and now does so from the +0 side, which is what it was written for. FastExpression's logarithm was Complex.Log(value, base.Real), whose base parameter is a double, so a complex base lost its imaginary part: log(x, 2) at 1 + i was NaN. It is ComplexBranches.Log now, ln(value) / ln(base), as the LINQ compiler already took it (#2239). BranchCutCompilationTest takes each of the 19 functions at 11 real points, with its argument written three ways that land on a cut: conj(x), cos(x) and i * x. It compares both compilers with evaluation, and adds three logarithms to complex bases. 9 of its 22 cases fail on master. A pole is left out, since there the infinity has a NaN part, which is not what this is about. So is an exponent with an odd denominator, where evaluation takes the real root, which a double exponent cannot show. Fixes #2236. Fixes #2239. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2240 +/- ##
==========================================
+ Coverage 86.68% 86.70% +0.02%
==========================================
Files 310 310
Lines 67766 67773 +7
Branches 19660 19660
==========================================
+ Hits 58744 58764 +20
+ Misses 4909 4896 -13
Partials 4113 4113
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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.
Fixes #2236. Fixes #2239. Both were found by
work/compilesweep.The branch cuts (#2236).
System.Numerics.Complexcomputes-0as a part of many real values:cos(2)is(cos 2, -sin 2 · sinh 0), which is(-0.416, -0), andconj(-2)is(-2, -0).Complex.Log,Complex.Sqrtand the inverse functions read a-0as the lower side of the negative real axis. The library has no signed zero, so a real number is on the upper side. So in both compilersln(cos(x))at 2 was-0.8767 - pi i, where evaluation gives-0.8767 + pi i, andsqrt(conj(x))at -4 was-2i.ComplexBranches.Unsignedreads each zero part as+0, by adding+0.0, which turns-0into+0and changes no other number. It is taken at the entry of every compiled function with a cut, in FastExpression and inMathAllMethods:RealProducts.Power;arcsin,arccos,arctan,arccotan,arcsecandarccosec;arg;lambertw.ComplexBranches.Arcsinalready conjugated on its cut. It now does so from the+0side, which is the case it was written for.The logarithm's base (#2239). FastExpression's logarithm was
Complex.Log(value, base.Real), whose base parameter is adouble, so a complex base lost its imaginary part:log(x, 2)at1 + iwasNaN. It is nowComplexBranches.Log,ln(value) / ln(base), which is how the LINQ compiler already took it."ln(cos(x))".ToEntity().Compile("x").Call(2)(-0.8767, -3.1416)(-0.8767, 3.1416)"log(x, 2)".ToEntity().Compile("x").Call(new Complex(1, 1))(NaN, NaN)(0.3260, -0.7387)Tests.
BranchCutCompilationTesttakes each of the 19 functions at 11 real points, with its argument written three ways that land on a cut:conj(x),cos(x)andi * x. It compares both compilers with evaluation, and adds three logarithms to complex bases. 9 of its 22 cases fail on master. Two kinds of point are left out:NaNpart, which is not what this test is about;Full suite 18135/0, interactive trio, AOT smoke 73/0, all frameworks build.
🤖 Generated with Claude Code