/
Single.cs
2156 lines (1801 loc) · 79.3 KB
/
Single.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*============================================================
**
**
**
** Purpose: A wrapper class for the primitive type float.
**
**
===========================================================*/
using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace System
{
[Serializable]
[StructLayout(LayoutKind.Sequential)]
[TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public readonly struct Single
: IComparable,
IConvertible,
ISpanFormattable,
IComparable<float>,
IEquatable<float>,
IBinaryFloatingPointIeee754<float>,
IMinMaxValue<float>
{
private readonly float m_value; // Do not rename (binary serialization)
//
// Public constants
//
public const float MinValue = (float)-3.40282346638528859e+38;
public const float MaxValue = (float)3.40282346638528859e+38;
// Note Epsilon should be a float whose hex representation is 0x1
// on little endian machines.
public const float Epsilon = (float)1.4e-45;
public const float NegativeInfinity = (float)-1.0 / (float)0.0;
public const float PositiveInfinity = (float)1.0 / (float)0.0;
public const float NaN = (float)0.0 / (float)0.0;
/// <summary>Represents the additive identity (0).</summary>
internal const float AdditiveIdentity = 0.0f;
/// <summary>Represents the multiplicative identity (1).</summary>
internal const float MultiplicativeIdentity = 1.0f;
/// <summary>Represents the number one (1).</summary>
internal const float One = 1.0f;
/// <summary>Represents the number zero (0).</summary>
internal const float Zero = 0.0f;
/// <summary>Represents the number negative one (-1).</summary>
internal const float NegativeOne = -1.0f;
/// <summary>Represents the number negative zero (-0).</summary>
public const float NegativeZero = -0.0f;
/// <summary>Represents the natural logarithmic base, specified by the constant, e.</summary>
/// <remarks>This is known as Euler's number and is approximately 2.7182818284590452354.</remarks>
public const float E = MathF.E;
/// <summary>Represents the ratio of the circumference of a circle to its diameter, specified by the constant, π.</summary>
/// <remarks>Pi is approximately 3.1415926535897932385.</remarks>
public const float Pi = MathF.PI;
/// <summary>Represents the number of radians in one turn, specified by the constant, τ.</summary>
/// <remarks>Tau is approximately 6.2831853071795864769.</remarks>
public const float Tau = MathF.Tau;
//
// Constants for manipulating the private bit-representation
//
internal const uint SignMask = 0x8000_0000;
internal const int SignShift = 31;
internal const byte ShiftedSignMask = (byte)(SignMask >> SignShift);
internal const uint BiasedExponentMask = 0x7F80_0000;
internal const int BiasedExponentShift = 23;
internal const byte ShiftedBiasedExponentMask = (byte)(BiasedExponentMask >> BiasedExponentShift);
internal const uint TrailingSignificandMask = 0x007F_FFFF;
internal const byte MinSign = 0;
internal const byte MaxSign = 1;
internal const byte MinBiasedExponent = 0x00;
internal const byte MaxBiasedExponent = 0xFF;
internal const byte ExponentBias = 127;
internal const sbyte MinExponent = -126;
internal const sbyte MaxExponent = +127;
internal const uint MinTrailingSignificand = 0x0000_0000;
internal const uint MaxTrailingSignificand = 0x007F_FFFF;
internal byte BiasedExponent
{
get
{
uint bits = BitConverter.SingleToUInt32Bits(m_value);
return ExtractBiasedExponentFromBits(bits);
}
}
internal sbyte Exponent
{
get
{
return (sbyte)(BiasedExponent - ExponentBias);
}
}
internal uint Significand
{
get
{
return TrailingSignificand | ((BiasedExponent != 0) ? (1U << BiasedExponentShift) : 0U);
}
}
internal uint TrailingSignificand
{
get
{
uint bits = BitConverter.SingleToUInt32Bits(m_value);
return ExtractTrailingSignificandFromBits(bits);
}
}
internal static byte ExtractBiasedExponentFromBits(uint bits)
{
return (byte)((bits >> BiasedExponentShift) & ShiftedBiasedExponentMask);
}
internal static uint ExtractTrailingSignificandFromBits(uint bits)
{
return bits & TrailingSignificandMask;
}
/// <summary>Determines whether the specified value is finite (zero, subnormal, or normal).</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsFinite(float f)
{
int bits = BitConverter.SingleToInt32Bits(f);
return (bits & 0x7FFFFFFF) < 0x7F800000;
}
/// <summary>Determines whether the specified value is infinite.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool IsInfinity(float f)
{
int bits = BitConverter.SingleToInt32Bits(f);
return (bits & 0x7FFFFFFF) == 0x7F800000;
}
/// <summary>Determines whether the specified value is NaN.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool IsNaN(float f)
{
// A NaN will never equal itself so this is an
// easy and efficient way to check for NaN.
#pragma warning disable CS1718
return f != f;
#pragma warning restore CS1718
}
/// <summary>Determines whether the specified value is negative.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool IsNegative(float f)
{
return BitConverter.SingleToInt32Bits(f) < 0;
}
/// <summary>Determines whether the specified value is negative infinity.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool IsNegativeInfinity(float f)
{
return f == float.NegativeInfinity;
}
/// <summary>Determines whether the specified value is normal.</summary>
[NonVersionable]
// This is probably not worth inlining, it has branches and should be rarely called
public static unsafe bool IsNormal(float f)
{
int bits = BitConverter.SingleToInt32Bits(f);
bits &= 0x7FFFFFFF;
return (bits < 0x7F800000) && (bits != 0) && ((bits & 0x7F800000) != 0);
}
/// <summary>Determines whether the specified value is positive infinity.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool IsPositiveInfinity(float f)
{
return f == float.PositiveInfinity;
}
/// <summary>Determines whether the specified value is subnormal.</summary>
[NonVersionable]
// This is probably not worth inlining, it has branches and should be rarely called
public static unsafe bool IsSubnormal(float f)
{
int bits = BitConverter.SingleToInt32Bits(f);
bits &= 0x7FFFFFFF;
return (bits < 0x7F800000) && (bits != 0) && ((bits & 0x7F800000) == 0);
}
// Compares this object to another object, returning an integer that
// indicates the relationship.
// Returns a value less than zero if this object
// null is considered to be less than any instance.
// If object is not of type Single, this method throws an ArgumentException.
//
public int CompareTo(object? value)
{
if (value == null)
{
return 1;
}
if (value is float f)
{
if (m_value < f) return -1;
if (m_value > f) return 1;
if (m_value == f) return 0;
// At least one of the values is NaN.
if (IsNaN(m_value))
return IsNaN(f) ? 0 : -1;
else // f is NaN.
return 1;
}
throw new ArgumentException(SR.Arg_MustBeSingle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CompareTo(float value)
{
if (m_value < value) return -1;
if (m_value > value) return 1;
if (m_value == value) return 0;
// At least one of the values is NaN.
if (IsNaN(m_value))
return IsNaN(value) ? 0 : -1;
else // f is NaN.
return 1;
}
/// <inheritdoc cref="IEqualityOperators{TSelf, TOther, TResult}.op_Equality(TSelf, TOther)" />
[NonVersionable]
public static bool operator ==(float left, float right) => left == right;
/// <inheritdoc cref="IEqualityOperators{TSelf, TOther, TResult}.op_Inequality(TSelf, TOther)" />
[NonVersionable]
public static bool operator !=(float left, float right) => left != right;
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_LessThan(TSelf, TOther)" />
[NonVersionable]
public static bool operator <(float left, float right) => left < right;
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_GreaterThan(TSelf, TOther)" />
[NonVersionable]
public static bool operator >(float left, float right) => left > right;
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_LessThanOrEqual(TSelf, TOther)" />
[NonVersionable]
public static bool operator <=(float left, float right) => left <= right;
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_GreaterThanOrEqual(TSelf, TOther)" />
[NonVersionable]
public static bool operator >=(float left, float right) => left >= right;
public override bool Equals([NotNullWhen(true)] object? obj)
{
if (!(obj is float))
{
return false;
}
float temp = ((float)obj).m_value;
if (temp == m_value)
{
return true;
}
return IsNaN(temp) && IsNaN(m_value);
}
public bool Equals(float obj)
{
if (obj == m_value)
{
return true;
}
return IsNaN(obj) && IsNaN(m_value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
int bits = Unsafe.As<float, int>(ref Unsafe.AsRef(in m_value));
// Optimized check for IsNan() || IsZero()
if (((bits - 1) & 0x7FFFFFFF) >= 0x7F800000)
{
// Ensure that all NaNs and both zeros have the same hash code
bits &= 0x7F800000;
}
return bits;
}
public override string ToString()
{
return Number.FormatSingle(m_value, null, NumberFormatInfo.CurrentInfo);
}
public string ToString(IFormatProvider? provider)
{
return Number.FormatSingle(m_value, null, NumberFormatInfo.GetInstance(provider));
}
public string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string? format)
{
return Number.FormatSingle(m_value, format, NumberFormatInfo.CurrentInfo);
}
public string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string? format, IFormatProvider? provider)
{
return Number.FormatSingle(m_value, format, NumberFormatInfo.GetInstance(provider));
}
public bool TryFormat(Span<char> destination, out int charsWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
{
return Number.TryFormatSingle(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
// Parses a float from a String in the given style. If
// a NumberFormatInfo isn't specified, the current culture's
// NumberFormatInfo is assumed.
//
// This method will not throw an OverflowException, but will return
// PositiveInfinity or NegativeInfinity for a number that is too
// large or too small.
//
public static float Parse(string s)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseSingle(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);
}
public static float Parse(string s, NumberStyles style)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseSingle(s, style, NumberFormatInfo.CurrentInfo);
}
public static float Parse(string s, IFormatProvider? provider)
{
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseSingle(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
}
public static float Parse(string s, NumberStyles style, IFormatProvider? provider)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
return Number.ParseSingle(s, style, NumberFormatInfo.GetInstance(provider));
}
public static float Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands, IFormatProvider? provider = null)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return Number.ParseSingle(s, style, NumberFormatInfo.GetInstance(provider));
}
public static bool TryParse([NotNullWhen(true)] string? s, out float result)
{
if (s == null)
{
result = 0;
return false;
}
return TryParse((ReadOnlySpan<char>)s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse(ReadOnlySpan<char> s, out float result)
{
return TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
}
public static bool TryParse([NotNullWhen(true)] string? s, NumberStyles style, IFormatProvider? provider, out float result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null)
{
result = 0;
return false;
}
return TryParse((ReadOnlySpan<char>)s, style, NumberFormatInfo.GetInstance(provider), out result);
}
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out float result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
}
private static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, NumberFormatInfo info, out float result)
{
return Number.TryParseSingle(s, style, info, out result);
}
//
// IConvertible implementation
//
public TypeCode GetTypeCode()
{
return TypeCode.Single;
}
bool IConvertible.ToBoolean(IFormatProvider? provider)
{
return Convert.ToBoolean(m_value);
}
char IConvertible.ToChar(IFormatProvider? provider)
{
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Single", "Char"));
}
sbyte IConvertible.ToSByte(IFormatProvider? provider)
{
return Convert.ToSByte(m_value);
}
byte IConvertible.ToByte(IFormatProvider? provider)
{
return Convert.ToByte(m_value);
}
short IConvertible.ToInt16(IFormatProvider? provider)
{
return Convert.ToInt16(m_value);
}
ushort IConvertible.ToUInt16(IFormatProvider? provider)
{
return Convert.ToUInt16(m_value);
}
int IConvertible.ToInt32(IFormatProvider? provider)
{
return Convert.ToInt32(m_value);
}
uint IConvertible.ToUInt32(IFormatProvider? provider)
{
return Convert.ToUInt32(m_value);
}
long IConvertible.ToInt64(IFormatProvider? provider)
{
return Convert.ToInt64(m_value);
}
ulong IConvertible.ToUInt64(IFormatProvider? provider)
{
return Convert.ToUInt64(m_value);
}
float IConvertible.ToSingle(IFormatProvider? provider)
{
return m_value;
}
double IConvertible.ToDouble(IFormatProvider? provider)
{
return Convert.ToDouble(m_value);
}
decimal IConvertible.ToDecimal(IFormatProvider? provider)
{
return Convert.ToDecimal(m_value);
}
DateTime IConvertible.ToDateTime(IFormatProvider? provider)
{
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Single", "DateTime"));
}
object IConvertible.ToType(Type type, IFormatProvider? provider)
{
return Convert.DefaultToType((IConvertible)this, type, provider);
}
//
// IAdditionOperators
//
/// <inheritdoc cref="IAdditionOperators{TSelf, TOther, TResult}.op_Addition(TSelf, TOther)" />
static float IAdditionOperators<float, float, float>.operator +(float left, float right) => left + right;
//
// IAdditiveIdentity
//
/// <inheritdoc cref="IAdditiveIdentity{TSelf, TResult}.AdditiveIdentity" />
static float IAdditiveIdentity<float, float>.AdditiveIdentity => AdditiveIdentity;
//
// IBinaryNumber
//
/// <inheritdoc cref="IBinaryNumber{TSelf}.AllBitsSet" />
static float IBinaryNumber<float>.AllBitsSet => BitConverter.UInt32BitsToSingle(0xFFFF_FFFF);
/// <inheritdoc cref="IBinaryNumber{TSelf}.IsPow2(TSelf)" />
public static bool IsPow2(float value)
{
uint bits = BitConverter.SingleToUInt32Bits(value);
byte biasedExponent = ExtractBiasedExponentFromBits(bits);
uint trailingSignificand = ExtractTrailingSignificandFromBits(bits);
return (value > 0)
&& (biasedExponent != MinBiasedExponent) && (biasedExponent != MaxBiasedExponent)
&& (trailingSignificand == MinTrailingSignificand);
}
/// <inheritdoc cref="IBinaryNumber{TSelf}.Log2(TSelf)" />
public static float Log2(float value) => MathF.Log2(value);
//
// IBitwiseOperators
//
/// <inheritdoc cref="IBitwiseOperators{TSelf, TOther, TResult}.op_BitwiseAnd(TSelf, TOther)" />
static float IBitwiseOperators<float, float, float>.operator &(float left, float right)
{
uint bits = BitConverter.SingleToUInt32Bits(left) & BitConverter.SingleToUInt32Bits(right);
return BitConverter.UInt32BitsToSingle(bits);
}
/// <inheritdoc cref="IBitwiseOperators{TSelf, TOther, TResult}.op_BitwiseOr(TSelf, TOther)" />
static float IBitwiseOperators<float, float, float>.operator |(float left, float right)
{
uint bits = BitConverter.SingleToUInt32Bits(left) | BitConverter.SingleToUInt32Bits(right);
return BitConverter.UInt32BitsToSingle(bits);
}
/// <inheritdoc cref="IBitwiseOperators{TSelf, TOther, TResult}.op_ExclusiveOr(TSelf, TOther)" />
static float IBitwiseOperators<float, float, float>.operator ^(float left, float right)
{
uint bits = BitConverter.SingleToUInt32Bits(left) ^ BitConverter.SingleToUInt32Bits(right);
return BitConverter.UInt32BitsToSingle(bits);
}
/// <inheritdoc cref="IBitwiseOperators{TSelf, TOther, TResult}.op_OnesComplement(TSelf)" />
static float IBitwiseOperators<float, float, float>.operator ~(float value)
{
uint bits = ~BitConverter.SingleToUInt32Bits(value);
return BitConverter.UInt32BitsToSingle(bits);
}
//
// IDecrementOperators
//
/// <inheritdoc cref="IDecrementOperators{TSelf}.op_Decrement(TSelf)" />
static float IDecrementOperators<float>.operator --(float value) => --value;
//
// IDivisionOperators
//
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
static float IDivisionOperators<float, float, float>.operator /(float left, float right) => left / right;
//
// IExponentialFunctions
//
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp" />
public static float Exp(float x) => MathF.Exp(x);
/// <inheritdoc cref="IExponentialFunctions{TSelf}.ExpM1(TSelf)" />
public static float ExpM1(float x) => MathF.Exp(x) - 1;
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp2(TSelf)" />
public static float Exp2(float x) => MathF.Pow(2, x);
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp2M1(TSelf)" />
public static float Exp2M1(float x) => MathF.Pow(2, x) - 1;
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp10(TSelf)" />
public static float Exp10(float x) => MathF.Pow(10, x);
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp10M1(TSelf)" />
public static float Exp10M1(float x) => MathF.Pow(10, x) - 1;
//
// IFloatingPoint
//
/// <inheritdoc cref="IFloatingPoint{TSelf}.Ceiling(TSelf)" />
public static float Ceiling(float x) => MathF.Ceiling(x);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Floor(TSelf)" />
public static float Floor(float x) => MathF.Floor(x);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Round(TSelf)" />
public static float Round(float x) => MathF.Round(x);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Round(TSelf, int)" />
public static float Round(float x, int digits) => MathF.Round(x, digits);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Round(TSelf, MidpointRounding)" />
public static float Round(float x, MidpointRounding mode) => MathF.Round(x, mode);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Round(TSelf, int, MidpointRounding)" />
public static float Round(float x, int digits, MidpointRounding mode) => MathF.Round(x, digits, mode);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Truncate(TSelf)" />
public static float Truncate(float x) => MathF.Truncate(x);
/// <inheritdoc cref="IFloatingPoint{TSelf}.GetExponentByteCount()" />
int IFloatingPoint<float>.GetExponentByteCount() => sizeof(sbyte);
/// <inheritdoc cref="IFloatingPoint{TSelf}.GetExponentShortestBitLength()" />
int IFloatingPoint<float>.GetExponentShortestBitLength()
{
sbyte exponent = Exponent;
if (exponent >= 0)
{
return (sizeof(sbyte) * 8) - sbyte.LeadingZeroCount(exponent);
}
else
{
return (sizeof(sbyte) * 8) + 1 - sbyte.LeadingZeroCount((sbyte)(~exponent));
}
}
/// <inheritdoc cref="IFloatingPoint{TSelf}.GetSignificandByteCount()" />
int IFloatingPoint<float>.GetSignificandByteCount() => sizeof(uint);
/// <inheritdoc cref="IFloatingPoint{TSelf}.GetSignificandBitLength()" />
int IFloatingPoint<float>.GetSignificandBitLength() => 24;
/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteExponentBigEndian(Span{byte}, out int)" />
bool IFloatingPoint<float>.TryWriteExponentBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(sbyte))
{
sbyte exponent = Exponent;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);
bytesWritten = sizeof(sbyte);
return true;
}
else
{
bytesWritten = 0;
return false;
}
}
/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteExponentLittleEndian(Span{byte}, out int)" />
bool IFloatingPoint<float>.TryWriteExponentLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(sbyte))
{
sbyte exponent = Exponent;
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);
bytesWritten = sizeof(sbyte);
return true;
}
else
{
bytesWritten = 0;
return false;
}
}
/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteSignificandBigEndian(Span{byte}, out int)" />
bool IFloatingPoint<float>.TryWriteSignificandBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(uint))
{
uint significand = Significand;
if (BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);
bytesWritten = sizeof(uint);
return true;
}
else
{
bytesWritten = 0;
return false;
}
}
/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteSignificandLittleEndian(Span{byte}, out int)" />
bool IFloatingPoint<float>.TryWriteSignificandLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(uint))
{
uint significand = Significand;
if (!BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);
bytesWritten = sizeof(uint);
return true;
}
else
{
bytesWritten = 0;
return false;
}
}
//
// IFloatingPointConstants
//
/// <inheritdoc cref="IFloatingPointConstants{TSelf}.E" />
static float IFloatingPointConstants<float>.E => E;
/// <inheritdoc cref="IFloatingPointConstants{TSelf}.Pi" />
static float IFloatingPointConstants<float>.Pi => Pi;
/// <inheritdoc cref="IFloatingPointConstants{TSelf}.Tau" />
static float IFloatingPointConstants<float>.Tau => Tau;
//
// IFloatingPointIeee754
//
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Epsilon" />
static float IFloatingPointIeee754<float>.Epsilon => Epsilon;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.NaN" />
static float IFloatingPointIeee754<float>.NaN => NaN;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.NegativeInfinity" />
static float IFloatingPointIeee754<float>.NegativeInfinity => NegativeInfinity;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.NegativeZero" />
static float IFloatingPointIeee754<float>.NegativeZero => NegativeZero;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.PositiveInfinity" />
static float IFloatingPointIeee754<float>.PositiveInfinity => PositiveInfinity;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Atan2(TSelf, TSelf)" />
public static float Atan2(float y, float x) => MathF.Atan2(y, x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Atan2Pi(TSelf, TSelf)" />
public static float Atan2Pi(float y, float x) => Atan2(y, x) / Pi;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.BitDecrement(TSelf)" />
public static float BitDecrement(float x) => MathF.BitDecrement(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.BitIncrement(TSelf)" />
public static float BitIncrement(float x) => MathF.BitIncrement(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.FusedMultiplyAdd(TSelf, TSelf, TSelf)" />
public static float FusedMultiplyAdd(float left, float right, float addend) => MathF.FusedMultiplyAdd(left, right, addend);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Ieee754Remainder(TSelf, TSelf)" />
public static float Ieee754Remainder(float left, float right) => MathF.IEEERemainder(left, right);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ILogB(TSelf)" />
public static int ILogB(float x) => MathF.ILogB(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ReciprocalEstimate(TSelf)" />
public static float ReciprocalEstimate(float x) => MathF.ReciprocalEstimate(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ReciprocalSqrtEstimate(TSelf)" />
public static float ReciprocalSqrtEstimate(float x) => MathF.ReciprocalSqrtEstimate(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ScaleB(TSelf, int)" />
public static float ScaleB(float x, int n) => MathF.ScaleB(x, n);
// /// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Compound(TSelf, TSelf)" />
// public static float Compound(float x, float n) => MathF.Compound(x, n);
//
// IHyperbolicFunctions
//
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Acosh(TSelf)" />
public static float Acosh(float x) => MathF.Acosh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Asinh(TSelf)" />
public static float Asinh(float x) => MathF.Asinh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Atanh(TSelf)" />
public static float Atanh(float x) => MathF.Atanh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Cosh(TSelf)" />
public static float Cosh(float x) => MathF.Cosh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Sinh(TSelf)" />
public static float Sinh(float x) => MathF.Sinh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Tanh(TSelf)" />
public static float Tanh(float x) => MathF.Tanh(x);
//
// IIncrementOperators
//
/// <inheritdoc cref="IIncrementOperators{TSelf}.op_Increment(TSelf)" />
static float IIncrementOperators<float>.operator ++(float value) => ++value;
//
// ILogarithmicFunctions
//
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log(TSelf)" />
public static float Log(float x) => MathF.Log(x);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log(TSelf, TSelf)" />
public static float Log(float x, float newBase) => MathF.Log(x, newBase);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.LogP1(TSelf)" />
public static float LogP1(float x) => MathF.Log(x + 1);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log10(TSelf)" />
public static float Log10(float x) => MathF.Log10(x);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log2P1(TSelf)" />
public static float Log2P1(float x) => MathF.Log2(x + 1);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log10P1(TSelf)" />
public static float Log10P1(float x) => MathF.Log10(x + 1);
//
// IMinMaxValue
//
/// <inheritdoc cref="IMinMaxValue{TSelf}.MinValue" />
static float IMinMaxValue<float>.MinValue => MinValue;
/// <inheritdoc cref="IMinMaxValue{TSelf}.MaxValue" />
static float IMinMaxValue<float>.MaxValue => MaxValue;
//
// IModulusOperators
//
/// <inheritdoc cref="IModulusOperators{TSelf, TOther, TResult}.op_Modulus(TSelf, TOther)" />
static float IModulusOperators<float, float, float>.operator %(float left, float right) => left % right;
//
// IMultiplicativeIdentity
//
/// <inheritdoc cref="IMultiplicativeIdentity{TSelf, TResult}.MultiplicativeIdentity" />
static float IMultiplicativeIdentity<float, float>.MultiplicativeIdentity => MultiplicativeIdentity;
//
// IMultiplyOperators
//
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_Multiply(TSelf, TOther)" />
static float IMultiplyOperators<float, float, float>.operator *(float left, float right) => left * right;
//
// INumber
//
/// <inheritdoc cref="INumber{TSelf}.Clamp(TSelf, TSelf, TSelf)" />
public static float Clamp(float value, float min, float max) => Math.Clamp(value, min, max);
/// <inheritdoc cref="INumber{TSelf}.CopySign(TSelf, TSelf)" />
public static float CopySign(float value, float sign) => MathF.CopySign(value, sign);
/// <inheritdoc cref="INumber{TSelf}.Max(TSelf, TSelf)" />
public static float Max(float x, float y) => MathF.Max(x, y);
/// <inheritdoc cref="INumber{TSelf}.MaxNumber(TSelf, TSelf)" />
public static float MaxNumber(float x, float y)
{
// This matches the IEEE 754:2019 `maximumNumber` function
//
// It does not propagate NaN inputs back to the caller and
// otherwise returns the larger of the inputs. It
// treats +0 as larger than -0 as per the specification.
if (x != y)
{
if (!IsNaN(y))
{
return y < x ? x : y;
}
return x;
}
return IsNegative(y) ? x : y;
}
/// <inheritdoc cref="INumber{TSelf}.Min(TSelf, TSelf)" />
public static float Min(float x, float y) => MathF.Min(x, y);
/// <inheritdoc cref="INumber{TSelf}.MinNumber(TSelf, TSelf)" />
public static float MinNumber(float x, float y)
{
// This matches the IEEE 754:2019 `minimumNumber` function
//
// It does not propagate NaN inputs back to the caller and
// otherwise returns the larger of the inputs. It
// treats +0 as larger than -0 as per the specification.
if (x != y)
{
if (!IsNaN(y))
{
return x < y ? x : y;
}
return x;
}
return IsNegative(x) ? x : y;
}
/// <inheritdoc cref="INumber{TSelf}.Sign(TSelf)" />
public static int Sign(float value) => MathF.Sign(value);
//
// INumberBase
//
/// <inheritdoc cref="INumberBase{TSelf}.One" />
static float INumberBase<float>.One => One;
/// <inheritdoc cref="INumberBase{TSelf}.Radix" />
static int INumberBase<float>.Radix => 2;
/// <inheritdoc cref="INumberBase{TSelf}.Zero" />
static float INumberBase<float>.Zero => Zero;
/// <inheritdoc cref="INumberBase{TSelf}.Abs(TSelf)" />
public static float Abs(float value) => MathF.Abs(value);
/// <inheritdoc cref="INumberBase{TSelf}.CreateChecked{TOther}(TOther)" />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float CreateChecked<TOther>(TOther value)
where TOther : INumberBase<TOther>
{
float result;
if (typeof(TOther) == typeof(float))
{
result = (float)(object)value;
}
else if (!TryConvertFrom(value, out result) && !TOther.TryConvertToChecked(value, out result))
{