Add Complex<T> generic struct mirroring Complex API surface#127333
Merged
tannergooding merged 8 commits intoMay 14, 2026
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/6ae7397b-1811-41d7-a5d8-55ef50af02cd Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add Complex<T> and IComplexNumber<TSelf, T> API surface
Add Apr 23, 2026
Complex<T> generic struct mirroring Complex API surface
…se in Complex<T> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/e09763ab-e8b2-4ccf-8029-2cc8c6dbd47a Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
… Complex.cs methods >4 lines to Complex<double> Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/4edc4c27-249d-40a9-92b4-fda815b84714 Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
This was referenced Apr 24, 2026
Open
…e to Complex<T>; delegate Complex explicit impls to them Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/423a9dba-89a1-41d2-8884-5915b7ffc74d Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
…-type conversion; share via Complex.TryConvertFrom delegation Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/0b63e9f7-7efe-4068-b899-dfda38c4dc5a Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
…T>; replace Complex.cs TryConvertTo* chains with delegation Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/ee27c47e-3787-4c63-9823-23e289245e0e Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
…s to static properties in Complex<T>; remove redundant INumberBase.One/Zero explicit impls Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/4daa6a5a-87a9-46b6-b055-56ce91e228a2 Co-authored-by: tannergooding <10487869+tannergooding@users.noreply.github.com>
tannergooding
approved these changes
May 13, 2026
eiriktsarpalis
approved these changes
May 13, 2026
lilinus
reviewed
May 13, 2026
1997f10
into
copilot/add-generic-complex-support
77 of 80 checks passed
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.
Implements a generic
Complex<T> where T : IFloatingPointIeee754<T>, IMinMaxValue<T>that mirrors the full public API of the existingComplexstruct, as scoped by @tannergooding. TheIComplexNumber<TSelf, T>interface and new members on non-genericComplexare deferred to a follow-up PR.Description
New type:
Complex<T>Direct generic port of
Complexin a newComplex.Generic.cssource file:Complex<T>×Complex<T>,T×Complex<T>, andComplex<T>×Toverloads for+,-,*,/, unary-/+,++,--Zero,One,ImaginaryOne,NaN,Infinity(exposed asstaticproperties per the approved API surface;INumberBase.OneandINumberBase.Zerobind implicitly to the matching public properties)Real,ImaginaryGetMagnitude(),GetPhase()(per approved API surface — exposed as named methods rather than properties)Abs,Conjugate,Reciprocal,Log/Log10,Exp,Sqrt,Pow,Sin/Cos/Tan,Sinh/Cosh/Tanh,Asin/Acos/Atan,FromPolarCoordinatesIs*set matchingINumberBase<T>INumberBase<Complex<T>>,ISignedNumber<Complex<T>>,IAdditiveIdentity,IMultiplicativeIdentity,IEquality/ComparisonoperatorsIParsable,ISpanParsable,IUtf8SpanParsable,ISpanFormattable,IUtf8SpanFormattableCreateChecked/CreateSaturating/CreateTruncatinguseinternalTryConvertFrom*CoreandTryConvertTo*Corehelpers that correctly chain same-type, cross-type (Complex↔Complex<T>),BigInteger, and general numeric type special cases per modeConstraint rationale
IMinMaxValue<T>is added alongsideIFloatingPointIeee754<T>because the portedSqrtandAsin_Internalalgorithms compute per-instantiation overflow thresholds fromT.MaxValue(e.g.,s_sqrtRescaleThreshold,s_asinOverflowThreshold). Every practical floating-point type (float,double,Half,BFloat16,NFloat) implements both interfaces.Shared constants
Complex.DefaultNumberStyleandComplex.InvalidNumberStylesare promoted fromprivatetointernalinComplex.csso thatComplex<T>can reference them directly rather than maintaining duplicate definitions.Code sharing between
ComplexandComplex<T>To ensure the non-generic
ComplexandComplex<T>stay in sync, allComplexmethods with more than 4 lines of implementation now delegate to the correspondingComplex<double>method. This covers:operator *(Complex, double),operator *(double, Complex), and all three/overloadsAsin,Acos,Tan,Sqrt,Pow(Complex, Complex)MaxMagnitude,MinMagnitudeToString(string?, IFormatProvider?)MaxMagnitudeNumber,MinMagnitudeNumber,MultiplyAddEstimate: each has aninternal staticimplementation onComplex<T>;Complex<T>'s explicit interface implementation delegates to it, andComplex's explicit interface implementation calls throughComplex<double>TryConvertFrom*:Complex<T>exposesinternal static TryConvertFromCheckedCore,TryConvertFromSaturatingCore,TryConvertFromTruncatingCorethat implement the full conversion logic (same-type,Complex→Complex<T>, signed types viaT.TryConvertFrom*, unsigned/decimal types viaTOther.TryConvertTo*<T>);Complex.TryConvertFromreplaces its long explicit type chain with aComplex<double>special case followed by delegation toComplex<double>.TryConvertFromCheckedCoreTryConvertTo*:Complex<T>exposesinternal static TryConvertToCheckedCore,TryConvertToSaturatingCore,TryConvertToTruncatingCorethat implement the full conversion logic (same-type fast path returning real/NaN,Complexoutput viadouble.Create*,BigIntegeroutput with appropriate imaginary-part handling, general types viaT.TryConvertTo*with fallback toTOther.TryConvertFrom*<T>);Complex's three longTryConvertTo*chains are replaced with aComplex<double>output special case followed by delegation toComplex<double>.TryConvertTo*CoreAs a result, the now-unused private fields (
s_sqrtRescaleThreshold,s_asinOverflowThreshold,s_log2) and helpers (Log1P,Asin_Internal) are removed fromComplex.cs.Cross-type conversion support
Both types now correctly support bi-directional conversion:
Complex.CreateChecked(new Complex<double>(1, 2))—ComplexacceptsComplex<double>inputComplex<float>.CreateChecked(new Complex(1, 2))—Complex<T>acceptsComplex(non-generic) input, with mode-appropriate component conversionint.CreateChecked(new Complex<double>(5, 0))— real types can be extracted fromComplex<T>viaTryConvertToCheckedCoreint.CreateChecked(new Complex(5, 0))— real types can be extracted fromComplexvia delegation toComplex<double>.TryConvertToCheckedCoreExample usage
Files changed
src/System/Numerics/Complex.csDefaultNumberStyleandInvalidNumberStylestointernal; delegate long methods toComplex<double>; replaceTryConvertFrom/TryConvertTo*chains withComplex<double>.*Coredelegationsrc/System/Numerics/Complex.Generic.csComplex<T>implementation withstaticproperties forZero/One/ImaginaryOne/NaN/Infinity;GetMagnitude()/GetPhase()methods;internal staticshared implementations forMaxMagnitudeNumber,MinMagnitudeNumber,MultiplyAddEstimate,TryConvertFrom*Core, andTryConvertTo*Coresrc/System.Runtime.Numerics.csprojref/System.Runtime.Numerics.csComplex<T>public surface to ref assemblytests/ComplexTests.csComplexTests_Genericwith 37 testsNote
This PR description was authored by GitHub Copilot.