Skip to content

Add a ForwardDiff.jl extension - #90

Draft
PatrickHaecker wants to merge 7 commits into
JuliaMath:masterfrom
PatrickHaecker:forwarddiff_extension
Draft

PatrickHaecker wants to merge 7 commits into
JuliaMath:masterfrom
PatrickHaecker:forwarddiff_extension

Conversation

@PatrickHaecker

Copy link
Copy Markdown
Contributor

This would benefit from someone looking at it who knows ForwardDiff.jl better than I do.

Fixes: #13

Builds on #88.

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (08ccb94) to head (4f7ba7f).

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #90   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         8    +2     
  Lines          336       383   +47     
=========================================
+ Hits           336       383   +47     

☔ 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 25, 2026 10:46
A direction is an angle modulo a full turn, so the field now counts turns in
units of 2^-64 and wraps where the circle does. Every `UInt64` names a
direction and every direction has exactly one count, which the old half-turn
field did not manage: half turns of 0.5 and 2.5 pointed the same way yet
compared unequal and hashed apart. Wrapping also makes the group operation a
machine add, and the resolution is uniform instead of thinning out towards a
full turn.

The count is what the constructor takes. An angle has to be rounded to reach
it, so that step is now named at the call site with the `halfturns` keyword,
and the old `ComplexInfinity(0.5)` is a `MethodError` rather than a silent
reinterpretation. Most code needs neither form, since multiplying by `∞`
takes the direction from the other operand: `im*∞` and `(1+im)*∞`.

The element type carried no information about the value, only about how the
direction had been spelled, so it is gone. It had been standing in for "this
infinity lies on the real axis", and that was never what it meant: a zero
angle written as a float pointed along the positive real axis just as
`ComplexInfinity()` does, yet only the latter could be ordered or divided.
The union types that used the parameter as a proxy now leave a `ComplexInfinity`
out entirely, so the complex plane carries no order and takes no integer
operation, exactly as `Base` treats a `Complex`.

The count has 64 bits and an angle in a `Float64` has 53, so the two are kept
apart wherever the difference shows. Equality and `hash` read the count, never
the angle. `angle` reports on `Base`'s branch of `(-π, π]`. And `show` gives
the readable `cispi(h)∞` only where that reads back, and the count itself
otherwise, so every printed form evaluates to the value it came from.
Dropping the element type removed the only way to ask a `ComplexInfinity`
whether it points along the real axis, and that question was worth keeping:
it just belongs to the value rather than to the type. `isreal` takes it over,
and `RealInfinity` converts a direction that lies on the axis and throws an
`InexactError` otherwise, which is what `Real` does with a `Complex`.

So an order or an integer operation is still reachable, by naming the
conversion, and an infinity off the axis throws instead of quietly behaving
as if the imaginary part were not there.
`Base` returns `NaN` where two reals have no result and `NaN + NaN*im` where
either operand is complex, and this package only did half of that. Anything
computed *from* a `NotANumber` already came back complex against a complex
operand, but every place that *produced* one returned the real value, so
`NaN * ComplexInfinity()` and `NotANumber() * ComplexInfinity()` disagreed.

`_undefined` picks the value from the two operands and now stands wherever an
undefined result is made: a `NaN` argument, a zero times an infinity, two
infinities divided, and two infinities added in different directions. Only
five of those sites can see a complex operand at all; for the rest the choice
is decided at compile time and costs nothing.
Patrick Häcker added 4 commits September 25, 2026 14:00
This takes over ideas from JuliaMath#20 created by @cscherrer. So thanks for that contribution!
As the PR is 5 years old, quite some things are now obsolete.

A nice side-effect was finding and fixing a missing `infpromote` for `Bool`.
This would benefit from someone looking at it who knows ForwardDiff
better than I do.

Fixes: JuliaMath#13
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.

Ambiguity with ForwardDiff

1 participant