-
Notifications
You must be signed in to change notification settings - Fork 2
/
BigFloat.cs
565 lines (428 loc) · 16.9 KB
/
BigFloat.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
using System.Text;
namespace System.Numerics;
[Serializable]
public readonly partial struct BigFloat : IComparable, IComparable<BigFloat>, IEquatable<BigFloat>
{
public readonly BigInteger Numerator;
public readonly BigInteger Denominator;
public static BigFloat One => new(BigInteger.One);
public static BigFloat Zero => new(BigInteger.Zero);
public static BigFloat MinusOne => new(BigInteger.MinusOne);
public static BigFloat OneHalf => new(BigInteger.One, 2);
public int Sign
=> (Numerator.Sign + Denominator.Sign) switch
{
2 or -2 => 1,
0 => -1,
_ => 0,
};
#region Constructors
public BigFloat()
=> (Numerator, Denominator) = (BigInteger.Zero, BigInteger.One);
private BigFloat(string value)
=> (Numerator, Denominator) = Parse(value);
public BigFloat(BigInteger numerator, BigInteger denominator)
{
if (denominator == 0)
throw new DivideByZeroException($"{nameof(denominator)} equals 0");
(Numerator, Denominator) = (numerator, denominator);
}
public BigFloat(BigInteger value)
=> (Numerator, Denominator) = (value, BigInteger.One);
public BigFloat(BigFloat value)
{
if (object.Equals(value, null))
(Numerator, Denominator) = (BigInteger.Zero, BigInteger.One);
else
(Numerator, Denominator) = value;
}
public BigFloat(ulong value)
: this(new BigInteger(value))
{ }
public BigFloat(long value)
: this(new BigInteger(value))
{ }
public BigFloat(uint value)
: this(new BigInteger(value))
{ }
public BigFloat(int value)
: this(new BigInteger(value))
{ }
public BigFloat(float value)
: this(value.ToString("N99"))
{ }
public BigFloat(double value)
: this(value.ToString("N99"))
{ }
public BigFloat(decimal value)
: this(value.ToString("N99"))
{ }
#endregion
#region Static Methods
public static BigFloat Add(BigFloat value, BigFloat other)
{
if (object.Equals(other, null))
throw new ArgumentNullException(nameof(other));
var numerator = value.Numerator * other.Denominator + other.Numerator * value.Denominator;
return new(numerator, value.Denominator * other.Denominator);
}
public static BigFloat Subtract(BigFloat value, BigFloat other)
{
if (object.Equals(other, null))
throw new ArgumentNullException(nameof(other));
var numerator = value.Numerator * other.Denominator - other.Numerator * value.Denominator;
return new(numerator, value.Denominator * other.Denominator);
}
public static BigFloat Multiply(BigFloat value, BigFloat other)
{
if (object.Equals(other, null))
throw new ArgumentNullException(nameof(other));
return new(value.Numerator * other.Numerator, value.Denominator * other.Denominator);
}
public static BigFloat Divide(BigFloat value, BigFloat other)
{
if (BigInteger.Equals(other, null))
throw new ArgumentNullException(nameof(other));
if (other.Numerator == 0)
throw new DivideByZeroException(nameof(other));
return new(value.Numerator * other.Denominator, value.Denominator * other.Numerator);
}
public static BigFloat Remainder(BigFloat value, BigFloat other)
{
if (BigInteger.Equals(other, null))
throw new ArgumentNullException(nameof(other));
return value - Floor(value / other) * other;
}
public static BigFloat DivideRemainder(BigFloat value, BigFloat other, out BigFloat remainder)
{
value = Divide(value, other);
remainder = Remainder(value, other);
return value;
}
public static BigFloat Pow(BigFloat value, int exponent)
{
if (value.Numerator.IsZero)
{
// Nothing to do
return value;
}
else if (exponent < 0)
{
var savedNumerator = value.Numerator;
var numerator = BigInteger.Pow(value.Denominator, -exponent);
var denominator = BigInteger.Pow(savedNumerator, -exponent);
return new(numerator, denominator);
}
else
{
var numerator = BigInteger.Pow(value.Numerator, exponent);
var denominator = BigInteger.Pow(value.Denominator, exponent);
return new(numerator, denominator);
}
}
public static BigFloat Abs(BigFloat value)
=> new(BigInteger.Abs(value.Numerator), value.Denominator);
public static BigFloat Negate(BigFloat value)
=> new(BigInteger.Negate(value.Numerator), value.Denominator);
public static BigFloat Inverse(BigFloat value)
=> new(value.Denominator, value.Numerator);
public static BigFloat Increment(BigFloat value)
=> new(value.Numerator + value.Denominator, value.Denominator);
public static BigFloat Decrement(BigFloat value)
=> new(value.Numerator - value.Denominator, value.Denominator);
public static BigFloat Ceil(BigFloat value)
{
var numerator = value.Numerator;
if (numerator < 0)
numerator -= BigInteger.Remainder(numerator, value.Denominator);
else
numerator += value.Denominator - BigInteger.Remainder(numerator, value.Denominator);
return Factor(new(numerator, value.Denominator));
}
public static BigFloat Floor(BigFloat value)
{
var numerator = value.Numerator;
if (numerator < 0)
numerator += value.Denominator - BigInteger.Remainder(numerator, value.Denominator);
else
numerator -= BigInteger.Remainder(numerator, value.Denominator);
return Factor(new(numerator, value.Denominator));
}
public static BigFloat Round(BigFloat value)
{
//get remainder. Over divisor see if it is > new BigFloat(0.5)
if (BigFloat.Decimals(value).CompareTo(OneHalf) >= 0)
return Ceil(value);
else
return Floor(value);
}
public static BigFloat Truncate(BigFloat value)
{
var numerator = value.Numerator;
numerator -= BigInteger.Remainder(numerator, value.Denominator);
return Factor(new(numerator, value.Denominator));
}
public static BigFloat Decimals(BigFloat value)
=> new(BigInteger.Remainder(value.Numerator, value.Denominator), value.Denominator);
public static BigFloat ShiftDecimalLeft(BigFloat value, int shift)
{
if (shift < 0)
return ShiftDecimalRight(value, -shift);
var numerator = value.Numerator * BigInteger.Pow(10, shift);
return new(numerator, value.Denominator);
}
public static BigFloat ShiftDecimalRight(BigFloat value, int shift)
{
if (shift < 0)
return ShiftDecimalLeft(value, -shift);
var denominator = value.Denominator * BigInteger.Pow(10, shift);
return new(value.Numerator, denominator);
}
public static BigFloat Sqrt(BigFloat value)
=> Divide(Math.Pow(10, BigInteger.Log10(value.Numerator) / 2), Math.Pow(10, BigInteger.Log10(value.Denominator) / 2));
public static double Log10(BigFloat value)
=> BigInteger.Log10(value.Numerator) - BigInteger.Log10(value.Denominator);
public static double Log(BigFloat value, double baseValue)
=> BigInteger.Log(value.Numerator, baseValue) - BigInteger.Log(value.Numerator, baseValue);
///<summary> factoring can be very slow. So use only when neccessary (ToString, and comparisons) </summary>
public static BigFloat Factor(BigFloat value)
{
if (value.Denominator == 1)
return value;
//factor numerator and denominator
var factor = BigInteger.GreatestCommonDivisor(value.Numerator, value.Denominator);
return new(value.Numerator / factor, value.Denominator / factor);
}
public new static bool Equals(object left, object right)
{
if (left == null && right == null)
return true;
if (left == null || right == null)
return false;
if (left.GetType() != right.GetType())
return false;
return ((BigInteger)left).Equals((BigInteger)right);
}
public static string ToString(BigFloat value)
=> value.ToString();
public static BigFloat Parse(string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
value = value.Trim();
var nf = Threading.Thread.CurrentThread.CurrentCulture.NumberFormat;
value = value.Replace(nf.NumberGroupSeparator, "");
var pos = value.IndexOf(nf.NumberDecimalSeparator);
value = value.Replace(nf.NumberDecimalSeparator, "");
if (pos < 0)
{
//no decimal point
return Factor(BigInteger.Parse(value));
}
else
{
//decimal point (length - pos - 1)
var numerator = BigInteger.Parse(value);
var denominator = BigInteger.Pow(10, value.Length - pos);
return Factor(new(numerator, denominator));
}
}
public static bool TryParse(string value, out BigFloat result)
{
try
{
result = BigFloat.Parse(value);
return true;
}
catch (ArgumentNullException)
{
result = default;
return false;
}
catch (FormatException)
{
result = default;
return false;
}
}
public static int Compare(BigFloat left, BigFloat right)
{
if (object.Equals(left, null))
throw new ArgumentNullException(nameof(left));
if (Equals(right, null))
throw new ArgumentNullException(nameof(right));
return new BigFloat(left).CompareTo(right);
}
#endregion
#region Instance Methods
public override string ToString()
//default precision = 100
=> ToString(100);
public string ToString(int precision, bool trailingZeros = false)
{
var value = Factor(this);
var nf = Threading.Thread.CurrentThread.CurrentCulture.NumberFormat;
var result = BigInteger.DivRem(value.Numerator, value.Denominator, out var remainder);
if (remainder == 0 && trailingZeros)
return result + nf.NumberDecimalSeparator + "0";
else if (remainder == 0)
return result.ToString();
var decimals = (value.Numerator * BigInteger.Pow(10, precision)) / value.Denominator;
if (decimals == 0 && trailingZeros)
return result + nf.NumberDecimalSeparator + "0";
else if (decimals == 0)
return result.ToString();
var sb = new StringBuilder();
while (precision-- > 0)
{
sb.Append(decimals % 10);
decimals /= 10;
}
var r = result + nf.NumberDecimalSeparator + new string(sb.ToString().Reverse().ToArray());
return trailingZeros ? r : r.TrimEnd('0');
}
public string ToMixString()
{
var value = Factor(this);
var result = BigInteger.DivRem(value.Numerator, value.Denominator, out var remainder);
if (remainder == 0)
return result.ToString();
else
return result + ", " + remainder + "/" + value.Denominator;
}
public string ToRationalString()
{
var value = Factor(this);
return value.Numerator + " / " + value.Denominator;
}
public int CompareTo(BigFloat other)
{
if (object.Equals(other, null))
throw new ArgumentNullException(nameof(other));
//Make copies
var one = Numerator;
var two = other.Numerator;
//cross multiply
one *= other.Denominator;
two *= Denominator;
//test
return BigInteger.Compare(one, two);
}
public int CompareTo(object obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
if (obj is not BigFloat bf)
throw new ArgumentException($"{nameof(obj)} is not a {nameof(BigFloat)}");
return CompareTo(bf);
}
public override bool Equals(object obj)
{
if (obj is not BigFloat bf)
return false;
return Numerator == bf.Numerator && Denominator == bf.Denominator;
}
public bool Equals(BigFloat other)
=> other.Numerator * Denominator == Numerator * other.Denominator;
public override int GetHashCode()
=> (Numerator, Denominator).GetHashCode();
private void Deconstruct(out BigInteger numerator, out BigInteger denominator)
=> (numerator, denominator) = (Numerator, Denominator);
#endregion
#region Operators
public static BigFloat operator -(BigFloat value)
=> Negate(value);
public static BigFloat operator -(BigFloat left, BigFloat right)
=> Subtract(left, right);
public static BigFloat operator --(BigFloat value)
=> Decrement(value);
public static BigFloat operator +(BigFloat left, BigFloat right)
=> Add(left, right);
public static BigFloat operator +(BigFloat value)
=> Abs(value);
public static BigFloat operator ++(BigFloat value)
=> Increment(value);
public static BigFloat operator %(BigFloat left, BigFloat right)
=> Remainder(left, right);
public static BigFloat operator *(BigFloat left, BigFloat right)
=> Multiply(left, right);
public static BigFloat operator /(BigFloat left, BigFloat right)
=> Divide(left, right);
public static BigFloat operator >>(BigFloat value, int shift)
=> ShiftDecimalRight(value, shift);
public static BigFloat operator <<(BigFloat value, int shift)
=> ShiftDecimalLeft(value, shift);
public static BigFloat operator ^(BigFloat left, int right)
=> Pow(left, right);
public static BigFloat operator ~(BigFloat value)
=> Inverse(value);
public static bool operator !=(BigFloat left, BigFloat right)
=> Compare(left, right) != 0;
public static bool operator ==(BigFloat left, BigFloat right)
=> Compare(left, right) == 0;
public static bool operator <(BigFloat left, BigFloat right)
=> Compare(left, right) < 0;
public static bool operator <=(BigFloat left, BigFloat right)
=> Compare(left, right) <= 0;
public static bool operator >(BigFloat left, BigFloat right)
=> Compare(left, right) > 0;
public static bool operator >=(BigFloat left, BigFloat right)
=> Compare(left, right) >= 0;
public static bool operator true(BigFloat value)
=> value != 0;
public static bool operator false(BigFloat value)
=> value == 0;
#endregion
#region Casts
public static explicit operator decimal(BigFloat value)
{
if (decimal.MinValue > value)
throw new OverflowException($"{nameof(value)} is less than decimal.MinValue.");
if (decimal.MaxValue < value)
throw new OverflowException($"{nameof(value)} is greater than decimal.MaxValue.");
return (decimal)value.Numerator / (decimal)value.Denominator;
}
public static explicit operator double(BigFloat value)
{
if (double.MinValue > value)
throw new OverflowException($"{nameof(value)} is less than double.MinValue.");
if (double.MaxValue < value)
throw new OverflowException($"{nameof(value)} is greater than double.MaxValue.");
return (double)value.Numerator / (double)value.Denominator;
}
public static explicit operator float(BigFloat value)
{
if (float.MinValue > value)
throw new OverflowException($"{nameof(value)} is less than float.MinValue.");
if (float.MaxValue < value)
throw new OverflowException($"{nameof(value)} is greater than float.MaxValue.");
return (float)value.Numerator / (float)value.Denominator;
}
public static implicit operator BigFloat(byte value)
=> new((uint)value);
public static implicit operator BigFloat(sbyte value)
=> new(value);
public static implicit operator BigFloat(short value)
=> new(value);
public static implicit operator BigFloat(ushort value)
=> new((uint)value);
public static implicit operator BigFloat(int value)
=> new(value);
public static implicit operator BigFloat(long value)
=> new(value);
public static implicit operator BigFloat(uint value)
=> new(value);
public static implicit operator BigFloat(ulong value)
=> new(value);
public static implicit operator BigFloat(decimal value)
=> new(value);
public static implicit operator BigFloat(double value)
=> new(value);
public static implicit operator BigFloat(float value)
=> new(value);
public static implicit operator BigFloat(BigInteger value)
=> new(value);
public static explicit operator BigFloat(string value)
=> new(value);
#endregion
}