/
Double.cs
2478 lines (2055 loc) · 95.2 KB
/
Double.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.
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
{
/// <summary>
/// Represents a double-precision floating-point number.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Sequential)]
[TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public readonly struct Double
: IComparable,
IConvertible,
ISpanFormattable,
IComparable<double>,
IEquatable<double>,
IBinaryFloatingPointIeee754<double>,
IMinMaxValue<double>,
IUtf8SpanFormattable,
IBinaryFloatParseAndFormatInfo<double>
{
private readonly double m_value; // Do not rename (binary serialization)
//
// Public Constants
//
public const double MinValue = -1.7976931348623157E+308;
public const double MaxValue = 1.7976931348623157E+308;
// Note Epsilon should be a double whose hex representation is 0x1
// on little endian machines.
public const double Epsilon = 4.9406564584124654E-324;
public const double NegativeInfinity = (double)-1.0 / (double)(0.0);
public const double PositiveInfinity = (double)1.0 / (double)(0.0);
public const double NaN = (double)0.0 / (double)0.0;
/// <summary>Represents the additive identity (0).</summary>
internal const double AdditiveIdentity = 0.0;
/// <summary>Represents the multiplicative identity (1).</summary>
internal const double MultiplicativeIdentity = 1.0;
/// <summary>Represents the number one (1).</summary>
internal const double One = 1.0;
/// <summary>Represents the number zero (0).</summary>
internal const double Zero = 0.0;
/// <summary>Represents the number negative one (-1).</summary>
internal const double NegativeOne = -1.0;
/// <summary>Represents the number negative zero (-0).</summary>
public const double NegativeZero = -0.0;
/// <summary>Represents the natural logarithmic base, specified by the constant, e.</summary>
/// <remarks>Euler's number is approximately 2.7182818284590452354.</remarks>
public const double E = Math.E;
/// <summary>Represents the ratio of the circumference of a circle to its diameter, specified by the constant, PI.</summary>
/// <remarks>Pi is approximately 3.1415926535897932385.</remarks>
public const double Pi = Math.PI;
/// <summary>Represents the number of radians in one turn, specified by the constant, Tau.</summary>
/// <remarks>Tau is approximately 6.2831853071795864769.</remarks>
public const double Tau = Math.Tau;
//
// Constants for manipulating the private bit-representation
//
internal const ulong SignMask = 0x8000_0000_0000_0000;
internal const int SignShift = 63;
internal const byte ShiftedSignMask = (byte)(SignMask >> SignShift);
internal const ulong BiasedExponentMask = 0x7FF0_0000_0000_0000;
internal const int BiasedExponentShift = 52;
internal const ushort ShiftedExponentMask = (ushort)(BiasedExponentMask >> BiasedExponentShift);
internal const ulong TrailingSignificandMask = 0x000F_FFFF_FFFF_FFFF;
internal const byte MinSign = 0;
internal const byte MaxSign = 1;
internal const ushort MinBiasedExponent = 0x0000;
internal const ushort MaxBiasedExponent = 0x07FF;
internal const ushort ExponentBias = 1023;
internal const short MinExponent = -1022;
internal const short MaxExponent = +1023;
internal const ulong MinTrailingSignificand = 0x0000_0000_0000_0000;
internal const ulong MaxTrailingSignificand = 0x000F_FFFF_FFFF_FFFF;
internal const int TrailingSignificandLength = 52;
internal const int SignificandLength = TrailingSignificandLength + 1;
internal ushort BiasedExponent
{
get
{
ulong bits = BitConverter.DoubleToUInt64Bits(m_value);
return ExtractBiasedExponentFromBits(bits);
}
}
internal short Exponent
{
get
{
return (short)(BiasedExponent - ExponentBias);
}
}
internal ulong Significand
{
get
{
return TrailingSignificand | ((BiasedExponent != 0) ? (1UL << BiasedExponentShift) : 0UL);
}
}
internal ulong TrailingSignificand
{
get
{
ulong bits = BitConverter.DoubleToUInt64Bits(m_value);
return ExtractTrailingSignificandFromBits(bits);
}
}
internal static ushort ExtractBiasedExponentFromBits(ulong bits)
{
return (ushort)((bits >> BiasedExponentShift) & ShiftedExponentMask);
}
internal static ulong ExtractTrailingSignificandFromBits(ulong bits)
{
return bits & TrailingSignificandMask;
}
/// <summary>Determines whether the specified value is finite (zero, subnormal, or normal).</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool IsFinite(double d)
{
long bits = BitConverter.DoubleToInt64Bits(d);
return (bits & 0x7FFFFFFFFFFFFFFF) < 0x7FF0000000000000;
}
/// <summary>Determines whether the specified value is infinite.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool IsInfinity(double d)
{
long bits = BitConverter.DoubleToInt64Bits(d);
return (bits & 0x7FFFFFFFFFFFFFFF) == 0x7FF0000000000000;
}
/// <summary>Determines whether the specified value is NaN.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool IsNaN(double d)
{
// A NaN will never equal itself so this is an
// easy and efficient way to check for NaN.
#pragma warning disable CS1718
return d != d;
#pragma warning restore CS1718
}
/// <summary>Determines whether the specified value is negative.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe bool IsNegative(double d)
{
return BitConverter.DoubleToInt64Bits(d) < 0;
}
/// <summary>Determines whether the specified value is negative infinity.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNegativeInfinity(double d)
{
return d == 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(double d)
{
long bits = BitConverter.DoubleToInt64Bits(d);
bits &= 0x7FFFFFFFFFFFFFFF;
return (bits < 0x7FF0000000000000) && (bits != 0) && ((bits & 0x7FF0000000000000) != 0);
}
/// <summary>Determines whether the specified value is positive infinity.</summary>
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPositiveInfinity(double d)
{
return d == 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(double d)
{
long bits = BitConverter.DoubleToInt64Bits(d);
bits &= 0x7FFFFFFFFFFFFFFF;
return (bits < 0x7FF0000000000000) && (bits != 0) && ((bits & 0x7FF0000000000000) == 0);
}
// Compares this object to another object, returning an instance of System.Relation.
// Null is considered less than any instance.
//
// If object is not of type Double, this method throws an ArgumentException.
//
// Returns a value less than zero if this object
//
public int CompareTo(object? value)
{
if (value == null)
{
return 1;
}
if (value is double d)
{
if (m_value < d) return -1;
if (m_value > d) return 1;
if (m_value == d) return 0;
// At least one of the values is NaN.
if (IsNaN(m_value))
return IsNaN(d) ? 0 : -1;
else
return 1;
}
throw new ArgumentException(SR.Arg_MustBeDouble);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CompareTo(double 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
return 1;
}
// True if obj is another Double with the same value as the current instance. This is
// a method of object equality, that only returns true if obj is also a double.
public override bool Equals([NotNullWhen(true)] object? obj)
{
if (!(obj is double))
{
return false;
}
double temp = ((double)obj).m_value;
// This code below is written this way for performance reasons i.e the != and == check is intentional.
if (temp == m_value)
{
return true;
}
return IsNaN(temp) && IsNaN(m_value);
}
/// <inheritdoc cref="IEqualityOperators{TSelf, TOther, TResult}.op_Equality(TSelf, TOther)" />
[NonVersionable]
public static bool operator ==(double left, double right) => left == right;
/// <inheritdoc cref="IEqualityOperators{TSelf, TOther, TResult}.op_Inequality(TSelf, TOther)" />
[NonVersionable]
public static bool operator !=(double left, double right) => left != right;
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_LessThan(TSelf, TOther)" />
[NonVersionable]
public static bool operator <(double left, double right) => left < right;
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_GreaterThan(TSelf, TOther)" />
[NonVersionable]
public static bool operator >(double left, double right) => left > right;
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_LessThanOrEqual(TSelf, TOther)" />
[NonVersionable]
public static bool operator <=(double left, double right) => left <= right;
/// <inheritdoc cref="IComparisonOperators{TSelf, TOther, TResult}.op_GreaterThanOrEqual(TSelf, TOther)" />
[NonVersionable]
public static bool operator >=(double left, double right) => left >= right;
public bool Equals(double obj)
{
if (obj == m_value)
{
return true;
}
return IsNaN(obj) && IsNaN(m_value);
}
// The hashcode for a double is the absolute value of the integer representation
// of that double.
[MethodImpl(MethodImplOptions.AggressiveInlining)] // 64-bit constants make the IL unusually large that makes the inliner to reject the method
public override int GetHashCode()
{
long bits = Unsafe.As<double, long>(ref Unsafe.AsRef(in m_value));
// Optimized check for IsNan() || IsZero()
if (((bits - 1) & 0x7FFFFFFFFFFFFFFF) >= 0x7FF0000000000000)
{
// Ensure that all NaNs and both zeros have the same hash code
bits &= 0x7FF0000000000000;
}
return unchecked((int)bits) ^ ((int)(bits >> 32));
}
public override string ToString()
{
return Number.FormatDouble(m_value, null, NumberFormatInfo.CurrentInfo);
}
public string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string? format)
{
return Number.FormatDouble(m_value, format, NumberFormatInfo.CurrentInfo);
}
public string ToString(IFormatProvider? provider)
{
return Number.FormatDouble(m_value, null, NumberFormatInfo.GetInstance(provider));
}
public string ToString([StringSyntax(StringSyntaxAttribute.NumericFormat)] string? format, IFormatProvider? provider)
{
return Number.FormatDouble(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.TryFormatDouble(m_value, format, NumberFormatInfo.GetInstance(provider), destination, out charsWritten);
}
/// <inheritdoc cref="IUtf8SpanFormattable.TryFormat" />
public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, [StringSyntax(StringSyntaxAttribute.NumericFormat)] ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
{
return Number.TryFormatDouble(m_value, format, NumberFormatInfo.GetInstance(provider), utf8Destination, out bytesWritten);
}
public static double Parse(string s) => Parse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider: null);
public static double Parse(string s, NumberStyles style) => Parse(s, style, provider: null);
public static double Parse(string s, IFormatProvider? provider) => Parse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider);
public static double Parse(string s, NumberStyles style, IFormatProvider? provider)
{
if (s is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
}
return Parse(s.AsSpan(), style, provider);
}
// Parses a double 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 double Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Float | NumberStyles.AllowThousands, IFormatProvider? provider = null)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return Number.ParseFloat<char, double>(s, style, NumberFormatInfo.GetInstance(provider));
}
public static bool TryParse([NotNullWhen(true)] string? s, out double result) => TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider: null, out result);
public static bool TryParse(ReadOnlySpan<char> s, out double result) => TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider: null, out result);
/// <summary>Tries to convert a UTF-8 character span containing the string representation of a number to its double-precision floating-point number equivalent.</summary>
/// <param name="utf8Text">A read-only UTF-8 character span that contains the number to convert.</param>
/// <param name="result">When this method returns, contains a double-precision floating-point number equivalent of the numeric value or symbol contained in <paramref name="utf8Text" /> if the conversion succeeded or zero if the conversion failed. The conversion fails if the <paramref name="utf8Text" /> is <see cref="ReadOnlySpan{T}.Empty" /> or is not in a valid format. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
/// <returns><c>true</c> if <paramref name="utf8Text" /> was converted successfully; otherwise, false.</returns>
public static bool TryParse(ReadOnlySpan<byte> utf8Text, out double result) => TryParse(utf8Text, NumberStyles.Float | NumberStyles.AllowThousands, provider: null, out result);
public static bool TryParse([NotNullWhen(true)] string? s, NumberStyles style, IFormatProvider? provider, out double result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
if (s == null)
{
result = 0;
return false;
}
return Number.TryParseFloat(s.AsSpan(), style, NumberFormatInfo.GetInstance(provider), out result);
}
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out double result)
{
NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
return Number.TryParseFloat(s, style, NumberFormatInfo.GetInstance(provider), out result);
}
//
// IConvertible implementation
//
public TypeCode GetTypeCode()
{
return TypeCode.Double;
}
bool IConvertible.ToBoolean(IFormatProvider? provider)
{
return Convert.ToBoolean(m_value);
}
char IConvertible.ToChar(IFormatProvider? provider)
{
throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Double", "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 Convert.ToSingle(m_value);
}
double IConvertible.ToDouble(IFormatProvider? provider)
{
return 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, "Double", "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 double IAdditionOperators<double, double, double>.operator +(double left, double right) => left + right;
//
// IAdditiveIdentity
//
/// <inheritdoc cref="IAdditiveIdentity{TSelf, TResult}.AdditiveIdentity" />
static double IAdditiveIdentity<double, double>.AdditiveIdentity => AdditiveIdentity;
//
// IBinaryNumber
//
/// <inheritdoc cref="IBinaryNumber{TSelf}.AllBitsSet" />
static double IBinaryNumber<double>.AllBitsSet => BitConverter.UInt64BitsToDouble(0xFFFF_FFFF_FFFF_FFFF);
/// <inheritdoc cref="IBinaryNumber{TSelf}.IsPow2(TSelf)" />
public static bool IsPow2(double value)
{
ulong bits = BitConverter.DoubleToUInt64Bits(value);
if ((long)bits <= 0)
{
// Zero and negative values cannot be powers of 2
return false;
}
ushort biasedExponent = ExtractBiasedExponentFromBits(bits);
ulong trailingSignificand = ExtractTrailingSignificandFromBits(bits);
if (biasedExponent == MinBiasedExponent)
{
// Subnormal values have 1 bit set when they're powers of 2
return ulong.PopCount(trailingSignificand) == 1;
}
else if (biasedExponent == MaxBiasedExponent)
{
// NaN and Infinite values cannot be powers of 2
return false;
}
// Normal values have 0 bits set when they're powers of 2
return trailingSignificand == MinTrailingSignificand;
}
/// <inheritdoc cref="IBinaryNumber{TSelf}.Log2(TSelf)" />
[Intrinsic]
public static double Log2(double value) => Math.Log2(value);
//
// IBitwiseOperators
//
/// <inheritdoc cref="IBitwiseOperators{TSelf, TOther, TResult}.op_BitwiseAnd(TSelf, TOther)" />
static double IBitwiseOperators<double, double, double>.operator &(double left, double right)
{
ulong bits = BitConverter.DoubleToUInt64Bits(left) & BitConverter.DoubleToUInt64Bits(right);
return BitConverter.UInt64BitsToDouble(bits);
}
/// <inheritdoc cref="IBitwiseOperators{TSelf, TOther, TResult}.op_BitwiseOr(TSelf, TOther)" />
static double IBitwiseOperators<double, double, double>.operator |(double left, double right)
{
ulong bits = BitConverter.DoubleToUInt64Bits(left) | BitConverter.DoubleToUInt64Bits(right);
return BitConverter.UInt64BitsToDouble(bits);
}
/// <inheritdoc cref="IBitwiseOperators{TSelf, TOther, TResult}.op_ExclusiveOr(TSelf, TOther)" />
static double IBitwiseOperators<double, double, double>.operator ^(double left, double right)
{
ulong bits = BitConverter.DoubleToUInt64Bits(left) ^ BitConverter.DoubleToUInt64Bits(right);
return BitConverter.UInt64BitsToDouble(bits);
}
/// <inheritdoc cref="IBitwiseOperators{TSelf, TOther, TResult}.op_OnesComplement(TSelf)" />
static double IBitwiseOperators<double, double, double>.operator ~(double value)
{
ulong bits = ~BitConverter.DoubleToUInt64Bits(value);
return BitConverter.UInt64BitsToDouble(bits);
}
//
// IDecrementOperators
//
/// <inheritdoc cref="IDecrementOperators{TSelf}.op_Decrement(TSelf)" />
static double IDecrementOperators<double>.operator --(double value) => --value;
//
// IDivisionOperators
//
/// <inheritdoc cref="IDivisionOperators{TSelf, TOther, TResult}.op_Division(TSelf, TOther)" />
static double IDivisionOperators<double, double, double>.operator /(double left, double right) => left / right;
//
// IExponentialFunctions
//
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp" />
[Intrinsic]
public static double Exp(double x) => Math.Exp(x);
/// <inheritdoc cref="IExponentialFunctions{TSelf}.ExpM1(TSelf)" />
public static double ExpM1(double x) => Math.Exp(x) - 1;
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp2(TSelf)" />
public static double Exp2(double x) => Math.Pow(2, x);
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp2M1(TSelf)" />
public static double Exp2M1(double x) => Math.Pow(2, x) - 1;
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp10(TSelf)" />
public static double Exp10(double x) => Math.Pow(10, x);
/// <inheritdoc cref="IExponentialFunctions{TSelf}.Exp10M1(TSelf)" />
public static double Exp10M1(double x) => Math.Pow(10, x) - 1;
//
// IFloatingPoint
//
/// <inheritdoc cref="IFloatingPoint{TSelf}.Ceiling(TSelf)" />
[Intrinsic]
public static double Ceiling(double x) => Math.Ceiling(x);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Floor(TSelf)" />
[Intrinsic]
public static double Floor(double x) => Math.Floor(x);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Round(TSelf)" />
[Intrinsic]
public static double Round(double x) => Math.Round(x);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Round(TSelf, int)" />
public static double Round(double x, int digits) => Math.Round(x, digits);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Round(TSelf, MidpointRounding)" />
public static double Round(double x, MidpointRounding mode) => Math.Round(x, mode);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Round(TSelf, int, MidpointRounding)" />
public static double Round(double x, int digits, MidpointRounding mode) => Math.Round(x, digits, mode);
/// <inheritdoc cref="IFloatingPoint{TSelf}.Truncate(TSelf)" />
[Intrinsic]
public static double Truncate(double x) => Math.Truncate(x);
/// <inheritdoc cref="IFloatingPoint{TSelf}.GetExponentByteCount()" />
int IFloatingPoint<double>.GetExponentByteCount() => sizeof(short);
/// <inheritdoc cref="IFloatingPoint{TSelf}.GetExponentShortestBitLength()" />
int IFloatingPoint<double>.GetExponentShortestBitLength()
{
short exponent = Exponent;
if (exponent >= 0)
{
return (sizeof(short) * 8) - short.LeadingZeroCount(exponent);
}
else
{
return (sizeof(short) * 8) + 1 - short.LeadingZeroCount((short)(~exponent));
}
}
/// <inheritdoc cref="IFloatingPoint{TSelf}.GetSignificandByteCount()" />
int IFloatingPoint<double>.GetSignificandByteCount() => sizeof(ulong);
/// <inheritdoc cref="IFloatingPoint{TSelf}.GetSignificandBitLength()" />
int IFloatingPoint<double>.GetSignificandBitLength() => 53;
/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteExponentBigEndian(Span{byte}, out int)" />
bool IFloatingPoint<double>.TryWriteExponentBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(short))
{
short exponent = Exponent;
if (BitConverter.IsLittleEndian)
{
exponent = BinaryPrimitives.ReverseEndianness(exponent);
}
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);
bytesWritten = sizeof(short);
return true;
}
else
{
bytesWritten = 0;
return false;
}
}
/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteExponentLittleEndian(Span{byte}, out int)" />
bool IFloatingPoint<double>.TryWriteExponentLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(short))
{
short exponent = Exponent;
if (!BitConverter.IsLittleEndian)
{
exponent = BinaryPrimitives.ReverseEndianness(exponent);
}
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), exponent);
bytesWritten = sizeof(short);
return true;
}
else
{
bytesWritten = 0;
return false;
}
}
/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteSignificandBigEndian(Span{byte}, out int)" />
bool IFloatingPoint<double>.TryWriteSignificandBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(ulong))
{
ulong significand = Significand;
if (BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);
bytesWritten = sizeof(ulong);
return true;
}
else
{
bytesWritten = 0;
return false;
}
}
/// <inheritdoc cref="IFloatingPoint{TSelf}.TryWriteSignificandLittleEndian(Span{byte}, out int)" />
bool IFloatingPoint<double>.TryWriteSignificandLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length >= sizeof(ulong))
{
ulong significand = Significand;
if (!BitConverter.IsLittleEndian)
{
significand = BinaryPrimitives.ReverseEndianness(significand);
}
Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), significand);
bytesWritten = sizeof(ulong);
return true;
}
else
{
bytesWritten = 0;
return false;
}
}
//
// IFloatingPointConstants
//
/// <inheritdoc cref="IFloatingPointConstants{TSelf}.E" />
static double IFloatingPointConstants<double>.E => Math.E;
/// <inheritdoc cref="IFloatingPointConstants{TSelf}.Pi" />
static double IFloatingPointConstants<double>.Pi => Pi;
/// <inheritdoc cref="IFloatingPointConstants{TSelf}.Tau" />
static double IFloatingPointConstants<double>.Tau => Tau;
//
// IFloatingPointIeee754
//
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Epsilon" />
static double IFloatingPointIeee754<double>.Epsilon => Epsilon;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.NaN" />
static double IFloatingPointIeee754<double>.NaN => NaN;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.NegativeInfinity" />
static double IFloatingPointIeee754<double>.NegativeInfinity => NegativeInfinity;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.NegativeZero" />
static double IFloatingPointIeee754<double>.NegativeZero => NegativeZero;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.PositiveInfinity" />
static double IFloatingPointIeee754<double>.PositiveInfinity => PositiveInfinity;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Atan2(TSelf, TSelf)" />
[Intrinsic]
public static double Atan2(double y, double x) => Math.Atan2(y, x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Atan2Pi(TSelf, TSelf)" />
public static double Atan2Pi(double y, double x) => Atan2(y, x) / Pi;
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.BitDecrement(TSelf)" />
public static double BitDecrement(double x) => Math.BitDecrement(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.BitIncrement(TSelf)" />
public static double BitIncrement(double x) => Math.BitIncrement(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.FusedMultiplyAdd(TSelf, TSelf, TSelf)" />
[Intrinsic]
public static double FusedMultiplyAdd(double left, double right, double addend) => Math.FusedMultiplyAdd(left, right, addend);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Ieee754Remainder(TSelf, TSelf)" />
public static double Ieee754Remainder(double left, double right) => Math.IEEERemainder(left, right);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ILogB(TSelf)" />
public static int ILogB(double x) => Math.ILogB(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Lerp(TSelf, TSelf, TSelf)" />
public static double Lerp(double value1, double value2, double amount) => (value1 * (1.0 - amount)) + (value2 * amount);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ReciprocalEstimate(TSelf)" />
public static double ReciprocalEstimate(double x) => Math.ReciprocalEstimate(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ReciprocalSqrtEstimate(TSelf)" />
public static double ReciprocalSqrtEstimate(double x) => Math.ReciprocalSqrtEstimate(x);
/// <inheritdoc cref="IFloatingPointIeee754{TSelf}.ScaleB(TSelf, int)" />
public static double ScaleB(double x, int n) => Math.ScaleB(x, n);
// /// <inheritdoc cref="IFloatingPointIeee754{TSelf}.Compound(TSelf, TSelf)" />
// public static double Compound(double x, double n) => Math.Compound(x, n);
//
// IHyperbolicFunctions
//
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Acosh(TSelf)" />
[Intrinsic]
public static double Acosh(double x) => Math.Acosh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Asinh(TSelf)" />
[Intrinsic]
public static double Asinh(double x) => Math.Asinh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Atanh(TSelf)" />
[Intrinsic]
public static double Atanh(double x) => Math.Atanh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Cosh(TSelf)" />
[Intrinsic]
public static double Cosh(double x) => Math.Cosh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Sinh(TSelf)" />
[Intrinsic]
public static double Sinh(double x) => Math.Sinh(x);
/// <inheritdoc cref="IHyperbolicFunctions{TSelf}.Tanh(TSelf)" />
[Intrinsic]
public static double Tanh(double x) => Math.Tanh(x);
//
// IIncrementOperators
//
/// <inheritdoc cref="IIncrementOperators{TSelf}.op_Increment(TSelf)" />
static double IIncrementOperators<double>.operator ++(double value) => ++value;
//
// ILogarithmicFunctions
//
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log(TSelf)" />
[Intrinsic]
public static double Log(double x) => Math.Log(x);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log(TSelf, TSelf)" />
public static double Log(double x, double newBase) => Math.Log(x, newBase);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.LogP1(TSelf)" />
public static double LogP1(double x) => Math.Log(x + 1);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log2P1(TSelf)" />
public static double Log2P1(double x) => Math.Log2(x + 1);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log10(TSelf)" />
[Intrinsic]
public static double Log10(double x) => Math.Log10(x);
/// <inheritdoc cref="ILogarithmicFunctions{TSelf}.Log10P1(TSelf)" />
public static double Log10P1(double x) => Math.Log10(x + 1);
//
// IMinMaxValue
//
/// <inheritdoc cref="IMinMaxValue{TSelf}.MinValue" />
static double IMinMaxValue<double>.MinValue => MinValue;
/// <inheritdoc cref="IMinMaxValue{TSelf}.MaxValue" />
static double IMinMaxValue<double>.MaxValue => MaxValue;
//
// IModulusOperators
//
/// <inheritdoc cref="IModulusOperators{TSelf, TOther, TResult}.op_Modulus(TSelf, TOther)" />
static double IModulusOperators<double, double, double>.operator %(double left, double right) => left % right;
//
// IMultiplicativeIdentity
//
/// <inheritdoc cref="IMultiplicativeIdentity{TSelf, TResult}.MultiplicativeIdentity" />
static double IMultiplicativeIdentity<double, double>.MultiplicativeIdentity => MultiplicativeIdentity;
//
// IMultiplyOperators
//
/// <inheritdoc cref="IMultiplyOperators{TSelf, TOther, TResult}.op_Multiply(TSelf, TOther)" />
static double IMultiplyOperators<double, double, double>.operator *(double left, double right) => left * right;
//
// INumber
//
/// <inheritdoc cref="INumber{TSelf}.Clamp(TSelf, TSelf, TSelf)" />
public static double Clamp(double value, double min, double max) => Math.Clamp(value, min, max);
/// <inheritdoc cref="INumber{TSelf}.CopySign(TSelf, TSelf)" />
public static double CopySign(double value, double sign) => Math.CopySign(value, sign);
/// <inheritdoc cref="INumber{TSelf}.Max(TSelf, TSelf)" />
[Intrinsic]
public static double Max(double x, double y) => Math.Max(x, y);
/// <inheritdoc cref="INumber{TSelf}.MaxNumber(TSelf, TSelf)" />
[Intrinsic]
public static double MaxNumber(double x, double 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)" />
[Intrinsic]
public static double Min(double x, double y) => Math.Min(x, y);
/// <inheritdoc cref="INumber{TSelf}.MinNumber(TSelf, TSelf)" />
[Intrinsic]
public static double MinNumber(double x, double 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;