-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigDouble.java
More file actions
949 lines (779 loc) · 28.2 KB
/
BigDouble.java
File metadata and controls
949 lines (779 loc) · 28.2 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
import java.io.Serializable;
public final class BigDouble implements Comparable<BigDouble>, Serializable
{
private double mantissa;
private long exponent;
private static final BigDouble Zero = new BigDouble(0.0, 0L);
private static final BigDouble NaN = new BigDouble(Double.NaN, Long.MIN_VALUE);
private static final double Tolerance = 1e-18;
//for example: if two exponents are more than 17 apart, consider adding them together pointless, just return the larger one
private static final int MaxSignificantDigits = 17;
private static final long ExpLimit = Long.MAX_VALUE;
//the largest exponent that can appear in a Double, though not all mantissas are valid here.
private static final long DoubleExpMax = 308;
//The smallest exponent that can appear in a Double, though not all mantissas are valid here.
private static final long DoubleExpMin = -324;
//the largest exponent that can appear in an Integer, though not all mantissas are valid here.
private static final long IntegerExpMax = 9;
public double getMantissa() {
return mantissa;
}
public long getExponent() {
return exponent;
}
public BigDouble() {
mantissa = Zero.mantissa;
exponent = Zero.exponent;
}
public BigDouble(double mantissa, long exponent)
{
this(mantissa, exponent, true);
}
public BigDouble(double mantissa, long exponent, boolean normalize) {
this.mantissa = mantissa;
this.exponent = exponent;
if (normalize) normalize();
}
public BigDouble(BigDouble other)
{
this(other, true);
}
public BigDouble(BigDouble other, boolean normalize) {
mantissa = other.mantissa;
exponent = other.exponent;
if (normalize) normalize();
}
public BigDouble(double value) {
//SAFETY: Handle Infinity and NaN in a somewhat meaningful way.
if (Double.isNaN(value))
{
mantissa = NaN.mantissa;
exponent = NaN.exponent;
}
else if (Double.isInfinite(value))
{
mantissa = value;
exponent = 0L;
}
else if (IsZero(value))
{
mantissa = 0.0;
exponent = 0L;
}
else
{
mantissa = value;
exponent = 0L;
normalize();
}
}
public BigDouble(String value)
{
this(Parse(value));
}
public static BigDouble valueOf(double value) {
return new BigDouble(value);
}
private boolean isNaN() {
return Double.isNaN(mantissa);
}
public static boolean isNaN(BigDouble value) {
return value.isNaN();
}
private boolean isPositiveInfinity() {
return mantissa == Double.POSITIVE_INFINITY;
}
private static boolean IsPositiveInfinity(BigDouble value) {
return value.isPositiveInfinity();
}
private boolean isNegativeInfinity() {
return mantissa == Double.NEGATIVE_INFINITY;
}
private static boolean IsNegativeInfinity(BigDouble value) {
return value.isNegativeInfinity();
}
private boolean isInfinity() {
return Double.isInfinite(mantissa);
}
private static boolean IsInfinity(BigDouble value) {
return value.isInfinity();
}
private boolean isInfinityOrNaN() {
return isInfinity() || isNaN();
}
public static boolean isBroken(BigDouble value) {
return value.isInfinityOrNaN() || value.lt(Zero);
}
private boolean isZero() {
return Math.abs(mantissa) < Double.MIN_VALUE;
}
public static boolean IsZero(double value) {
return Math.abs(value) < Double.MIN_VALUE;
}
private static boolean AreEqual(double left, double right) {
return Math.abs(left - right) < Tolerance;
}
private static boolean IsInteger(double value) {
return IsZero(Math.abs(value % 1));
}
private boolean isFinite() {
return !Double.isNaN(mantissa) && !Double.isInfinite(mantissa);
}
public static boolean isFinite(double value) {
return !Double.isNaN(value) && !Double.isInfinite(value);
}
public static BigDouble Parse(String value) {
int indexOfE = value.indexOf('e');
if (indexOfE == 0) {
if (value.charAt(1) == 'e') return Pow10(Pow10(Double.parseDouble(value.substring(2))).toDouble());
else return Pow10(Double.parseDouble(value.substring(1)));
} else if (indexOfE != -1) {
double mantissa = Double.parseDouble(value.substring(0, indexOfE));
long exponent = Long.parseLong(value.substring(indexOfE + 1).replace(",","").replace("+", ""));
return Normalize(mantissa, exponent);
}
BigDouble result = new BigDouble(Double.parseDouble(value));
if (result.isNaN() || result.isInfinity())
{
throw new IllegalArgumentException("Invalid argument: " + value);
}
return result;
}
public static BigDouble Parse(String value, BigDouble defaultIfNotParsable){
try {
return Parse(value);
} catch(Exception e){
return defaultIfNotParsable;
}
}
public double toDouble() {
if (this.isNaN())
{
return Double.NaN;
}
if (exponent > DoubleExpMax)
{
return mantissa > 0 ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
if (exponent < DoubleExpMin)
{
return 0.0;
}
//SAFETY: again, handle 5e-324, -5e-324 separately
if (exponent == DoubleExpMin)
{
return mantissa > 0 ? 5e-324 : -5e-324;
}
double result = mantissa * Lookup(exponent);
if (!isFinite(result) || exponent < 0)
{
return result;
}
double resultRounded = round(result);
if (Math.abs(resultRounded - result) < 1e-10) return resultRounded;
return result;
}
public int toInteger() {
if (this.isNaN())
{
return Integer.MIN_VALUE;
}
if (exponent > IntegerExpMax) {
return mantissa > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
} else if (exponent == IntegerExpMax) {
if (mantissa >= 2.147483647) return Integer.MAX_VALUE;
else if (mantissa <= -2.147483648) return Integer.MIN_VALUE;
} else if (exponent < -1) {
return 0;
}
return (int)Math.round(mantissa * Lookup(exponent));
}
public BigDouble abs() {
mantissa = Math.abs(mantissa);
return this;
}
public BigDouble Abs() {
BigDouble result = new BigDouble(this, false);
return result.abs();
}
public static BigDouble Abs(BigDouble value) {
return value.Abs();
}
public BigDouble negate() {
mantissa = -mantissa;
return this;
}
public BigDouble Negate() {
BigDouble result = new BigDouble(this, false);
return result.negate();
}
public double Sign() {
return Math.signum(mantissa);
}
public BigDouble round(long precision) {
if (!isNaN())
{
if (exponent < -1)
{
mantissa = 0.0;
exponent = 0L;
}
else if (exponent + precision < MaxSignificantDigits)
{
mantissa = round(mantissa * Lookup(exponent + precision)) / Lookup(exponent + precision);
}
}
return this;
}
public BigDouble round() {
return round(0);
}
public BigDouble Round(long precision) {
BigDouble result = new BigDouble(this, false);
return result.round(precision);
}
public BigDouble Round() {
return Round(0);
}
public BigDouble floor(long precision) {
if (!isNaN())
{
if (exponent < -1)
{
mantissa = Math.signum(mantissa) >= 0 ? 0.0 : -1.0;
exponent = 0L;
}
else if (exponent + precision < MaxSignificantDigits)
{
mantissa = Math.floor(mantissa * Lookup(exponent + precision)) / Lookup(exponent + precision);
}
}
return this;
}
public BigDouble floor() {
return floor(0L);
}
public BigDouble Floor(long precision) {
BigDouble result = new BigDouble(this, false);
return result.floor(precision);
}
public BigDouble Floor() {
return Floor(0L);
}
public BigDouble ceiling(long precision) {
if (!isNaN())
{
if (exponent < -1)
{
mantissa = Math.signum(mantissa) > 0 ? 1.0 : 0.0;
exponent = 0L;
}
else if (exponent + precision < MaxSignificantDigits)
{
mantissa = Math.ceil(mantissa * Lookup(exponent + precision)) / Lookup(exponent + precision);
}
}
return this;
}
public BigDouble ceiling() {
return ceiling(0L);
}
public BigDouble Ceiling(long precision) {
BigDouble result = new BigDouble(this, false);
return result.ceiling(precision);
}
public BigDouble Ceiling() {
return Ceiling(0L);
}
public static double truncate(double value) {
return value >= 0 ? Math.floor(value) : Math.ceil(value);
}
public BigDouble truncate(long precision) {
if (!isNaN())
{
if (exponent < 0)
{
mantissa = 0.0;
exponent = 0L;
}
else if (exponent + precision < MaxSignificantDigits)
{
mantissa = truncate(mantissa * Lookup(exponent + precision)) / Lookup(exponent + precision);
}
}
return this;
}
public BigDouble truncate() {
return truncate(0L);
}
public BigDouble Truncate(long precision) {
BigDouble result = new BigDouble(this, false);
return result.truncate(precision);
}
public BigDouble Truncate() {
return Truncate(0L);
}
public BigDouble add(BigDouble augend) {
//figure out which is bigger, shrink the mantissa of the smaller by the difference in exponents, add mantissas, normalize and return
//TODO: Optimizations and simplification may be possible, see https://github.com/Patashu/break_infinity.js/issues/8
if (IsZero(mantissa)) {
mantissa = augend.mantissa;
exponent = augend.exponent;
} else if (!IsZero(augend.mantissa)) {
if (isNaN() || augend.isNaN() || isInfinity() || IsInfinity(augend)) {
// Let Double handle these cases.
mantissa += augend.mantissa;
} else {
BigDouble bigger, smaller;
if (exponent >= augend.exponent) {
bigger = this;
smaller = augend;
} else {
bigger = augend;
smaller = this;
}
if (bigger.exponent - smaller.exponent > MaxSignificantDigits) {
mantissa = bigger.mantissa;
exponent = bigger.exponent;
} else {
//have to do this because adding numbers that were once integers but scaled down is imprecise.
//Example: 299 + 18
mantissa = round(1e14 * bigger.mantissa + 1e14 * smaller.mantissa *
Lookup(smaller.exponent - bigger.exponent));
exponent = bigger.exponent - 14;
normalize();
}
}
}
return this;
}
public BigDouble Add(BigDouble augend) {
BigDouble result = new BigDouble(this, false);
return result.add(augend);
}
public BigDouble subtract(BigDouble subtrahend) {
add(subtrahend.Negate());
return this;
}
public BigDouble Subtract(BigDouble subtrahend) {
BigDouble result = new BigDouble(this, false);
return result.subtract(subtrahend);
}
public BigDouble multiply(int multiplicand) {
mantissa *= multiplicand;
return normalize();
}
public BigDouble Multiply(int multiplicand) {
BigDouble result = new BigDouble(this, false);
return result.multiply(multiplicand);
}
public BigDouble multiply(double multiplicand) {
mantissa *= multiplicand;
return normalize();
}
public BigDouble Multiply(double multiplicand) {
BigDouble result = new BigDouble(this, false);
return result.multiply(multiplicand);
}
public BigDouble multiply(BigDouble multiplicand) {
// 2e3 * 4e5 = (2 * 4)e(3 + 5)
mantissa *= multiplicand.mantissa;
exponent += multiplicand.exponent;
return normalize();
}
public BigDouble Multiply(BigDouble multiplicand) {
BigDouble result = new BigDouble(this, false);
return result.multiply(multiplicand);
}
public BigDouble divide(double divisor) {
return divide(valueOf(divisor));
}
public BigDouble divide(BigDouble divisor) {
multiply(divisor.Reciprocate());
normalize();
return this;
}
public BigDouble Divide(double divisor) {
return Divide(valueOf(divisor));
}
public BigDouble Divide(BigDouble divisor) {
BigDouble result = new BigDouble(this, false);
return result.divide(divisor);
}
public BigDouble reciprocate() {
mantissa = 1.0 / mantissa;
exponent = -exponent;
normalize();
return this;
}
public BigDouble Reciprocate() {
BigDouble result = new BigDouble(this, false);
return result.reciprocate();
}
private BigDouble normalize() {
if (mantissa >= 1 && mantissa < 10 || !isFinite(mantissa)) {
return this;
}
if (IsZero(mantissa)) {
mantissa = 0.0;
exponent = 0L;
return this;
}
long tempExponent = (long)Math.floor(Math.log10(Math.abs(mantissa)));
//SAFETY: handle 5e-324, -5e-324 separately
if (tempExponent == DoubleExpMin) {
mantissa = mantissa * 10 / 1e-323;
} else {
mantissa = mantissa / Lookup(tempExponent);
}
exponent = exponent + tempExponent;
return this;
}
private static BigDouble Normalize(double mantissa, long exponent) {
BigDouble result = new BigDouble(mantissa, exponent);
return result.normalize();
}
public String toString() {
return Double.toString(mantissa) + 'e' + exponent;
}
public String toStringPlusMinus() {
return Double.toString(mantissa) + 'e' + (exponent > 0 ? '+' : "") + exponent;
}
private int CompareTo(Object other) {
if (other == null) {
return 1;
}
if (!(other instanceof BigDouble)) {
throw new IllegalArgumentException("The parameter must be a BigDouble.");
}
return CompareTo(other);
}
@Override
public int compareTo(BigDouble other) {
if (IsZero(mantissa) || IsZero(other.mantissa)
|| isNaN() || other.isNaN()
|| IsInfinity(this) || IsInfinity(other))
{
// Let Double handle these cases.
return Double.compare(mantissa, other.mantissa);
}
if (mantissa > 0 && other.mantissa < 0) {
return 1;
}
if (mantissa < 0 && other.mantissa > 0) {
return -1;
}
int exponentComparison = Long.compare(exponent, other.exponent);
return exponentComparison != 0
? (mantissa > 0 ? exponentComparison : -exponentComparison)
: Double.compare(mantissa, other.mantissa);
}
public boolean Equals(BigDouble other) {
return !isNaN() && !other.isNaN() &&
(exponent == other.exponent && AreEqual(mantissa, other.mantissa) || AreSameInfinity(this, other));
}
/// <summary>
/// Relative comparison with tolerance being adjusted with greatest exponent.
/// <para>
/// For example, if you put in 1e-9, then any number closer to the larger number
/// than (larger number) * 1e-9 will be considered equal.
/// </para>
/// </summary>
public boolean Equals(BigDouble other, double tolerance) {
return !isNaN() && !other.isNaN() && (AreSameInfinity(this, other)
|| Abs(this.Subtract(other)).lte(Max(Abs(), Abs(other)).Multiply(new BigDouble(tolerance))));
}
private static boolean AreSameInfinity(BigDouble first, BigDouble second) {
return IsPositiveInfinity(first) && IsPositiveInfinity(second)
|| IsNegativeInfinity(first) && IsNegativeInfinity(second);
}
public boolean eq(BigDouble other) {
return Equals(other);
}
public static boolean eq(BigDouble left, BigDouble right) {
return left.Equals(right);
}
public boolean lt(BigDouble other) {
if (isNaN() || other.isNaN()) return false;
if (IsZero(mantissa)) return other.mantissa > 0;
if (IsZero(other.mantissa)) return mantissa < 0;
if (exponent == other.exponent) return mantissa < other.mantissa;
if (mantissa > 0) return other.mantissa > 0 && exponent < other.exponent;
return other.mantissa > 0 || exponent > other.exponent;
}
public static boolean lt(BigDouble left, BigDouble right) {
return left.lt(right);
}
public boolean lte(BigDouble other) {
if (isNaN() || other.isNaN()) return false;
return !gt(other);
}
public static boolean lte(BigDouble left, BigDouble right) {
return left.lte(right);
}
public boolean gt(BigDouble other) {
if (isNaN() || other.isNaN()) return false;
if (IsZero(mantissa)) return other.mantissa < 0;
if (IsZero(other.mantissa)) return mantissa > 0;
if (exponent == other.exponent) return mantissa > other.mantissa;
if (mantissa > 0) return other.mantissa < 0 || exponent > other.exponent;
return other.mantissa < 0 && exponent < other.exponent;
}
public static boolean gt(BigDouble left, BigDouble right) {
return left.gt(right);
}
public boolean gte(BigDouble other) {
if (isNaN() || other.isNaN()) return false;
return !lt(other);
}
public static boolean gte(BigDouble left, BigDouble right) {
return left.gte(right);
}
private BigDouble maxInPlace(BigDouble other) {
if (lt(other) || isNaN()) {
mantissa = other.getMantissa();
exponent = other.getExponent();
}
return this;
}
public BigDouble max(BigDouble other) {
if (isNaN() || other.isNaN()) return NaN;
return gt(other) ? this.copy() : other.copy();
}
public static BigDouble Max(BigDouble left, BigDouble right) {
return left.max(right);
}
private BigDouble minInPlace(BigDouble other) {
if (gt(other) || isNaN()) {
mantissa = other.getMantissa();
exponent = other.getExponent();
}
return this;
}
public BigDouble min(BigDouble other) {
if (isNaN() || other.isNaN()) return NaN;
return this.gt(other) ? other.copy() : this.copy();
}
public static BigDouble Min(BigDouble left, BigDouble right) {
return left.min(right);
}
public double AbsLog10() {
return exponent + Math.log10(Math.abs(mantissa));
}
public double log10() {
return exponent + Math.log10(mantissa);
}
public static double log10(BigDouble value) {
return value.log10();
}
public double pLog10() {
return (mantissa <= 0 || exponent < 0) ? 0 : log10();
}
public static double pLog10(BigDouble value) {
return value.pLog10();
}
public double log(BigDouble base) {
return log(base.toDouble());
}
public static double log(BigDouble value, BigDouble base) {
return value.log(base.toDouble());
}
public double log(double base) {
if (IsZero(base)) {
return Double.NaN;
}
//UN-SAFETY: Most incremental game cases are Log(number := 1 or greater, base := 2 or greater). We assume this to be true and thus only need to return a number, not a BigDouble, and don't do any other kind of error checking.
return 2.30258509299404568402 / Math.log(base) * log10();
}
public static double log(BigDouble value, double base) {
return value.log(base);
}
public double log2() {
return 3.32192809488736234787 * log10();
}
public static double Log2(BigDouble value) {
return value.log2();
}
public double ln() {
return 2.30258509299404568402 * log10();
}
public static double Ln(BigDouble value) {
return value.ln();
}
public static BigDouble Pow10(double power) {
return IsInteger(power)
? Pow10((long) power)
: Normalize(Math.pow(10, power % 1), (long) (power > 0 ? Math.floor(power) : Math.ceil(power)));
}
public static BigDouble Pow10(long power) {
return new BigDouble(1.0, power);
}
public BigDouble pow(BigDouble power) {
pow(power.toDouble());
return this;
}
public BigDouble Pow(BigDouble power) {
BigDouble result = new BigDouble(this, false);
return result.pow(power);
}
public static BigDouble Pow(BigDouble value, BigDouble power) {
return value.Pow(power);
}
public BigDouble pow(double power) {
// TODO: power can be greater than long.MaxValue, which can bring troubles in fast track
boolean powerIsInteger = IsInteger(power);
if (lt(Zero) && !powerIsInteger) {
mantissa = NaN.mantissa;
exponent = NaN.exponent;
} else {
if(is10() && powerIsInteger) {
BigDouble result = Pow10(power);
mantissa = result.mantissa;
exponent = result.exponent;
} else {
powInternal(power);
}
}
return this;
}
public BigDouble Pow(double power) {
BigDouble result = new BigDouble(this, false);
return result.pow(power);
}
private boolean is10() {
return exponent == 1 && IsZero(mantissa - 1);
}
private boolean is10(BigDouble value) {
return value.is10();
}
private void powInternal(double power) {
//UN-SAFETY: Accuracy not guaranteed beyond ~9~11 decimal places.
//TODO: Fast track seems about neutral for performance. It might become faster if an integer pow is implemented, or it might not be worth doing (see https://github.com/Patashu/break_infinity.js/issues/4 )
//Fast track: If (this.exponent*power) is an integer and mantissa^power fits in a Number, we can do a very fast method.
double temp = exponent * power;
double newMantissa;
if (IsInteger(temp) && isFinite(temp) && Math.abs(temp) < ExpLimit) {
newMantissa = Math.pow(mantissa, power);
if (isFinite(newMantissa)) {
mantissa = newMantissa;
exponent = (long) temp;
normalize();
return;
}
}
//Same speed and usually more accurate. (An arbitrary-precision version of this calculation is used in break_break_infinity.js, sacrificing performance for utter accuracy.)
double newExponent = temp >= 0 ? Math.floor(temp) : Math.ceil(temp);
double residue = temp - newExponent;
newMantissa = Math.pow(10.0, power * Math.log10(mantissa) + residue);
if (isFinite(newMantissa)) {
mantissa = newMantissa;
exponent = (long) newExponent;
normalize();
} else {
//UN-SAFETY: This should return NaN when mantissa is negative and value is noninteger.
BigDouble result = Pow10(power * AbsLog10()); //this is 2x faster and gives same values AFAIK
if (Sign() == -1 && AreEqual(power % 2, 1)) {
mantissa = -result.mantissa;
} else {
mantissa = result.mantissa;
}
exponent = result.exponent;
}
}
public BigDouble exp() {
double x = toDouble(); // Fast track: if -706 < this < 709, we can use regular exp.
BigDouble result;
if (-706 < x && x < 709) {
result = new BigDouble(Math.exp(x));
} else {
result = valueOf(Math.E).Pow(x);
}
mantissa = result.mantissa;
exponent = result.exponent;
return this;
}
public BigDouble Exp() {
return copy().exp();
}
public static BigDouble exp(double value) {
return valueOf(value).exp();
}
public BigDouble sqrt() {
if (mantissa < 0) {
mantissa = NaN.mantissa;
exponent = NaN.exponent;
} else {
if (exponent % 2 != 0) {
// mod of a negative number is negative, so != means '1 or -1'
mantissa = Math.sqrt(mantissa) * 3.16227766016838;
exponent = (long) Math.floor(exponent / 2.0);
} else {
mantissa = Math.sqrt(mantissa);
exponent = (long) Math.floor(exponent / 2.0);
}
normalize();
}
return this;
}
public BigDouble Sqrt() {
return copy().sqrt();
}
public BigDouble copy() {
return new BigDouble(this, false);
}
public BigDouble clamp(BigDouble min, BigDouble max) {
return this.max(min).min(max);
}
public BigDouble clampMin(BigDouble min) {
return maxInPlace(min);
}
public BigDouble ClampMin(BigDouble min) {
return this.max(min);
}
public BigDouble clampMax(BigDouble max) {
return minInPlace(max);
}
public BigDouble ClampMax(BigDouble max) {
return this.min(max);
}
public BigDouble clampMaxExponent(long maxExp) {
if (exponent >= maxExp) {
exponent = maxExp;
}
return this;
}
public BigDouble ClampMaxExponent(long maxExp) {
BigDouble result = new BigDouble(this, false);
return result.clampMaxExponent(maxExp);
}
public boolean isExtremelySmall() {
return exponent < -9000000000000000000L;
}
public static double round(double value) {
if (value > -9.223372036854776e18 && value < 9.223372036854776e18) {
return Math.round(value);
} else {
return value;
}
}
/// <summary>
/// We need this lookup table because Math.pow(10, exponent) when exponent's absolute value
/// is large is slightly inaccurate. you can fix it with the power of math... or just make
/// a lookup table. Faster AND simpler.
/// </summary>
private static double[] Powers;
private static final int IndexOf0 = (int)(-DoubleExpMin - 1);
static {
Powers = new double[(int)(DoubleExpMax - DoubleExpMin)];
int index = 0;
for (int i = 0; i < Powers.length; i++) {
Powers[index++] = Double.parseDouble("1e" + (i - IndexOf0));
}
}
private static double Lookup(int power) {
return Powers[IndexOf0 + power];
}
private static double Lookup(long power) {
return Powers[IndexOf0 + (int)power];
}
}