-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigFloat.cs
More file actions
1595 lines (1392 loc) · 77.5 KB
/
Copy pathBigFloat.cs
File metadata and controls
1595 lines (1392 loc) · 77.5 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
//========================================================================
// Badger Maths Library
// Copyright (C) 2018-2025 Mike Conroy
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//========================================================================
//========================================================================
// Based on code provided by Patrick Demian, see copyright notice below
// The above code has been modified, primarily to convert from a class to
// a struct and all of the consequent changes to the code.
// XML comments have also been added to the code
//========================================================================
// Downloaded from GitHub 15 April 2018
// https://github.com/Osinko/BigFloat
//========================================================================
//Copyright(C) 2014 Patrick Demian
//Permission is hereby granted, free of charge, to any person obtaining a copy of
//this software and associated documentation files (the "Software"), to deal in
//the Software without restriction, including without limitation the rights to
//use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
//of the Software, and to permit persons to whom the Software is furnished to do
//so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
//========================================================================
using System.Globalization;
using System.Numerics;
namespace Badger.Maths.Algebra
{
/// <summary>
/// The <see cref="BigFloat"/> struct represents a floating point number of arbitrary precision using
/// the <see cref="System.Numerics.BigInteger"/> struct for the numerator and denominator.
/// </summary>
[Serializable]
public readonly struct BigFloat : IComparable, IComparable<BigFloat>, IEquatable<BigFloat>
{
#region Fields
/// <summary>
/// The numerator of the BigFloat number.
/// </summary>
private readonly BigInteger _numerator;
/// <summary>
/// The denominator of the BigFloat number.
/// </summary>
private readonly BigInteger _denominator;
#endregion
#region Constructors
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of 0 (which is the same
/// as <see cref="BigFloat.Zero"/>)"/>
/// </summary>
public BigFloat()
{
this._numerator = BigInteger.Zero;
this._denominator = BigInteger.One;
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// where <paramref name="value"/> is parsed by the <see cref="BigFloat.Parse(string)"/> function
/// </summary>
/// <param name="value">A <see cref="String"/> representing a floating point value</param>
/// <remarks><paramref name="value"/> is sent to the <see cref="BigFloat.Parse(string)"/> function
/// which in turn uses the <see cref="BigInteger.Parse(string)"/> and <see cref="BigFloat.Reduce(BigFloat)"/> functions
/// to parse the string to a floating point value</remarks>
public BigFloat(string value)
{
BigFloat bf = Parse(value);
this._numerator = bf.Numerator;
this._denominator = bf.Denominator;
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of
/// <paramref name="numerator"/> / <paramref name="denominator"/>
/// </summary>
/// <param name="numerator">The numerator of the new <see cref="BigFloat"/> struct</param>
/// <param name="denominator">The denominator of the new <see cref="BigFloat"/> struct</param>
/// <exception cref="ArgumentException">This exception is thrown if the denominator is set to zero</exception>
public BigFloat(BigInteger numerator, BigInteger denominator)
{
this._numerator = numerator;
if (denominator == 0)
throw new ArgumentException("The denominator cannot be zero");
this._denominator = BigInteger.Abs(denominator);
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="BigInteger"/> value to be converted to a <see cref="BigFloat"/></param>
/// <remarks>The numerator is set to <paramref name="value"/> and the denominator is set to
/// <see cref="BigInteger.One"/></remarks>
public BigFloat(BigInteger value)
{
this._numerator = value;
this._denominator = BigInteger.One;
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> value to be copied</param>
/// <remarks>If <paramref name="value"/> is <c>Null</c> then the new <see cref="BigFloat"/> is
/// set to the equivalent of <see cref="BigFloat.Zero"/> else the numerator and denominator
/// are copied from <paramref name="value"/> to the new instance</remarks>
public BigFloat(BigFloat value)
{
if (BigFloat.Equals(value, null))
{
this._numerator = BigInteger.Zero;
this._denominator = BigInteger.One;
}
else
{
this._numerator = value.Numerator;
this._denominator = value.Denominator;
}
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="ulong"/> value to be converted to a <see cref="BigFloat"/></param>
/// <remarks>The numerator is set to <paramref name="value"/> and the denominator is set to
/// <see cref="BigInteger.One"/></remarks>
public BigFloat(ulong value)
{
this._numerator = new BigInteger(value);
this._denominator = BigInteger.One;
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="long"/> value to be converted to a <see cref="BigFloat"/></param>
/// <remarks>The numerator is set to <paramref name="value"/> and the denominator is set to
/// <see cref="BigInteger.One"/></remarks>
public BigFloat(long value)
{
this._numerator = new BigInteger(value);
this._denominator = BigInteger.One;
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="uint"/> value to be converted to a <see cref="BigFloat"/></param>
/// <remarks>The numerator is set to <paramref name="value"/> and the denominator is set to
/// <see cref="BigInteger.One"/></remarks>
public BigFloat(uint value)
{
this._numerator = new BigInteger(value);
this._denominator = BigInteger.One;
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="int"/> value to be converted to a <see cref="BigFloat"/></param>
/// <remarks>The numerator is set to <paramref name="value"/> and the denominator is set to
/// <see cref="BigInteger.One"/></remarks>
public BigFloat(int value)
{
this._numerator = new BigInteger(value);
this._denominator = BigInteger.One;
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="float"/> value to be converted to a <see cref="BigFloat"/></param>
/// <remarks><paramref name="value"/> is first converted to a string and then
/// <see cref="BigFloat.BigFloat(string)"/> is called</remarks>
public BigFloat(float value) : this(value.ToString("N99"))
{
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="double"/> value to be converted to a <see cref="BigFloat"/></param>
/// <remarks><paramref name="value"/> is first converted to a string and then
/// <see cref="BigFloat.BigFloat(string)"/> is called</remarks>
public BigFloat(double value) : this(value.ToString("N99"))
{
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="decimal"/> value to be converted to a <see cref="BigFloat"/></param>
/// <remarks><paramref name="value"/> is first converted to a string and then
/// <see cref="BigFloat.BigFloat(string)"/> is called</remarks>
public BigFloat(decimal value) : this(value.ToString("N99"))
{
}
/// <summary>
/// Initialises a new instance of the <see cref="BigFloat"/> struct with a value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="string"/> value to be converted to a <see cref="BigFloat"/></param>
/// <returns>A new instance of a <see cref="BigFloat"/> struct with a value <paramref name="value"/></returns>
/// <exception cref="ArgumentNullException">This exception is thrown if <paramref name="value"/> is <c>Null</c>></exception>
/// <remarks><see cref="BigFloat.Parse(string)"/> uses <see cref="BigInteger.Parse(string)"/> and
/// <see cref="BigFloat.Factor()"/> to convert the string to a <see cref="BigFloat"/></remarks>
public static BigFloat Parse(string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
value = value.Trim();
value = value.Replace(",", "");
int pos = value.IndexOf('.');
value = value.Replace(".", "");
if (pos < 0)
{
//no decimal point
BigInteger numerator = BigInteger.Parse(value);
return BigFloat.Reduce(new BigFloat(numerator));
}
else
{
//decimal point (length - pos - 1)
BigInteger numerator = BigInteger.Parse(value);
BigInteger denominator = BigInteger.Pow(10, value.Length - pos);
return BigFloat.Reduce(new BigFloat(numerator, denominator));
}
}
/// <summary>
/// Attempts to parse a <see cref="string"/> value into a <see cref="BigFloat"/> value
/// </summary>
/// <param name="value">The <see cref="string"/> to be parsed into a <see cref="BigFloat"/></param>
/// <param name="result">The result of the <see cref="TryParse(string, out BigFloat)"/> method</param>
/// <returns><para>If <paramref name="value"/> is successfully parsed to a <see cref="BigFloat"/> then
/// <see cref="TryParse(string, out BigFloat)"/> returns <c>true</c> otherwise <c>false</c></para>
/// <para>If <see cref="TryParse(string, out BigFloat)"/> succeeds then <paramref name="result"/>
/// is the parsed value of <paramref name="value"/></para>
/// <para>If <see cref="TryParse(string, out BigFloat)"/> fails then <paramref name="result"/>
/// is <see cref="BigFloat.Zero"/></para></returns>
public static bool TryParse(string value, out BigFloat result)
{
try
{
result = BigFloat.Parse(value);
return true;
}
catch (ArgumentNullException)
{
result = BigFloat.Zero;
return false;
}
catch (FormatException)
{
result = BigFloat.Zero;
return false;
}
}
#endregion
#region Public Static Fields
/// <summary>
/// Provides a constant value of 1
/// </summary>
public static readonly BigFloat One = new(1);
/// <summary>
/// Provides a constant value of 0
/// </summary>
public static readonly BigFloat Zero = new();
/// <summary>
/// Provides a constant value of -1
/// </summary>
public static readonly BigFloat MinusOne = new(-1);
/// <summary>
/// Provides a constant value of 0.5
/// </summary>
public static readonly BigFloat OneHalf = new(1, 2);
/// <summary>
/// Provides a constant value of Pi
/// </summary>
public static readonly BigFloat Pi = new(new BigInteger(22), new BigInteger(7));
#endregion
#region Properties / Accessors
/// <summary>
/// Gets the numerator of the <see cref="BigFloat"/> number.
/// </summary>
public BigInteger Numerator
{
get { return this._numerator; }
}
/// <summary>
/// Gets the denominator of the <see cref="BigFloat"/> number.
/// </summary>
public BigInteger Denominator
{
get { return this._denominator; }
}
/// <summary>
/// Gets the sign of the <see cref="BigFloat"/> number.
/// </summary>
public int Sign
{
get
{
return (this._numerator.Sign + this._denominator.Sign) switch
{
2 or -2 => 1,
0 => -1,
_ => 0,
};
}
}
#endregion
#region Static Operators
/// <summary>
/// The unary - operator, negates the value of <paramref name="value"/>
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> that will be negated</param>
/// <returns>A negated <see cref="BigFloat"/></returns>
public static BigFloat operator -(BigFloat value)
{
return BigFloat.Negate(value);
}
/// <summary>
/// Subtracts two <see cref="BigFloat"/> numbers and returns the result
/// </summary>
/// <param name="left"><paramref name="right"/> will be subtracted frim this <see cref="BigFloat"/></param>
/// <param name="right">The <see cref="BigFloat"/> that will extracted from <paramref name="left"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the substration of
/// <paramref name="right"/> from <paramref name="left"/></returns>
public static BigFloat operator -(BigFloat left, BigFloat right)
{
return BigFloat.Subtract(left, right);
}
/// <summary>
/// Decrements a <see cref="BigFloat"/> number by 1
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be decremented by 1</param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> minus <see cref="BigFloat.One"/></returns>
public static BigFloat operator --(BigFloat value)
{
return BigFloat.Decrement(value);
}
/// <summary>
/// Adds two <see cref="BigFloat"/> numbers together and returns the result
/// </summary>
/// <param name="left">The <see cref="BigFloat"/> to add to <paramref name="right"/></param>
/// <param name="right">The <see cref="BigFloat"/> to add to <paramref name="left"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the sum of <paramref name="left"/> and
/// <paramref name="right"/></returns>
public static BigFloat operator +(BigFloat left, BigFloat right)
{
return BigFloat.Add(left, right);
}
/// <summary>
/// The unary + operator, returns the value of <paramref name="value"/>, i.e. it is a no-op
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> that will be subject to the unary + operation</param>
/// <returns><paramref name="value"/></returns>
/// <remarks>This operator is implemented for consistency with the - operator, it is a no-op,
/// it does not change the value of <paramref name="value"/></remarks>
public static BigFloat operator +(BigFloat value)
{
return value;
}
/// <summary>
/// Increments a <see cref="BigFloat"/> number by 1
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be incremented by 1</param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> plus <see cref="BigFloat.One"/></returns>
/// <remarks>Internally uses the <see cref="BigFloat.Increment(BigFloat)"/> method</remarks>
public static BigFloat operator ++(BigFloat value)
{
return BigFloat.Increment(value);
}
/// <summary>
/// Calculates the remainder of the division of two <see cref="BigFloat"/> numbers
/// </summary>
/// <param name="left">The <see cref="BigFloat"/> that will be divided by <paramref name="right"/></param>
/// <param name="right"><paramref name="left"/> will be diveded by this <see cref="BigFloat"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the remainder of the division of
/// <paramref name="left"/> by <paramref name="right"/></returns>
/// <remarks>Internally uses the <see cref="BigFloat.Remainder(BigFloat, BigFloat)"/> method</remarks>
public static BigFloat operator %(BigFloat left, BigFloat right)
{
return BigFloat.Remainder(left, right);
}
/// <summary>
/// Multiplies two <see cref="BigFloat"/> numbers and returns the result
/// </summary>
/// <param name="left">The <see cref="BigFloat"/> that will be mutiplied by <paramref name="right"/></param>
/// <param name="right">The <see cref="BigFloat"/> that will be mutiplied by <paramref name="left"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the multiplication of
/// <paramref name="left"/> and <paramref name="right"/></returns>
/// <remarks>Interally uses the <see cref="BigFloat.Multiply(BigFloat, BigFloat)"/> method</remarks>
public static BigFloat operator *(BigFloat left, BigFloat right)
{
return BigFloat.Multiply(left, right);
}
/// <summary>
/// Divides two <see cref="BigFloat"/> numbers and returns the result
/// </summary>
/// <param name="left">The <see cref="BigFloat"/> that will be divided by <paramref name="right"/></param>
/// <param name="right"><paramref name="left"/> will be diveded by this <see cref="BigFloat"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the division of
/// <paramref name="left"/> by <paramref name="right"/></returns>
/// <remarks>Internally uses the <see cref="BigFloat.Divide(BigFloat, BigFloat)"/> method</remarks>
public static BigFloat operator /(BigFloat left, BigFloat right)
{
return BigFloat.Divide(left, right);
}
/// <summary>
/// Shifts the decimal point of a <see cref="BigFloat"/> number to the right by <paramref name="shift"/>
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be shifted right</param>
/// <param name="shift">The number of places to ShiftRight</param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> right shifted by
/// <paramref name="shift"/> places</returns>
/// <remarks>Internally uses the <see cref="BigFloat.ShiftDecimalRight(BigFloat, int)"/> method</remarks>
public static BigFloat operator >>(BigFloat value, int shift)
{
return BigFloat.ShiftDecimalRight(value, shift);
}
/// <summary>
/// Shifts the decimal point of a <see cref="BigFloat"/> number to the left by <paramref name="shift"/>
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be shifted left</param>
/// <param name="shift">The number of places to ShiftLeft</param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> left shifted by
/// <paramref name="shift"/> places</returns>
/// <remarks>Internally uses the <see cref="BigFloat.ShiftDecimalLeft(BigFloat, int)"/> method</remarks>
public static BigFloat operator <<(BigFloat value, int shift)
{
return BigFloat.ShiftDecimalLeft(value, shift);
}
/// <summary>
/// Raises a <see cref="BigFloat"/> number to the power of an integer exponent
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> that will be raised to the power of <paramref name="exponent"/></param>
/// <param name="exponent"><paramref name="value"/> will be raised to the power of this <see cref="int"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> raised to the
/// power of <paramref name="exponent"/>A new <see cref="BigFloat"/> instance that is
/// <paramref name="value"/> raised to the power of <paramref name="exponent"/></returns>
/// <remarks>Internally uses the <see cref="BigFloat.Pow(BigFloat, int)"/> method</remarks>
public static BigFloat operator ^(BigFloat left, int right)
{
return BigFloat.Pow(left, right);
}
/// <summary>
/// Compares two <see cref="BigFloat"/> numbers for inequality
/// </summary>
/// <param name="left">A <see cref="BigFloat"/> to be compared to <paramref name="right"/></param>
/// <param name="right">A <see cref="BigFloat"/> to be compared to <paramref name="left"/></param>
/// <returns>Returns <c>true</c> if <paramref name="left"/> is not equal to <paramref name="right"/></returns>
/// <remarks>Internally uses the <see cref="BigFloat.Compare(BigFloat, BigFloat)"/> method</remarks>
public static bool operator !=(BigFloat left, BigFloat right)
{
return BigFloat.Compare(left, right) != 0;
}
/// <summary>
/// Compares two <see cref="BigFloat"/> numbers for equality
/// </summary>
/// <param name="left">A <see cref="BigFloat"/> to be compared to <paramref name="right"/></param>
/// <param name="right">A <see cref="BigFloat"/> to be compared to <paramref name="left"/></param>
/// <returns>Returns <c>true</c> if <paramref name="left"/> is equal to <paramref name="right"/></returns>
/// <example><list type="bullet">
/// <item><description>1.5 (1/2) will be equal to 1.5 (1/2)</description></item>
/// <item><description>1.5 (1/2) will be equal to 1.5 (4/8)</description></item>
/// <item><description>2.3 (23/10) will not be equal to 3.2 (16/5)</description></item>
/// </list></example>
/// <remarks>Internally uses the <see cref="BigFloat.Compare(BigFloat, BigFloat)"/> method, this method
/// does not require that the two <see cref="BigFloat.Denominator"/> values be the same</remarks>
public static bool operator ==(BigFloat left, BigFloat right)
{
return BigFloat.Compare(left, right) == 0;
}
/// <summary>
/// Conducts a <c>less than</c> comparison on two <see cref="BigFloat"/> values
/// </summary>
/// <param name="left">A <see cref="BigFloat"/> to be compared to <paramref name="right"/></param>
/// <param name="right">A <see cref="BigFloat"/> to be compared to <paramref name="left"/></param>
/// <returns>Returns <c>true</c> if <paramref name="left"/> is less than <paramref name="right"/></returns>
/// <remarks>Internally uses the <see cref="BigFloat.Compare(BigFloat, BigFloat)"/> method</remarks>
public static bool operator <(BigFloat left, BigFloat right)
{
return BigFloat.Compare(left, right) < 0;
}
/// <summary>
/// Conducts a <c>less than or equal to</c> comparison on two <see cref="BigFloat"/> values
/// </summary>
/// <param name="left">A <see cref="BigFloat"/> to be compared to <paramref name="right"/></param>
/// <param name="right">A <see cref="BigFloat"/> to be compared to <paramref name="left"/></param>
/// <returns>Returns <c>true</c> if <paramref name="left"/> is less than or equal to <paramref name="right"/></returns>
/// <remarks>Internally uses the <see cref="BigFloat.Compare(BigFloat, BigFloat)"/> method</remarks>
public static bool operator <=(BigFloat left, BigFloat right)
{
return BigFloat.Compare(left, right) <= 0;
}
/// <summary>
/// Conducts a <c>greater than</c> comparison on two <see cref="BigFloat"/> values
/// </summary>
/// <param name="left">A <see cref="BigFloat"/> to be compared to <paramref name="right"/></param>
/// <param name="right">A <see cref="BigFloat"/> to be compared to <paramref name="left"/></param>
/// <returns>Returns <c>true</c> if <paramref name="left"/> is greater than <paramref name="right"/></returns>
/// <remarks>Internally uses the <see cref="BigFloat.Compare(BigFloat, BigFloat)"/> method</remarks>
public static bool operator >(BigFloat left, BigFloat right)
{
return BigFloat.Compare(left, right) > 0;
}
/// <summary>
/// Conducts a <c>greater than or equal to</c> comparison on two <see cref="BigFloat"/> values
/// </summary>
/// <param name="left">A <see cref="BigFloat"/> to be compared to <paramref name="right"/></param>
/// <param name="right">A <see cref="BigFloat"/> to be compared to <paramref name="left"/></param>
/// <returns>Returns <c>true</c> if <paramref name="left"/> is greater than or equal to <paramref name="right"/></returns>
/// <remarks>Internally uses the <see cref="BigFloat.Compare(BigFloat, BigFloat)"/> method</remarks>
public static bool operator >=(BigFloat left, BigFloat right)
{
return BigFloat.Compare(left, right) >= 0;
}
#endregion
#region Static Arithmetic Methods
/// <summary>
/// Calculates the machine epsilon of the <see cref="BigFloat"/> struct
/// </summary>
/// <returns>Returns the machine epsilon of the <see cref="BigFloat"/> struct</returns>
/// <remarks>Machine epsilon is a concept in numerical analysis that represents the smallest value that can
/// be added to 1 to produce a result distinguishable from 1 in the floating-point system. It's essentially
/// a measure of the precision or the "gap" between representable floating-point numbers</remarks>
public static BigFloat GetEpsilon()
{
BigFloat epsilon = new(BigInteger.One, BigInteger.One); // Start with 1
BigFloat one = BigFloat.One;
while (one + epsilon != one)
{
epsilon = new BigFloat(epsilon.Numerator, epsilon.Denominator * 2); // Halve epsilon
}
return epsilon;
}
/// <summary>
/// Adds two <see cref="BigFloat"/> numbers together and returns the result
/// </summary>
/// <param name="left">The <see cref="BigFloat"/> to add to <paramref name="right"/></param>
/// <param name="right">The <see cref="BigFloat"/> to add to <paramref name="left"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the sum of <paramref name="left"/> and
/// <paramref name="right"/></returns>
/// <exception cref="ArgumentNullException">This exception is thrown if either
/// <paramref name="left"/> or <paramref name="right"/> is <c>null</c></exception>
public static BigFloat Add(BigFloat left, BigFloat right)
{
if (BigFloat.Equals(left, null))
throw new ArgumentNullException(nameof(left));
if (BigFloat.Equals(right, null))
throw new ArgumentNullException(nameof(right));
BigInteger newNumerator = BigInteger.Add(BigInteger.Multiply(left.Numerator, right.Denominator), BigInteger.Multiply(right.Numerator, left.Denominator));
BigInteger newDenominator = BigInteger.Multiply(left.Numerator, right.Denominator);
return new BigFloat(newNumerator, newDenominator);
}
/// <summary>
/// Subtracts two <see cref="BigFloat"/> numbers and returns the result
/// </summary>
/// <param name="left"><paramref name="right"/> will be subtracted frim this <see cref="BigFloat"/></param>
/// <param name="right">The <see cref="BigFloat"/> that will extracted from <paramref name="left"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the substration of
/// <paramref name="right"/> from <paramref name="left"/></returns>
/// <exception cref="ArgumentNullException">This exception is thrown if either
/// <paramref name="left"/> or <paramref name="right"/> is <c>null</c></exception>
public static BigFloat Subtract(BigFloat left, BigFloat right)
{
if (BigFloat.Equals(left, null))
throw new ArgumentNullException(nameof(left));
if (BigFloat.Equals(right, null))
throw new ArgumentNullException(nameof(right));
BigInteger newNumerator = BigInteger.Subtract(BigInteger.Multiply(left.Numerator, right.Denominator), BigInteger.Multiply(right.Numerator, left.Denominator));
BigInteger newDenominator = BigInteger.Multiply(left.Numerator, right.Denominator);
return new BigFloat(newNumerator, newDenominator);
}
/// <summary>
/// Multiplies two <see cref="BigFloat"/> numbers and returns the result
/// </summary>
/// <param name="left">The <see cref="BigFloat"/> that will be mutiplied by <paramref name="right"/></param>
/// <param name="right">The <see cref="BigFloat"/> that will be mutiplied by <paramref name="left"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the multiplication of
/// <paramref name="left"/> and <paramref name="right"/></returns>
/// <exception cref="ArgumentNullException">This exception is thrown if either
/// <paramref name="left"/> or <paramref name="right"/> is <c>null</c></exception>
public static BigFloat Multiply(BigFloat left, BigFloat right)
{
if (BigFloat.Equals(left, null))
throw new ArgumentNullException(nameof(left));
if (BigFloat.Equals(right, null))
throw new ArgumentNullException(nameof(right));
BigInteger newNumerator = BigInteger.Multiply(left.Numerator, right.Numerator);
BigInteger newDenominator = BigInteger.Multiply(left.Denominator, right.Denominator);
return new BigFloat(newNumerator, newDenominator);
}
/// <summary>
/// Divides two <see cref="BigFloat"/> numbers and returns the result
/// </summary>
/// <param name="left">The <see cref="BigFloat"/> that will be divided by <paramref name="right"/></param>
/// <param name="right"><paramref name="left"/> will be diveded by this <see cref="BigFloat"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the division of
/// <paramref name="left"/> by <paramref name="right"/></returns>
/// <exception cref="ArgumentNullException">This exception is thrown if either
/// <paramref name="left"/> or <paramref name="right"/> is <c>null</c></exception>
/// <exception cref="System.DivideByZeroException">This excdeption is thrown if
/// <paramref name="right"/> evalutes to zero (if the numerator of
/// <paramref name="right"/> equals zero)</exception>
public static BigFloat Divide(BigFloat left, BigFloat right)
{
if (BigInteger.Equals(left, null))
throw new ArgumentNullException(nameof(left));
if (BigInteger.Equals(right, null))
throw new ArgumentNullException(nameof(right));
if (right.Numerator.Equals(BigInteger.Zero))
throw new System.DivideByZeroException(nameof(right));
BigInteger newNumerator = BigInteger.Multiply(left.Numerator, right.Denominator);
BigInteger newDenominator = BigInteger.Multiply(left.Denominator, right.Numerator);
return new BigFloat(newNumerator, newDenominator);
}
/// <summary>
/// Calculates the remainder of the division of two <see cref="BigFloat"/> numbers
/// </summary>
/// <param name="left">The <see cref="BigFloat"/> that will be divided by <paramref name="right"/></param>
/// <param name="right"><paramref name="left"/> will be diveded by this <see cref="BigFloat"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is the remainder of the division of
/// <paramref name="left"/> by <paramref name="right"/></returns>
/// <exception cref="ArgumentNullException">This exception is thrown if either
/// <paramref name="left"/> or <paramref name="right"/> is <c>null</c></exception>
public static BigFloat Remainder(BigFloat left, BigFloat right)
{
if (BigFloat.Equals(left, null))
throw new ArgumentNullException(nameof(left));
if (BigFloat.Equals(right, null))
throw new ArgumentNullException(nameof(right));
BigFloat result = BigFloat.Subtract(left, BigFloat.Floor(BigFloat.Multiply(BigFloat.Divide(left, right), right)));
return new BigFloat(result.Numerator, result.Denominator);
}
/// <summary>
/// Gets the arctangent (inverse tangent function) of the <see cref="BigFloat"/> number.
/// </summary>
/// <returns>Returns the arctangent of this <see cref="BigFloat"/></returns>
public static BigFloat Atan(BigFloat value)
{
if (value == BigFloat.Zero) return BigFloat.Zero;
BigFloat result = BigFloat.Zero;
BigFloat term = value; // First term in the series
BigFloat xSquared = BigFloat.Pow(value, 2);
bool subtract = true;
int k = 1;
while (BigFloat.Abs(value) > BigFloat.GetEpsilon())
{
result += term;
// Calculate next term in the series
k += 2;
term *= xSquared;
term /= new BigFloat(k);
if (subtract)
term = -term;
subtract = !subtract;
}
return result;
}
/// <summary>
/// Raises a <see cref="BigFloat"/> number to the power of an integer exponent
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> that will be raised to the power of <paramref name="exponent"/></param>
/// <param name="exponent"><paramref name="value"/> will be raised to the power of this <see cref="int"/></param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> raised to the
/// power of <paramref name="exponent"/>A new <see cref="BigFloat"/> instance that is
/// <paramref name="value"/> raised to the power of <paramref name="exponent"/></returns>
public static BigFloat Pow(BigFloat value, int exponent)
{
BigInteger newNumerator = value.Numerator;
BigInteger newDenominator = value.Denominator;
if (value.Numerator.IsZero)
{
// Nothing to do
}
else if (exponent < 0)
{
newNumerator = BigInteger.Pow(value.Denominator, -exponent);
newDenominator = BigInteger.Pow(value.Numerator, -exponent);
}
else
{
newNumerator = BigInteger.Pow(value.Numerator, exponent);
newDenominator = BigInteger.Pow(value.Denominator, exponent);
}
return new BigFloat(newNumerator, newDenominator);
}
/// <summary>
/// Calculates the absolute value of a <see cref="BigFloat"/> number
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> of which the absolute value will be calculated</param>
/// <returns>A new <see cref="BigFloat"/> instance that is the absolute value of <paramref name="value"/></returns>
public static BigFloat Abs(BigFloat value)
{
BigInteger newNumerator = BigInteger.Abs(value.Numerator);
return new BigFloat(newNumerator, value.Denominator);
}
/// <summary>
/// Returns the negation of a <see cref="BigFloat"/> number
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> that will be negated</param>
/// <returns>A new <see cref="BigFloat"/> that is the negative of <paramref name="value"/></returns>
public static BigFloat Negate(BigFloat value)
{
return new BigFloat(BigInteger.Negate(value.Numerator), value.Denominator);
}
/// <summary>
/// Returns the bitwise inversion of a <see cref="BigFloat"/> number, performing a bitwise inversion of
/// <paramref name="value"/>, flipping all the bits, also called a <c>bitwise complement</c> or <c>bitwise not</c>
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> subject to the bitwise inversion</param>
/// <returns>A new <see cref="BigFloat"/> instance that is the bitwise inversion of <paramref name="value"/></returns>
public static BigFloat InvertBits(BigFloat value)
{
return new BigFloat(~value.Numerator, ~value.Denominator);
}
/// <summary>
/// Calculates the reciprocal of a <see cref="BigFloat"/> number, the numerator and denominator are swapped
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static BigFloat Reciprocal(BigFloat value)
{
return new BigFloat(value.Denominator, value.Numerator);
}
/// <summary>
/// Increments a <see cref="BigFloat"/> number by 1
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be incremented by 1</param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> plus <see cref="BigFloat.One"/></returns>
public static BigFloat Increment(BigFloat value)
{
return BigFloat.Add(value, BigFloat.One);
}
/// <summary>
/// Decrements a <see cref="BigFloat"/> number by 1
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be decremented by 1</param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> minus <see cref="BigFloat.One"/></returns>
public static BigFloat Decrement(BigFloat value)
{
return BigFloat.Subtract(value, BigFloat.One);
}
/// <summary>
/// Reduces the <see cref="BigFloat"/> using <see cref="BigInteger.GreatestCommonDivisor(BigInteger, BigInteger)"/>
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be reduced</param>
/// <returns>A new <see cref="BigFloat"/> instance that is the reduced version of <paramref name="value"/></returns>
public static BigFloat Reduce(BigFloat value)
{
if (BigInteger.Equals(value.Denominator, BigInteger.One))
return new BigFloat(value);
// Reduce numerator and denominator
// System.Numerics.BigInteger uses the Euclidean Algirithm to find the GCD
BigInteger factor = BigInteger.GreatestCommonDivisor(value.Numerator, value.Denominator);
return new BigFloat(BigInteger.Divide(value.Numerator, factor), BigInteger.Divide(value.Denominator, factor));
}
/// <summary>
/// Calculates the smallest integral value greater than or equal to the specified <see cref="BigFloat"/> value
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> for which the ceiling value will be calculated</param>
/// <returns>A new <see cref="BigFloat"/> instance that is the smallest integral value greater than
/// or equal to <paramref name="value"/></returns>
public static BigFloat Ceiling(BigFloat value)
{
BigInteger newNumerator = value.Numerator;
if (value.Numerator < BigInteger.Zero)
newNumerator = BigInteger.Subtract(newNumerator, BigInteger.Remainder(value.Numerator, value.Denominator));
else
newNumerator = BigInteger.Add(newNumerator, BigInteger.Subtract(value.Denominator, BigInteger.Remainder(value.Numerator, value.Denominator)));
return BigFloat.Reduce(new BigFloat(newNumerator, value.Denominator));
}
/// <summary>
/// Calculates the largest integral value less than or equal to the specified <see cref="BigFloat"/> value
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> for which the floor value will be calculated</param>
/// <returns>A new <see cref="BigFloat"/> instance that is the largest integral value smaller than
/// or equal to <paramref name="value"/></returns>
public static BigFloat Floor(BigFloat value)
{
BigInteger newNumerator = value.Numerator;
if (value.Numerator < BigInteger.Zero)
newNumerator = BigInteger.Add(newNumerator, BigInteger.Subtract(value.Denominator, BigInteger.Remainder(value.Numerator, value.Denominator)));
else
newNumerator = BigInteger.Subtract(newNumerator, BigInteger.Remainder(value.Numerator, value.Denominator));
return BigFloat.Reduce(new BigFloat(newNumerator, value.Denominator));
}
/// <summary>
/// Rounds <paramref name="value"/> to the nearest integer where 0.5 rounds up
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be rounded</param>
/// <returns>A new <see cref="BigFloat"/> instance that is the nearest integer to
/// <paramref name="value"/> with 0.5 rounding up</returns>
/// <example>3.5 (7/2) will round to 4 (4/1)</example>
/// <example>-3.5 (-7/2) will round to -3 (-3/1)</example>
public static BigFloat Round(BigFloat value)
{
BigFloat remainder = BigFloat.Decimals(value);
if (remainder.CompareTo(OneHalf) >= 0)
return BigFloat.Ceiling(value);
else
return BigFloat.Floor(value);
}
// TODO Add other rounding methods
/// <summary>
/// Truncates <paramref name="value"/> to the nearest smaller integer
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be truncated</param>
/// <returns>A new <see cref="BigFloat"/> instance that is the truncated value of <paramref name="value"/></returns>
/// <remarks>This overload is more efficient then <see cref="BigFloat.Truncate(BigFloat, bool)"/> but the
/// fractional representation is not preserved, the <see cref="BigFloat.Denominator"/> will
/// always be 1 (one)</remarks>
public static BigFloat Truncate(BigFloat value)
{
return new BigFloat(BigInteger.Divide(value.Numerator, value.Denominator), BigInteger.One);
}
/// <summary>
/// Truncates <paramref name="value"/> to the nearest smaller integer with the option of maintaining
/// the fractional representation
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be truncated</param>
/// <param name="KeepDenominator">A boolean flag indicating if the fractional representation should
/// be maintained</param>
/// <returns>A new <see cref="BigFloat"/> instance that is the truncated value of <paramref name="value"/></returns>
/// <remarks>If <paramref name="KeepDenominator"/> is set to <c>true</c> then this overload is less
/// efficient than <see cref="BigFloat.Truncate(BigFloat)"/></remarks>
public static BigFloat Truncate(BigFloat value, bool KeepDenominator)
{
if (KeepDenominator)
{
BigInteger newNumerator = value.Numerator;
newNumerator = BigInteger.Subtract(newNumerator, BigInteger.Remainder(value.Numerator, value.Denominator));
return new BigFloat(newNumerator, value.Denominator);
}
else
{
return BigFloat.Truncate(value);
}
}
/// <summary>
/// Calculates the decimal part of a <see cref="BigFloat"/> number
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> for which the decimal part is required</param>
/// <returns>A new <see cref="BigFloat"/> instance that is the decimal part only of <paramref name="value"/></returns>
public static BigFloat Decimals(BigFloat value)
{
BigInteger result = BigInteger.Remainder(value.Numerator, value.Denominator);
return new BigFloat(result, value.Denominator);
}
/// <summary>
/// Shifts the decimal point of a <see cref="BigFloat"/> number to the left by <paramref name="shift"/>
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be shifted left</param>
/// <param name="shift">The number of places to ShiftLeft</param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> left shifted by
/// <paramref name="shift"/> places</returns>
public static BigFloat ShiftDecimalLeft(BigFloat value, int shift)
{
BigInteger newNumerator = value.Numerator;
if (shift < 0)
return ShiftDecimalRight(value, -shift);
newNumerator = BigInteger.Multiply(newNumerator, BigInteger.Pow(10, shift));
return new BigFloat(newNumerator, value.Denominator);
}
/// <summary>
/// Shifts the decimal point of a <see cref="BigFloat"/> number to the right by <paramref name="shift"/>
/// </summary>
/// <param name="value">The <see cref="BigFloat"/> to be shifted right</param>
/// <param name="shift">The number of places to ShiftRight</param>
/// <returns>A new <see cref="BigFloat"/> instance that is <paramref name="value"/> right shifted by