Add Complex<T> generic struct mirroring Complex API surface#127333
Draft
Copilot wants to merge 4 commits intocopilot/add-generic-complex-supportfrom
Draft
Add Complex<T> generic struct mirroring Complex API surface#127333Copilot wants to merge 4 commits intocopilot/add-generic-complex-supportfrom
Complex<T> generic struct mirroring Complex API surface#127333Copilot wants to merge 4 commits intocopilot/add-generic-complex-supportfrom
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
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,InfinityReal,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/CreateTruncatingeach delegate to the matchingT.TryConvertFrom{Checked,Saturating,Truncating}so saturation/truncation semantics are correct 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?)As a result, the now-unused private fields (
s_sqrtRescaleThreshold,s_asinOverflowThreshold,s_log2) and helpers (Log1P,Asin_Internal) are removed fromComplex.cs.Example usage
Files changed
src/System/Numerics/Complex.csDefaultNumberStyleandInvalidNumberStylestointernal; delegate long methods toComplex<double>src/System/Numerics/Complex.Generic.csComplex<T>implementation withGetMagnitude()/GetPhase()methodssrc/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.