Skip to content

Fix complex angle usage in toinf and add off-axis tests - #85

Open
PatrickHaecker wants to merge 3 commits into
JuliaMath:masterfrom
PatrickHaecker:complex_angle
Open

Fix complex angle usage in toinf and add off-axis tests#85
PatrickHaecker wants to merge 3 commits into
JuliaMath:masterfrom
PatrickHaecker:complex_angle

Conversation

@PatrickHaecker

Copy link
Copy Markdown
Contributor

The different units, half-turns and radians, are indeed incorrectly mixed in toinf leading to an error of a factor of π. The only test covering this line was an angle of 0

This PR fixes this and adds some off-axis tests to cover these things with the tests in the future.

Thanks for triggering the investigation with your review finding, @dlfivefifty.

@codecov

codecov Bot commented Sep 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (13f9e1f) to head (7d555d3).

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #85   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          266       266           
=========================================
  Hits           266       266           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Patrick Häcker added 3 commits September 6, 2026 07:01
`_sb` was short for `signbit`. The function returns a count of half turns.
For a real number that count is the sign bit, which is where the old name
came from.

The new name says which unit the caller gets.
`ComplexInfinity` stores its direction in half turns, but `toinf` filled the
field with the radian `angle(x)`. The direction of an infinite complex
summand was therefore off by a factor of π: `angle(toinf(complex(0,Inf)))`
gave 4.93 instead of `π/2`.

`_infadd` compares those angles, so a sum whose parts point the same way
threw although `==` called them equal. Both `complex(-Inf,0.0) + -∞` and
`complex(0,Inf) + ComplexInfinity(0.5)` raised an ArgumentError. Only the
positive real axis escaped, angle `0` being the fixed point of the missing
scaling.

`_halfturns` is the conversion the multiplication already uses. It moves
above the addition section, because both sections use it now.
`exp(im*π/4)*∞ == Inf+im*Inf` held by coincidence. `angle(x)/π` round-trips
exactly only on the axes, where `angle` returns exactly `0`, `±π/2` or `π`.
Off the axes `π/4` happens to come back unchanged, while the adjacent `π/8`
gives 0.12500000000000003 and a sweep of `k/2^12` misses 2000 of 8192 angles.
That assertion now compares the angle with `≈`.

The four axes go through one loop, which also compares the infinity with the
infinite `Complex` itself. That comparison is the only test in the suite
reaching `==(::AllInfinities, ::Complex)`, and on an axis it is exact.

Two assertions say what a rescaling does. `angle` is scale invariant wherever
the scaling itself is exact, so a direction only moves when the rescaled
operand has to be rounded.
@PatrickHaecker

Copy link
Copy Markdown
Contributor Author

I updated and rebased the branch so that it fits best to the other PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant