Skip to content

Commit 8962e24

Browse files
Refactoring which numeric interface exposes which methods to match latest API review decisions (#69582)
* Refactoring which numeric interface exposes which methods to match latest API review decisions * Refactoring the generic math tests to have a consistent order, allowing coverage to be more easily seen * Adding test coverage for the APIs that were moved down to INumber and INumberBase * Adding generic-math tests for System.Numerics.Complex * Fixing some copy/paste issues that came about from resolving the Int128/UInt128 merge conflicts
1 parent 4955138 commit 8962e24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+21449
-14179
lines changed

src/libraries/Common/tests/System/GenericMathHelpers.cs

Lines changed: 207 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public static class BinaryIntegerHelper<TSelf>
4242
public static bool TryWriteBigEndian(TSelf value, Span<byte> destination, out int bytesWritten) => value.TryWriteBigEndian(destination, out bytesWritten);
4343

4444
public static bool TryWriteLittleEndian(TSelf value, Span<byte> destination, out int bytesWritten) => value.TryWriteLittleEndian(destination, out bytesWritten);
45+
46+
public static int WriteBigEndian(TSelf value, byte[] destination) => value.WriteBigEndian(destination);
47+
48+
public static int WriteBigEndian(TSelf value, byte[] destination, int startIndex) => value.WriteBigEndian(destination, startIndex);
49+
50+
public static int WriteBigEndian(TSelf value, Span<byte> destination) => value.WriteBigEndian(destination);
51+
52+
public static int WriteLittleEndian(TSelf value, byte[] destination) => value.WriteLittleEndian(destination);
53+
54+
public static int WriteLittleEndian(TSelf value, byte[] destination, int startIndex) => value.WriteLittleEndian(destination, startIndex);
55+
56+
public static int WriteLittleEndian(TSelf value, Span<byte> destination) => value.WriteLittleEndian(destination);
4557
}
4658

4759
public static class BinaryNumberHelper<TSelf>
@@ -100,9 +112,39 @@ public static class EqualityOperatorsHelper<TSelf, TOther>
100112
public static bool op_Inequality(TSelf left, TOther right) => left != right;
101113
}
102114

115+
public static class ExponentialFunctionsHelper<TSelf>
116+
where TSelf : IExponentialFunctions<TSelf>
117+
{
118+
public static TSelf Exp(TSelf x) => TSelf.Exp(x);
119+
120+
public static TSelf ExpM1(TSelf x) => TSelf.ExpM1(x);
121+
122+
public static TSelf Exp2(TSelf x) => TSelf.Exp2(x);
123+
124+
public static TSelf Exp2M1(TSelf x) => TSelf.Exp2M1(x);
125+
126+
public static TSelf Exp10(TSelf x) => TSelf.Exp10(x);
127+
128+
public static TSelf Exp10M1(TSelf x) => TSelf.Exp10M1(x);
129+
}
130+
103131
public static class FloatingPointHelper<TSelf>
104132
where TSelf : IFloatingPoint<TSelf>
105133
{
134+
public static TSelf Ceiling(TSelf x) => TSelf.Ceiling(x);
135+
136+
public static TSelf Floor(TSelf x) => TSelf.Floor(x);
137+
138+
public static TSelf Round(TSelf x) => TSelf.Round(x);
139+
140+
public static TSelf Round(TSelf x, int digits) => TSelf.Round(x, digits);
141+
142+
public static TSelf Round(TSelf x, MidpointRounding mode) => TSelf.Round(x, mode);
143+
144+
public static TSelf Round(TSelf x, int digits, MidpointRounding mode) => TSelf.Round(x, digits, mode);
145+
146+
public static TSelf Truncate(TSelf x) => TSelf.Truncate(x);
147+
106148
public static int GetExponentByteCount(TSelf value) => value.GetExponentByteCount();
107149

108150
public static int GetExponentShortestBitLength(TSelf value) => value.GetExponentShortestBitLength();
@@ -118,6 +160,82 @@ public static class FloatingPointHelper<TSelf>
118160
public static bool TryWriteSignificandBigEndian(TSelf value, Span<byte> destination, out int bytesWritten) => value.TryWriteSignificandBigEndian(destination, out bytesWritten);
119161

120162
public static bool TryWriteSignificandLittleEndian(TSelf value, Span<byte> destination, out int bytesWritten) => value.TryWriteSignificandLittleEndian(destination, out bytesWritten);
163+
164+
public static int WriteExponentBigEndian(TSelf value, byte[] destination) => value.WriteExponentBigEndian(destination);
165+
166+
public static int WriteExponentBigEndian(TSelf value, byte[] destination, int startIndex) => value.WriteExponentBigEndian(destination, startIndex);
167+
168+
public static int WriteExponentBigEndian(TSelf value, Span<byte> destination) => value.WriteExponentBigEndian(destination);
169+
170+
public static int WriteExponentLittleEndian(TSelf value, byte[] destination) => value.WriteExponentLittleEndian(destination);
171+
172+
public static int WriteExponentLittleEndian(TSelf value, byte[] destination, int startIndex) => value.WriteExponentLittleEndian(destination, startIndex);
173+
174+
public static int WriteExponentLittleEndian(TSelf value, Span<byte> destination) => value.WriteExponentLittleEndian(destination);
175+
176+
public static int WriteSignificandBigEndian(TSelf value, byte[] destination) => value.WriteSignificandBigEndian(destination);
177+
178+
public static int WriteSignificandBigEndian(TSelf value, byte[] destination, int startIndex) => value.WriteSignificandBigEndian(destination, startIndex);
179+
180+
public static int WriteSignificandBigEndian(TSelf value, Span<byte> destination) => value.WriteSignificandBigEndian(destination);
181+
182+
public static int WriteSignificandLittleEndian(TSelf value, byte[] destination) => value.WriteSignificandLittleEndian(destination);
183+
184+
public static int WriteSignificandLittleEndian(TSelf value, byte[] destination, int startIndex) => value.WriteSignificandLittleEndian(destination, startIndex);
185+
186+
public static int WriteSignificandLittleEndian(TSelf value, Span<byte> destination) => value.WriteSignificandLittleEndian(destination);
187+
}
188+
189+
public static class FloatingPointIeee754Helper<TSelf>
190+
where TSelf : IFloatingPointIeee754<TSelf>
191+
{
192+
public static TSelf E => TSelf.E;
193+
194+
public static TSelf Epsilon => TSelf.Epsilon;
195+
196+
public static TSelf NaN => TSelf.NaN;
197+
198+
public static TSelf NegativeInfinity => TSelf.NegativeInfinity;
199+
200+
public static TSelf NegativeZero => TSelf.NegativeZero;
201+
202+
public static TSelf Pi => TSelf.Pi;
203+
204+
public static TSelf PositiveInfinity => TSelf.PositiveInfinity;
205+
206+
public static TSelf Tau => TSelf.Tau;
207+
208+
public static TSelf BitDecrement(TSelf x) => TSelf.BitDecrement(x);
209+
210+
public static TSelf BitIncrement(TSelf x) => TSelf.BitIncrement(x);
211+
212+
public static TSelf FusedMultiplyAdd(TSelf left, TSelf right, TSelf addend) => TSelf.FusedMultiplyAdd(left, right, addend);
213+
214+
public static TSelf Ieee754Remainder(TSelf left, TSelf right) => TSelf.Ieee754Remainder(left, right);
215+
216+
public static int ILogB(TSelf x) => TSelf.ILogB(x);
217+
218+
public static TSelf ReciprocalEstimate(TSelf x) => TSelf.ReciprocalEstimate(x);
219+
220+
public static TSelf ReciprocalSqrtEstimate(TSelf x) => TSelf.ReciprocalSqrtEstimate(x);
221+
222+
public static TSelf ScaleB(TSelf x, int n) => TSelf.ScaleB(x, n);
223+
}
224+
225+
public static class HyperbolicFunctionsHelper<TSelf>
226+
where TSelf : IHyperbolicFunctions<TSelf>
227+
{
228+
public static TSelf Acosh(TSelf x) => TSelf.Acosh(x);
229+
230+
public static TSelf Asinh(TSelf x) => TSelf.Asinh(x);
231+
232+
public static TSelf Atanh(TSelf x) => TSelf.Atanh(x);
233+
234+
public static TSelf Cosh(TSelf x) => TSelf.Cosh(x);
235+
236+
public static TSelf Sinh(TSelf x) => TSelf.Sinh(x);
237+
238+
public static TSelf Tanh(TSelf x) => TSelf.Tanh(x);
121239
}
122240

123241
public static class IncrementOperatorsHelper<TSelf>
@@ -128,6 +246,24 @@ public static class IncrementOperatorsHelper<TSelf>
128246
public static TSelf op_CheckedIncrement(TSelf value) => checked(++value);
129247
}
130248

249+
public static class LogarithmicFunctionsHelper<TSelf>
250+
where TSelf : ILogarithmicFunctions<TSelf>
251+
{
252+
public static TSelf Log(TSelf x) => TSelf.Log(x);
253+
254+
public static TSelf Log(TSelf x, TSelf newBase) => TSelf.Log(x, newBase);
255+
256+
public static TSelf LogP1(TSelf x) => TSelf.LogP1(x);
257+
258+
public static TSelf Log2(TSelf x) => TSelf.Log2(x);
259+
260+
public static TSelf Log2P1(TSelf x) => TSelf.Log2P1(x);
261+
262+
public static TSelf Log10(TSelf x) => TSelf.Log10(x);
263+
264+
public static TSelf Log10P1(TSelf x) => TSelf.Log10P1(x);
265+
}
266+
131267
public static class MinMaxValueHelper<TSelf>
132268
where TSelf : IMinMaxValue<TSelf>
133269
{
@@ -162,58 +298,72 @@ public static class NumberBaseHelper<TSelf>
162298
public static TSelf One => TSelf.One;
163299

164300
public static TSelf Zero => TSelf.Zero;
165-
}
166301

167-
public static class NumberHelper<TSelf>
168-
where TSelf : INumber<TSelf>
169-
{
170302
public static TSelf Abs(TSelf value) => TSelf.Abs(value);
171303

172-
public static TSelf Clamp(TSelf value, TSelf min, TSelf max) => TSelf.Clamp(value, min, max);
173-
174-
public static TSelf CopySign(TSelf value, TSelf sign) => TSelf.CopySign(value, sign);
175-
176304
public static TSelf CreateChecked<TOther>(TOther value)
177-
where TOther : INumber<TOther> => TSelf.CreateChecked(value);
305+
where TOther : INumberBase<TOther> => TSelf.CreateChecked(value);
178306

179307
public static TSelf CreateSaturating<TOther>(TOther value)
180-
where TOther : INumber<TOther> => TSelf.CreateSaturating(value);
308+
where TOther : INumberBase<TOther> => TSelf.CreateSaturating(value);
181309

182310
public static TSelf CreateTruncating<TOther>(TOther value)
183-
where TOther : INumber<TOther> => TSelf.CreateTruncating(value);
311+
where TOther : INumberBase<TOther> => TSelf.CreateTruncating(value);
312+
313+
public static bool IsFinite(TSelf value) => TSelf.IsFinite(value);
314+
315+
public static bool IsInfinity(TSelf value) => TSelf.IsInfinity(value);
316+
317+
public static bool IsNaN(TSelf value) => TSelf.IsNaN(value);
184318

185319
public static bool IsNegative(TSelf value) => TSelf.IsNegative(value);
186320

187-
public static TSelf Max(TSelf x, TSelf y) => TSelf.Max(x, y);
321+
public static bool IsNegativeInfinity(TSelf value) => TSelf.IsNegativeInfinity(value);
322+
323+
public static bool IsNormal(TSelf value) => TSelf.IsNormal(value);
324+
325+
public static bool IsPositiveInfinity(TSelf value) => TSelf.IsPositiveInfinity(value);
326+
327+
public static bool IsSubnormal(TSelf value) => TSelf.IsSubnormal(value);
188328

189329
public static TSelf MaxMagnitude(TSelf x, TSelf y) => TSelf.MaxMagnitude(x, y);
190330

191-
public static TSelf Min(TSelf x, TSelf y) => TSelf.Min(x, y);
331+
public static TSelf MaxMagnitudeNumber(TSelf x, TSelf y) => TSelf.MaxMagnitudeNumber(x, y);
192332

193333
public static TSelf MinMagnitude(TSelf x, TSelf y) => TSelf.MinMagnitude(x, y);
194334

195-
public static TSelf Parse(string s, IFormatProvider provider) => TSelf.Parse(s, provider);
335+
public static TSelf MinMagnitudeNumber(TSelf x, TSelf y) => TSelf.MinMagnitudeNumber(x, y);
196336

197337
public static TSelf Parse(string s, NumberStyles style, IFormatProvider provider) => TSelf.Parse(s, style, provider);
198338

199-
public static TSelf Parse(ReadOnlySpan<char> s, IFormatProvider provider) => TSelf.Parse(s, provider);
200-
201339
public static TSelf Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider) => TSelf.Parse(s, style, provider);
202340

