-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInt256.Api.cs
More file actions
1223 lines (1077 loc) · 66.3 KB
/
Copy pathInt256.Api.cs
File metadata and controls
1223 lines (1077 loc) · 66.3 KB
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
using System;
using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
namespace Stardust.Utilities
{
/// <summary>
/// Extended API surface for <see cref="Int256"/> that mirrors the BCL
/// <see cref="Int128"/> type: static math helpers, bit-level operations,
/// endian read/write, checked operators, float / decimal conversions, and
/// the full <see cref="INumber{TSelf}"/> / <see cref="IBinaryInteger{TSelf}"/>
/// generic-math interface set.
/// </summary>
public readonly partial struct Int256 :
INumber<Int256>,
IBinaryInteger<Int256>,
IBinaryNumber<Int256>,
IMinMaxValue<Int256>,
ISignedNumber<Int256>
#if NET8_0_OR_GREATER
,
IUtf8SpanFormattable,
IUtf8SpanParsable<Int256>
#endif
{
private const double TWO_POW_64_D = 1.8446744073709552e+19;
private const double TWO_POW_128_D = 3.4028236692093846e+38;
private const double TWO_POW_192_D = 6.277101735386680e+57;
private const double TWO_POW_255_D = 5.7896044618658097e+76; // 2^255
#region A. Static math helpers
/// <summary>Returns the absolute value.</summary>
/// <param name="value">The value.</param>
/// <returns>|value|.</returns>
/// <exception cref="OverflowException"><paramref name="value"/> is <see cref="MinValue"/>.</exception>
public static Int256 Abs(Int256 value)
{
if (IsNegative(value))
{
Int256 r = -value;
if (IsNegative(r)) throw new OverflowException();
return r;
}
return value;
}
/// <summary>Clamps a value to an inclusive range.</summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">Inclusive lower bound.</param>
/// <param name="max">Inclusive upper bound.</param>
/// <returns>The clamped value.</returns>
/// <exception cref="ArgumentException"><paramref name="min"/> is greater than <paramref name="max"/>.</exception>
public static Int256 Clamp(Int256 value, Int256 min, Int256 max)
{
if (min > max) throw new ArgumentException("min must be <= max", nameof(min));
if (value < min) return min;
if (value > max) return max;
return value;
}
/// <summary>Returns <paramref name="value"/> with the sign of <paramref name="sign"/>.</summary>
/// <param name="value">Magnitude source.</param>
/// <param name="sign">Sign source.</param>
/// <returns>|value| with sign(sign).</returns>
public static Int256 CopySign(Int256 value, Int256 sign)
{
Int256 abs = IsNegative(value) ? -value : value;
return IsNegative(sign) ? -abs : abs;
}
/// <summary>Computes the quotient and remainder of two values (truncating toward zero).</summary>
/// <param name="left">The dividend.</param>
/// <param name="right">The divisor.</param>
/// <returns>A tuple of (Quotient, Remainder).</returns>
/// <exception cref="DivideByZeroException"><paramref name="right"/> is zero.</exception>
public static (Int256 Quotient, Int256 Remainder) DivRem(Int256 left, Int256 right)
{
Int256 q = left / right;
Int256 r = left - q * right;
return (q, r);
}
/// <summary>Returns the greater of two values (signed comparison).</summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>The greater of the two values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int256 Max(Int256 x, Int256 y) => x >= y ? x : y;
/// <summary>Returns the lesser of two values (signed comparison).</summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>The lesser of the two values.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int256 Min(Int256 x, Int256 y) => x <= y ? x : y;
/// <summary>Returns the value with the greater magnitude (by |x| vs |y|).</summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>The value with greater magnitude.</returns>
public static Int256 MaxMagnitude(Int256 x, Int256 y)
{
Int256 ax = IsNegative(x) ? -x : x;
Int256 ay = IsNegative(y) ? -y : y;
if (ax > ay) return x;
if (ax < ay) return y;
return IsNegative(x) ? y : x;
}
/// <summary>Returns the value with the lesser magnitude.</summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>The value with lesser magnitude.</returns>
public static Int256 MinMagnitude(Int256 x, Int256 y)
{
Int256 ax = IsNegative(x) ? -x : x;
Int256 ay = IsNegative(y) ? -y : y;
if (ax < ay) return x;
if (ax > ay) return y;
return IsNegative(x) ? x : y;
}
/// <summary>INumberBase-required: alias for <see cref="MaxMagnitude"/>.</summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>See <see cref="MaxMagnitude"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int256 MaxMagnitudeNumber(Int256 x, Int256 y) => MaxMagnitude(x, y);
/// <summary>INumberBase-required: alias for <see cref="MinMagnitude"/>.</summary>
/// <param name="x">First value.</param>
/// <param name="y">Second value.</param>
/// <returns>See <see cref="MinMagnitude"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int256 MinMagnitudeNumber(Int256 x, Int256 y) => MinMagnitude(x, y);
/// <summary>Returns the sign of a value: -1 if negative, 0 if zero, 1 if positive.</summary>
/// <param name="value">The value.</param>
/// <returns>-1, 0, or 1.</returns>
public static int Sign(Int256 value)
{
if (IsNegative(value)) return -1;
if ((value._p0 | value._p1 | value._p2 | value._p3) == 0UL) return 0;
return 1;
}
#endregion
#region B. Bit-level operations
/// <summary>Counts the number of leading zero bits (0..256).</summary>
/// <param name="value">The value.</param>
/// <returns>Number of leading zero bits as an <see cref="Int256"/>.</returns>
public static Int256 LeadingZeroCount(Int256 value)
{
ulong p3 = value._p3;
ulong p2 = value._p2;
ulong p1 = value._p1;
ulong p0 = value._p0;
if (p3 != 0) return BitOperations.LeadingZeroCount(p3);
if (p2 != 0) return 64 + BitOperations.LeadingZeroCount(p2);
if (p1 != 0) return 128 + BitOperations.LeadingZeroCount(p1);
return 192 + BitOperations.LeadingZeroCount(p0);
}
/// <summary>Counts the number of trailing zero bits (0..256).</summary>
/// <param name="value">The value.</param>
/// <returns>Number of trailing zero bits as an <see cref="Int256"/>.</returns>
public static Int256 TrailingZeroCount(Int256 value)
{
ulong p0 = value._p0;
ulong p1 = value._p1;
ulong p2 = value._p2;
ulong p3 = value._p3;
if (p0 != 0) return BitOperations.TrailingZeroCount(p0);
if (p1 != 0) return 64 + BitOperations.TrailingZeroCount(p1);
if (p2 != 0) return 128 + BitOperations.TrailingZeroCount(p2);
return 192 + BitOperations.TrailingZeroCount(p3);
}
/// <summary>Counts the number of set bits (population count).</summary>
/// <param name="value">The value.</param>
/// <returns>Number of set bits as an <see cref="Int256"/>.</returns>
public static Int256 PopCount(Int256 value)
{
ulong p0 = value._p0;
ulong p1 = value._p1;
ulong p2 = value._p2;
ulong p3 = value._p3;
return BitOperations.PopCount(p0) + BitOperations.PopCount(p1)
+ BitOperations.PopCount(p2) + BitOperations.PopCount(p3);
}
/// <summary>Computes the integer (floor) base-2 logarithm.</summary>
/// <param name="value">The value. Must be non-negative.</param>
/// <returns>Floor(log2(value)) as an <see cref="Int256"/>.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is negative.</exception>
public static Int256 Log2(Int256 value)
{
if (IsNegative(value)) throw new ArgumentOutOfRangeException(nameof(value));
ulong p0 = value._p0;
ulong p1 = value._p1;
ulong p2 = value._p2;
ulong p3 = value._p3;
if (p3 != 0) return 192 + BitOperations.Log2(p3);
if (p2 != 0) return 128 + BitOperations.Log2(p2);
if (p1 != 0) return 64 + BitOperations.Log2(p1);
return BitOperations.Log2(p0);
}
/// <summary>Rotates the value left by the specified number of bits (mod 256).</summary>
/// <param name="value">The value to rotate.</param>
/// <param name="rotateAmount">Rotate amount (interpreted mod 256).</param>
/// <returns>The rotated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int256 RotateLeft(Int256 value, int rotateAmount)
=> (Int256)UInt256.RotateLeft((UInt256)value, rotateAmount);
/// <summary>Rotates the value right by the specified number of bits (mod 256).</summary>
/// <param name="value">The value to rotate.</param>
/// <param name="rotateAmount">Rotate amount (interpreted mod 256).</param>
/// <returns>The rotated value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int256 RotateRight(Int256 value, int rotateAmount)
=> (Int256)UInt256.RotateRight((UInt256)value, rotateAmount);
/// <summary>Returns the fixed byte size of the type: 32.</summary>
/// <returns>Always 32.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetByteCount() => 32;
/// <summary>Returns the number of bits needed to represent the current value. Matches BCL <see cref="Int128"/> semantics.</summary>
/// <returns>Significant bit length.</returns>
public int GetShortestBitLength()
{
if (IsNegative(this))
{
UInt256 u = ~(UInt256)this;
int lzc = u._p3 != 0 ? BitOperations.LeadingZeroCount(u._p3)
: u._p2 != 0 ? 64 + BitOperations.LeadingZeroCount(u._p2)
: u._p1 != 0 ? 128 + BitOperations.LeadingZeroCount(u._p1)
: u._p0 != 0 ? 192 + BitOperations.LeadingZeroCount(u._p0)
: 256;
return 257 - lzc;
}
UInt256 up = (UInt256)this;
if (up._p3 != 0) return 256 - BitOperations.LeadingZeroCount(up._p3);
if (up._p2 != 0) return 192 - BitOperations.LeadingZeroCount(up._p2);
if (up._p1 != 0) return 128 - BitOperations.LeadingZeroCount(up._p1);
if (up._p0 != 0) return 64 - BitOperations.LeadingZeroCount(up._p0);
return 0;
}
#endregion
#region C. Is-predicates
/// <summary>Determines whether the value is zero.</summary>
/// <param name="value">The value.</param>
/// <returns><c>true</c> if zero.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsZero(Int256 value) => (value._p0 | value._p1 | value._p2 | value._p3) == 0UL;
/// <summary>Determines whether the value is an even integer.</summary>
/// <param name="value">The value.</param>
/// <returns><c>true</c> if even.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsEvenInteger(Int256 value) => (value._p0 & 1UL) == 0UL;
/// <summary>Determines whether the value is an odd integer.</summary>
/// <param name="value">The value.</param>
/// <returns><c>true</c> if odd.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsOddInteger(Int256 value) => (value._p0 & 1UL) != 0UL;
/// <summary>Determines whether the value is a (positive) power of two.</summary>
/// <param name="value">The value.</param>
/// <returns><c>true</c> if <paramref name="value"/> is a positive power of two.</returns>
public static bool IsPow2(Int256 value)
{
if (IsNegative(value) || IsZero(value)) return false;
return UInt256.PopCount((UInt256)value) == UInt256.One;
}
/// <summary>Returns <c>true</c> when the value is negative.</summary>
/// <param name="value">The value.</param>
/// <returns><c>true</c> if negative.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNegative(Int256 value) => (value._p3 & SIGN_BIT) != 0UL;
/// <summary>Returns <c>true</c> when the value is zero or positive (matches BCL Int128 semantics).</summary>
/// <param name="value">The value.</param>
/// <returns><c>true</c> if non-negative.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPositive(Int256 value) => (value._p3 & SIGN_BIT) == 0UL;
/// <summary>Always returns <c>true</c> for fixed-width integers.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>true</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsCanonical(Int256 value) => true;
/// <summary>Always returns <c>false</c> for real integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>false</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsComplexNumber(Int256 value) => false;
/// <summary>Always returns <c>true</c> for integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>true</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsFinite(Int256 value) => true;
/// <summary>Always returns <c>false</c> for real integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>false</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsImaginaryNumber(Int256 value) => false;
/// <summary>Always returns <c>false</c> for integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>false</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsInfinity(Int256 value) => false;
/// <summary>Always returns <c>true</c> for integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>true</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsInteger(Int256 value) => true;
/// <summary>Always returns <c>false</c> for integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>false</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNaN(Int256 value) => false;
/// <summary>Always returns <c>false</c> for integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>false</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNegativeInfinity(Int256 value) => false;
/// <summary>Returns <c>true</c> when the value is non-zero.</summary>
/// <param name="value">The value.</param>
/// <returns><c>true</c> if non-zero.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNormal(Int256 value) => !IsZero(value);
/// <summary>Always returns <c>false</c> for integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>false</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPositiveInfinity(Int256 value) => false;
/// <summary>Always returns <c>true</c> for integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>true</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsRealNumber(Int256 value) => true;
/// <summary>Always returns <c>false</c> for integer types.</summary>
/// <param name="value">Ignored.</param>
/// <returns>Always <c>false</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSubnormal(Int256 value) => false;
#endregion
#region D. Endian Read/Write
/// <summary>Reads an <see cref="Int256"/> value from a big-endian byte sequence.</summary>
/// <param name="source">The source bytes in big-endian order.</param>
/// <param name="isUnsigned"><c>true</c> if <paramref name="source"/> represents an unsigned value;
/// <c>false</c> if signed with sign-extension.</param>
/// <returns>The parsed value.</returns>
/// <exception cref="OverflowException">The source exceeds 256 bits or has sign overflow.</exception>
public static Int256 ReadBigEndian(ReadOnlySpan<byte> source, bool isUnsigned)
{
if (!TryReadBigEndian(source, isUnsigned, out Int256 value))
throw new OverflowException("Value too large for Int256.");
return value;
}
/// <summary>Reads an <see cref="Int256"/> value from a little-endian byte sequence.</summary>
/// <param name="source">The source bytes in little-endian order.</param>
/// <param name="isUnsigned"><c>true</c> if <paramref name="source"/> represents an unsigned value.</param>
/// <returns>The parsed value.</returns>
/// <exception cref="OverflowException">The source exceeds 256 bits or has sign overflow.</exception>
public static Int256 ReadLittleEndian(ReadOnlySpan<byte> source, bool isUnsigned)
{
if (!TryReadLittleEndian(source, isUnsigned, out Int256 value))
throw new OverflowException("Value too large for Int256.");
return value;
}
/// <summary>Attempts to read an <see cref="Int256"/> from a big-endian byte sequence.</summary>
/// <param name="source">The source bytes.</param>
/// <param name="isUnsigned">Whether the source is unsigned.</param>
/// <param name="value">The parsed value on success.</param>
/// <returns><c>true</c> on success.</returns>
public static bool TryReadBigEndian(ReadOnlySpan<byte> source, bool isUnsigned, out Int256 value)
{
if (source.IsEmpty) { value = default; return true; }
int len = source.Length;
bool sourceNeg = !isUnsigned && (source[0] & 0x80) != 0;
byte signByte = sourceNeg ? (byte)0xFF : (byte)0;
if (len > 32)
{
int excess = len - 32;
for (int i = 0; i < excess; i++)
if (source[i] != signByte) { value = default; return false; }
source = source[excess..];
len = 32;
}
if (isUnsigned && len == 32 && (source[0] & 0x80) != 0) { value = default; return false; }
Span<byte> buf = stackalloc byte[32];
if (sourceNeg) buf.Fill(0xFF);
source.CopyTo(buf[(32 - len)..]);
value = new Int256(buf, isBigEndian: true);
return true;
}
/// <summary>Attempts to read an <see cref="Int256"/> from a little-endian byte sequence.</summary>
/// <param name="source">The source bytes.</param>
/// <param name="isUnsigned">Whether the source is unsigned.</param>
/// <param name="value">The parsed value on success.</param>
/// <returns><c>true</c> on success.</returns>
public static bool TryReadLittleEndian(ReadOnlySpan<byte> source, bool isUnsigned, out Int256 value)
{
if (source.IsEmpty) { value = default; return true; }
int len = source.Length;
bool sourceNeg = !isUnsigned && (source[len - 1] & 0x80) != 0;
byte signByte = sourceNeg ? (byte)0xFF : (byte)0;
if (len > 32)
{
for (int i = 32; i < len; i++)
if (source[i] != signByte) { value = default; return false; }
source = source[..32];
len = 32;
}
if (isUnsigned && len == 32 && (source[len - 1] & 0x80) != 0) { value = default; return false; }
Span<byte> buf = stackalloc byte[32];
if (sourceNeg) buf.Fill(0xFF);
source.CopyTo(buf);
value = new Int256(buf, isBigEndian: false);
return true;
}
/// <summary>Attempts to write this value in big-endian order to a destination span.</summary>
/// <param name="destination">The destination span (must be at least 32 bytes).</param>
/// <param name="bytesWritten">Bytes written on success.</param>
/// <returns><c>true</c> on success.</returns>
public bool TryWriteBigEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length < 32) { bytesWritten = 0; return false; }
ulong p3 = _p3;
ulong p2 = _p2;
ulong p1 = _p1;
ulong p0 = _p0;
BinaryPrimitives.WriteUInt64BigEndian(destination[..8], p3);
BinaryPrimitives.WriteUInt64BigEndian(destination.Slice(8, 8), p2);
BinaryPrimitives.WriteUInt64BigEndian(destination.Slice(16, 8), p1);
BinaryPrimitives.WriteUInt64BigEndian(destination.Slice(24, 8), p0);
bytesWritten = 32;
return true;
}
/// <summary>Attempts to write this value in little-endian order to a destination span.</summary>
/// <param name="destination">The destination span (must be at least 32 bytes).</param>
/// <param name="bytesWritten">Bytes written on success.</param>
/// <returns><c>true</c> on success.</returns>
public bool TryWriteLittleEndian(Span<byte> destination, out int bytesWritten)
{
if (destination.Length < 32) { bytesWritten = 0; return false; }
ulong p0 = _p0;
ulong p1 = _p1;
ulong p2 = _p2;
ulong p3 = _p3;
BinaryPrimitives.WriteUInt64LittleEndian(destination[..8], p0);
BinaryPrimitives.WriteUInt64LittleEndian(destination.Slice(8, 8), p1);
BinaryPrimitives.WriteUInt64LittleEndian(destination.Slice(16, 8), p2);
BinaryPrimitives.WriteUInt64LittleEndian(destination.Slice(24, 8), p3);
bytesWritten = 32;
return true;
}
/// <summary>Writes this value in big-endian order to a destination span.</summary>
/// <param name="destination">The destination span (must be at least 32 bytes).</param>
/// <returns>Bytes written.</returns>
/// <exception cref="ArgumentException">Destination is too short.</exception>
public int WriteBigEndian(Span<byte> destination)
{
if (!TryWriteBigEndian(destination, out int n))
throw new ArgumentException("Destination too short.", nameof(destination));
return n;
}
/// <summary>Writes this value in little-endian order to a destination span.</summary>
/// <param name="destination">The destination span (must be at least 32 bytes).</param>
/// <returns>Bytes written.</returns>
/// <exception cref="ArgumentException">Destination is too short.</exception>
public int WriteLittleEndian(Span<byte> destination)
{
if (!TryWriteLittleEndian(destination, out int n))
throw new ArgumentException("Destination too short.", nameof(destination));
return n;
}
#endregion
#region E. Checked operators
/// <summary>Checked addition; throws on signed overflow.</summary>
/// <param name="a">Left operand.</param>
/// <param name="b">Right operand.</param>
/// <returns>The sum.</returns>
/// <exception cref="OverflowException">The signed result does not fit in <see cref="Int256"/>.</exception>
public static Int256 operator checked +(Int256 a, Int256 b)
{
Int256 r = a + b;
bool aN = IsNegative(a), bN = IsNegative(b), rN = IsNegative(r);
if (aN == bN && aN != rN) throw new OverflowException();
return r;
}
/// <summary>Checked subtraction; throws on signed overflow.</summary>
/// <param name="a">Left operand.</param>
/// <param name="b">Right operand.</param>
/// <returns>The difference.</returns>
/// <exception cref="OverflowException">The signed result does not fit in <see cref="Int256"/>.</exception>
public static Int256 operator checked -(Int256 a, Int256 b)
{
Int256 r = a - b;
bool aN = IsNegative(a), bN = IsNegative(b), rN = IsNegative(r);
if (aN != bN && aN != rN) throw new OverflowException();
return r;
}
/// <summary>Checked unary negation; throws on <see cref="MinValue"/>.</summary>
/// <param name="a">The operand.</param>
/// <returns>-<paramref name="a"/>.</returns>
/// <exception cref="OverflowException"><paramref name="a"/> is <see cref="MinValue"/>.</exception>
public static Int256 operator checked -(Int256 a)
{
if (a == MinValue) throw new OverflowException();
return -a;
}
/// <summary>Checked multiplication; throws on signed overflow.</summary>
/// <param name="a">Left operand.</param>
/// <param name="b">Right operand.</param>
/// <returns>The product.</returns>
/// <exception cref="OverflowException">The signed result does not fit in <see cref="Int256"/>.</exception>
public static Int256 operator checked *(Int256 a, Int256 b)
{
if (IsZero(a) || IsZero(b)) return Zero;
bool aN = IsNegative(a), bN = IsNegative(b);
UInt256 au = aN ? (UInt256)(-a) : (UInt256)a;
UInt256 bu = bN ? (UInt256)(-b) : (UInt256)b;
UInt256 prod = checked(au * bu);
bool resultNeg = aN ^ bN;
UInt256 limit = resultNeg
? new UInt256((UInt128)1 << 127, UInt128.Zero) // |MinValue|
: new UInt256(~UInt128.Zero ^ ((UInt128)1 << 127), ~UInt128.Zero); // MaxValue
if (prod > limit) throw new OverflowException();
if (resultNeg)
{
if (prod == new UInt256((UInt128)1 << 127, UInt128.Zero)) return MinValue;
return (Int256)(UInt256.Zero - prod);
}
return new Int256(prod.Upper, prod.Lower);
}
/// <summary>Checked division; throws on <see cref="MinValue"/> / -1.</summary>
/// <param name="a">Left operand.</param>
/// <param name="b">Right operand.</param>
/// <returns>The quotient.</returns>
/// <exception cref="OverflowException"><paramref name="a"/> is <see cref="MinValue"/> and <paramref name="b"/> is -1.</exception>
/// <exception cref="DivideByZeroException"><paramref name="b"/> is zero.</exception>
public static Int256 operator checked /(Int256 a, Int256 b)
{
if (a == MinValue && b == NegativeOne) throw new OverflowException();
return a / b;
}
/// <summary>Checked increment; throws on <see cref="MaxValue"/>.</summary>
/// <param name="a">The value to increment.</param>
/// <returns>The incremented value.</returns>
/// <exception cref="OverflowException"><paramref name="a"/> is <see cref="MaxValue"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int256 operator checked ++(Int256 a) => checked(a + One);
/// <summary>Checked decrement; throws on <see cref="MinValue"/>.</summary>
/// <param name="a">The value to decrement.</param>
/// <returns>The decremented value.</returns>
/// <exception cref="OverflowException"><paramref name="a"/> is <see cref="MinValue"/>.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int256 operator checked --(Int256 a) => checked(a - One);
#endregion
#region F. Additional parsing
/// <summary>Parses using the specified <paramref name="style"/> and culture.</summary>
/// <param name="s">The input string.</param>
/// <param name="style">Number style to apply.</param>
/// <param name="provider">Culture provider.</param>
/// <returns>The parsed value.</returns>
/// <exception cref="ArgumentNullException"><paramref name="s"/> is null.</exception>
public static Int256 Parse(string s, NumberStyles style, IFormatProvider? provider)
{
if (s is null) throw new ArgumentNullException(nameof(s));
return Parse(s.AsSpan(), style, provider);
}
/// <summary>Parses a span using the specified <paramref name="style"/> and culture.</summary>
/// <param name="s">The input span.</param>
/// <param name="style">Number style to apply.</param>
/// <param name="provider">Culture provider.</param>
/// <returns>The parsed value.</returns>
public static Int256 Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider)
{
if (style == NumberStyles.Integer || style == NumberStyles.None)
return Parse(s.ToString(), NumberStyles.Integer);
if ((style & NumberStyles.HexNumber) == NumberStyles.HexNumber)
return Parse(s.ToString(), NumberStyles.HexNumber);
BigInteger big = BigInteger.Parse(s.ToString(), style, provider ?? CultureInfo.InvariantCulture);
return FromBigInteger(big);
}
/// <summary>Attempts to parse a string using the specified style and culture.</summary>
/// <param name="s">The input.</param>
/// <param name="style">Number style.</param>
/// <param name="provider">Culture provider.</param>
/// <param name="result">Parsed value on success.</param>
/// <returns><c>true</c> on success.</returns>
public static bool TryParse([NotNullWhen(true)] string? s, NumberStyles style, IFormatProvider? provider, out Int256 result)
{
if (s is null) { result = default; return false; }
try { result = Parse(s, style, provider); return true; }
catch { result = default; return false; }
}
/// <summary>Attempts to parse a span using the specified style and culture.</summary>
/// <param name="s">The input.</param>
/// <param name="style">Number style.</param>
/// <param name="provider">Culture provider.</param>
/// <param name="result">Parsed value on success.</param>
/// <returns><c>true</c> on success.</returns>
public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out Int256 result)
{
try { result = Parse(s, style, provider); return true; }
catch { result = default; return false; }
}
#endregion
#region G. UTF-8 parsing/formatting
/// <summary>Formats this value into a UTF-8 destination span.</summary>
/// <param name="utf8Destination">The destination span.</param>
/// <param name="bytesWritten">Bytes written on success.</param>
/// <param name="format">Format string.</param>
/// <param name="provider">Culture provider.</param>
/// <returns><c>true</c> on success.</returns>
public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
{
string s = ToString(format.IsEmpty ? "G" : format.ToString(), provider);
int needed = Encoding.UTF8.GetByteCount(s);
if (utf8Destination.Length < needed) { bytesWritten = 0; return false; }
bytesWritten = Encoding.UTF8.GetBytes(s, utf8Destination);
return true;
}
#if NET8_0_OR_GREATER
/// <summary>Parses a value from a UTF-8 byte sequence.</summary>
/// <param name="utf8Text">The UTF-8 input.</param>
/// <param name="provider">Culture provider.</param>
/// <returns>The parsed value.</returns>
public static Int256 Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider)
=> Parse(Encoding.UTF8.GetString(utf8Text), NumberStyles.Integer, provider);
/// <summary>Parses a value from a UTF-8 byte sequence with the given number style.</summary>
/// <param name="utf8Text">The UTF-8 input.</param>
/// <param name="style">Number style.</param>
/// <param name="provider">Culture provider.</param>
/// <returns>The parsed value.</returns>
public static Int256 Parse(ReadOnlySpan<byte> utf8Text, NumberStyles style, IFormatProvider? provider)
=> Parse(Encoding.UTF8.GetString(utf8Text), style, provider);
/// <summary>Attempts to parse a value from a UTF-8 byte sequence.</summary>
/// <param name="utf8Text">The UTF-8 input.</param>
/// <param name="provider">Culture provider.</param>
/// <param name="result">Parsed value on success.</param>
/// <returns><c>true</c> on success.</returns>
public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out Int256 result)
=> TryParse(Encoding.UTF8.GetString(utf8Text), NumberStyles.Integer, provider, out result);
/// <summary>Attempts to parse a value from a UTF-8 byte sequence.</summary>
/// <param name="utf8Text">The UTF-8 input.</param>
/// <param name="style">Number style.</param>
/// <param name="provider">Culture provider.</param>
/// <param name="result">Parsed value on success.</param>
/// <returns><c>true</c> on success.</returns>
public static bool TryParse(ReadOnlySpan<byte> utf8Text, NumberStyles style, IFormatProvider? provider, out Int256 result)
=> TryParse(Encoding.UTF8.GetString(utf8Text), style, provider, out result);
#endif
#endregion
#region H. Additional conversions
/// <summary>Implicitly widens a <see cref="char"/> to an <see cref="Int256"/>.</summary>
/// <param name="a">The input character.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Int256(char a) => new((long)a);
/// <summary>Implicitly widens a <see cref="byte"/> to an <see cref="Int256"/>.</summary>
/// <param name="a">The input byte.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Int256(byte a) => new((long)a);
/// <summary>Implicitly widens a <see cref="ushort"/> to an <see cref="Int256"/>.</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Int256(ushort a) => new((long)a);
/// <summary>Implicitly widens a <see cref="uint"/> to an <see cref="Int256"/>.</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Int256(uint a) => new((long)a);
/// <summary>Explicit widening from <see cref="ulong"/> (matches BCL cross-sign policy).</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator Int256(ulong a) => new(UInt128.Zero, a);
/// <summary>Explicit widening from <see cref="UInt128"/> (matches BCL cross-sign policy).</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator Int256(UInt128 a) => new(UInt128.Zero, a);
/// <summary>Converts a <see cref="float"/> to <see cref="Int256"/>; truncates toward zero.</summary>
/// <param name="value">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator Int256(float value) => (Int256)(double)value;
/// <summary>Converts a <see cref="double"/> to <see cref="Int256"/>; truncates toward zero.</summary>
/// <param name="value">The input.</param>
public static explicit operator Int256(double value)
{
if (double.IsNaN(value)) return Zero;
if (value >= TWO_POW_255_D) return MinValue; // matches BCL: positive overflow wraps to MinValue
if (value < -TWO_POW_255_D) return MinValue;
if (value >= 0.0)
{
UInt256 u = (UInt256)value;
return new Int256(u.Upper, u.Lower);
}
UInt256 mag = (UInt256)(-value);
return (Int256)(UInt256.Zero - mag);
}
/// <summary>Converts a <see cref="decimal"/> to <see cref="Int256"/>.</summary>
/// <param name="value">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator Int256(decimal value)
=> FromBigInteger(new BigInteger(decimal.Truncate(value)));
/// <summary>Checked conversion from <see cref="float"/>; throws on NaN, Infinity, or overflow.</summary>
/// <param name="value">The input.</param>
/// <exception cref="OverflowException">Value is outside valid range.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator checked Int256(float value) => checked((Int256)(double)value);
/// <summary>Checked conversion from <see cref="double"/>; throws on NaN, Infinity, or overflow.</summary>
/// <param name="value">The input.</param>
/// <exception cref="OverflowException">Value is outside valid range.</exception>
public static explicit operator checked Int256(double value)
{
if (double.IsNaN(value) || double.IsInfinity(value)) throw new OverflowException();
if (value >= TWO_POW_255_D || value < -TWO_POW_255_D) throw new OverflowException();
if (value >= 0.0)
{
UInt256 u = (UInt256)value;
return new Int256(u.Upper, u.Lower);
}
UInt256 mag = (UInt256)(-value);
return (Int256)(UInt256.Zero - mag);
}
/// <summary>Checked conversion from <see cref="decimal"/>.</summary>
/// <param name="value">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator checked Int256(decimal value)
=> FromBigInteger(new BigInteger(decimal.Truncate(value)));
/// <summary>Converts <see cref="Int256"/> to <see cref="float"/>.</summary>
/// <param name="value">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator float(Int256 value) => (float)(double)value;
/// <summary>Converts <see cref="Int256"/> to <see cref="double"/>.</summary>
/// <param name="value">The input.</param>
public static explicit operator double(Int256 value)
{
if (IsNegative(value))
{
UInt256 mag = (UInt256)(-value);
return -(double)mag;
}
return (double)(UInt256)value;
}
/// <summary>Converts <see cref="Int256"/> to <see cref="decimal"/>.</summary>
/// <param name="value">The input.</param>
/// <exception cref="OverflowException">Value does not fit in <see cref="decimal"/>.</exception>
public static explicit operator decimal(Int256 value)
{
bool neg = IsNegative(value);
UInt256 mag = neg ? (UInt256)(-value) : (UInt256)value;
if (mag._p3 != 0 || mag._p2 != 0 || mag._p1 > uint.MaxValue)
throw new OverflowException("Value does not fit in decimal.");
return new decimal(
(int)mag._p0,
(int)(mag._p0 >> 32),
(int)mag._p1,
isNegative: neg,
scale: 0);
}
/// <summary>Narrowing conversion to <see cref="byte"/> (keeps low 8 bits).</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator byte(Int256 a) => (byte)a._p0;
/// <summary>Narrowing conversion to <see cref="ushort"/> (keeps low 16 bits).</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator ushort(Int256 a) => (ushort)a._p0;
/// <summary>Narrowing conversion to <see cref="uint"/> (keeps low 32 bits).</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator uint(Int256 a) => (uint)a._p0;
/// <summary>Narrowing conversion to <see cref="ulong"/> (keeps low 64 bits).</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator ulong(Int256 a) => a._p0;
/// <summary>Narrowing conversion to <see cref="UInt128"/> (keeps low 128 bits).</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator UInt128(Int256 a) => new(a._p1, a._p0);
/// <summary>Narrowing conversion to <see cref="char"/> (keeps low 16 bits).</summary>
/// <param name="a">The input.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator char(Int256 a) => (char)(ushort)a._p0;
/// <summary>Checked narrowing to <see cref="byte"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside byte range.</exception>
public static explicit operator checked byte(Int256 a)
{
if (IsNegative(a)) throw new OverflowException();
if ((a._p1 | a._p2 | a._p3) != 0UL || a._p0 > byte.MaxValue) throw new OverflowException();
return (byte)a._p0;
}
/// <summary>Checked narrowing to <see cref="sbyte"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside sbyte range.</exception>
public static explicit operator checked sbyte(Int256 a)
{
long v = checked((long)a);
return checked((sbyte)v);
}
/// <summary>Checked narrowing to <see cref="ushort"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside ushort range.</exception>
public static explicit operator checked ushort(Int256 a)
{
if (IsNegative(a) || (a._p1 | a._p2 | a._p3) != 0UL || a._p0 > ushort.MaxValue) throw new OverflowException();
return (ushort)a._p0;
}
/// <summary>Checked narrowing to <see cref="short"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside short range.</exception>
public static explicit operator checked short(Int256 a)
{
long v = checked((long)a);
return checked((short)v);
}
/// <summary>Checked narrowing to <see cref="uint"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside uint range.</exception>
public static explicit operator checked uint(Int256 a)
{
if (IsNegative(a) || (a._p1 | a._p2 | a._p3) != 0UL || a._p0 > uint.MaxValue) throw new OverflowException();
return (uint)a._p0;
}
/// <summary>Checked narrowing to <see cref="int"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside int range.</exception>
public static explicit operator checked int(Int256 a)
{
long v = checked((long)a);
return checked((int)v);
}
/// <summary>Checked narrowing to <see cref="ulong"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside ulong range.</exception>
public static explicit operator checked ulong(Int256 a)
{
if (IsNegative(a) || (a._p1 | a._p2 | a._p3) != 0UL) throw new OverflowException();
return a._p0;
}
/// <summary>Checked narrowing to <see cref="long"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside long range.</exception>
public static explicit operator checked long(Int256 a)
{
// Valid range: value fits in signed 64 bits. Upper 192 bits must be sign-extension of bit 63 of _p0.
bool neg = IsNegative(a);
ulong expected = neg ? ulong.MaxValue : 0UL;
if (a._p1 != expected || a._p2 != expected || a._p3 != expected) throw new OverflowException();
if (neg && (a._p0 & SIGN_BIT) == 0UL) throw new OverflowException();
if (!neg && (a._p0 & SIGN_BIT) != 0UL) throw new OverflowException();
return (long)a._p0;
}
/// <summary>Checked narrowing to <see cref="UInt128"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside <see cref="UInt128"/> range.</exception>
public static explicit operator checked UInt128(Int256 a)
{
if (IsNegative(a) || (a._p2 | a._p3) != 0UL) throw new OverflowException();
return new UInt128(a._p1, a._p0);
}
/// <summary>Checked narrowing to <see cref="Int128"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside <see cref="Int128"/> range.</exception>
public static explicit operator checked Int128(Int256 a)
{
// Valid range: low 128 bits form a valid signed Int128. Upper 128 bits must
// be sign-extension of bit 127 (= top bit of _p1).
bool neg = IsNegative(a);
ulong expected = neg ? ulong.MaxValue : 0UL;
if (a._p2 != expected || a._p3 != expected) throw new OverflowException();
if (neg && (a._p1 & SIGN_BIT) == 0UL) throw new OverflowException();
if (!neg && (a._p1 & SIGN_BIT) != 0UL) throw new OverflowException();
return (Int128)new UInt128(a._p1, a._p0);
}
/// <summary>Checked narrowing to <see cref="char"/>.</summary>
/// <param name="a">The input.</param>
/// <exception cref="OverflowException">Value is outside char range.</exception>
public static explicit operator checked char(Int256 a)
{
if (IsNegative(a) || (a._p1 | a._p2 | a._p3) != 0UL || a._p0 > char.MaxValue) throw new OverflowException();
return (char)(ushort)a._p0;
}
#endregion
#region I. Identity properties / generic-math scaffolding
/// <summary>The additive identity (zero).</summary>
public static Int256 AdditiveIdentity => Zero;
/// <summary>The multiplicative identity (one).</summary>
public static Int256 MultiplicativeIdentity => One;
/// <summary>The radix (base) of this numeric type: 2.</summary>
public static int Radix => 2;
#if NET8_0_OR_GREATER
/// <summary>A value with every bit set (same as -1).</summary>
public static Int256 AllBitsSet => NegativeOne;
#endif
/// <summary>Creates an <see cref="Int256"/> from <paramref name="value"/>; throws on overflow.</summary>
/// <typeparam name="TOther">Source numeric type.</typeparam>
/// <param name="value">The source value.</param>