203-
public static int Sign(TSelf value) => TSelf.Sign(value);
204-
205341
public static bool TryCreate<TOther>(TOther value, out TSelf result)
206-
where TOther : INumber<TOther> => TSelf.TryCreate(value, out result);
207-
208-
public static bool TryParse(string s, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, provider, out result);
342+
where TOther : INumberBase<TOther> => TSelf.TryCreate(value, out result);
209343

210344
public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, style, provider, out result);
211345

212-
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, provider, out result);
213-
214346
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, style, provider, out result);
215347
}
216348

349+
public static class NumberHelper<TSelf>
350+
where TSelf : INumber<TSelf>
351+
{
352+
public static TSelf Clamp(TSelf value, TSelf min, TSelf max) => TSelf.Clamp(value, min, max);
353+
354+
public static TSelf CopySign(TSelf value, TSelf sign) => TSelf.CopySign(value, sign);
355+
356+
public static TSelf Max(TSelf x, TSelf y) => TSelf.Max(x, y);
357+
358+
public static TSelf MaxNumber(TSelf x, TSelf y) => TSelf.MaxNumber(x, y);
359+
360+
public static TSelf Min(TSelf x, TSelf y) => TSelf.Min(x, y);
361+
362+
public static TSelf MinNumber(TSelf x, TSelf y) => TSelf.MinNumber(x, y);
363+
364+
public static int Sign(TSelf value) => TSelf.Sign(value);
365+
}
366+
217367
public static class ParsableHelper<TSelf>
218368
where TSelf : IParsable<TSelf>
219369
{
@@ -222,6 +372,20 @@ public static class ParsableHelper<TSelf>
222372
public static bool TryParse(string s, IFormatProvider provider, out TSelf result) => TSelf.TryParse(s, provider, out result);
223373
}
224374

375+
public static class PowerFunctionsHelper<TSelf>
376+
where TSelf : IPowerFunctions<TSelf>
377+
{
378+
public static TSelf Pow(TSelf x, TSelf y) => TSelf.Pow(x, y);
379+
}
380+
381+
public static class RootFunctionsHelper<TSelf>
382+
where TSelf : IRootFunctions<TSelf>
383+
{
384+
public static TSelf Cbrt(TSelf x) => TSelf.Cbrt(x);
385+
386+
public static TSelf Sqrt(TSelf x) => TSelf.Sqrt(x);
387+
}
388+
225389
public static class ShiftOperatorsHelper<TSelf, TResult>
226390
where TSelf : IShiftOperators<TSelf, TResult>
227391
{
@@ -254,6 +418,26 @@ public static class SubtractionOperatorsHelper<TSelf, TOther, TResult>
254418
public static TResult op_CheckedSubtraction(TSelf left, TOther right) => checked(left - right);
255419
}
256420

421+
public static class TrigonometricFunctionsHelper<TSelf>
422+
where TSelf : ITrigonometricFunctions<TSelf>
423+
{
424+
public static TSelf Acos(TSelf x) => TSelf.Acos(x);
425+
426+
public static TSelf Asin(TSelf x) => TSelf.Asin(x);
427+
428+
public static TSelf Atan(TSelf x) => TSelf.Atan(x);
429+
430+
public static TSelf Atan2(TSelf y, TSelf x) => TSelf.Atan2(y, x);
431+
432+
public static TSelf Cos(TSelf x) => TSelf.Cos(x);
433+
434+
public static TSelf Sin(TSelf x) => TSelf.Sin(x);
435+
436+
public static (TSelf Sin, TSelf Cos) SinCos(TSelf x) => TSelf.SinCos(x);
437+
438+
public static TSelf Tan(TSelf x) => TSelf.Tan(x);
439+
}
440+
257441
public static class UnaryNegationOperatorsHelper<TSelf, TResult>
258442
where TSelf : IUnaryNegationOperators<TSelf, TResult>
259443
{

0 commit comments

Comments
 (0)