-
Notifications
You must be signed in to change notification settings - Fork 310
/
DD.cs
1218 lines (1099 loc) · 43.9 KB
/
DD.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Text;
namespace NetTopologySuite.Mathematics
{
/// <summary>
/// Implements extended-precision floating-point numbers
/// which maintain 106 bits (approximately 30 decimal digits) of precision.
/// <para/>
/// A DoubleDouble uses a representation containing two double-precision values.
/// A number x is represented as a pair of doubles, x.hi and x.lo,
/// such that the number represented by x is x.hi + x.lo, where
/// <pre>
/// |x.lo| <= 0.5*ulp(x.hi)
/// </pre>
/// and ulp(y) means "unit in the last place of y".
/// The basic arithmetic operations are implemented using
/// convenient properties of IEEE-754 floating-point arithmetic.
/// <para/>
/// The range of values which can be represented is the same as in IEEE-754.
/// The precision of the representable numbers
/// is twice as great as IEEE-754 double precision.
/// <para/>
/// The correctness of the arithmetic algorithms relies on operations
/// being performed with standard IEEE-754 double precision and rounding.
/// This is the Java standard arithmetic model, but for performance reasons
/// Java implementations are not
/// constrained to using this standard by default.
/// Some processors (notably the Intel Pentium architecture) perform
/// floating point operations in (non-IEEE-754-standard) extended-precision.
/// A JVM implementation may choose to use the non-standard extended-precision
/// as its default arithmetic mode.
/// To prevent this from happening, this code uses the
/// Java <tt>strictfp</tt> modifier,
/// which forces all operations to take place in the standard IEEE-754 rounding model.
/// <para/>
/// The API provides both a set of value-oriented operations
/// and a set of mutating operations.
/// Value-oriented operations treat DoubleDouble values as
/// immutable; operations on them return new objects carrying the result
/// of the operation. This provides a simple and safe semantics for
/// writing DoubleDouble expressions. However, there is a performance
/// penalty for the object allocations required.
/// The mutable interface updates object values in-place.
/// It provides optimum memory performance, but requires
/// care to ensure that aliasing errors are not created
/// and constant values are not changed.
/// <para/>
/// This implementation uses algorithms originally designed variously by
/// Knuth, Kahan, Dekker, and Linnainmaa.
/// Douglas Priest developed the first C implementation of these techniques.
/// Other more recent C++ implementation are due to Keith M. Briggs and David Bailey et al.
/// <h3>References</h3>
/// <list type="bullet">
/// <item><description>Priest, D., <i>Algorithms for Arbitrary Precision Floating Point Arithmetic</i>,
/// in P. Kornerup and D. Matula, Eds., Proc. 10th Symposium on Computer Arithmetic,
/// IEEE Computer Society Press, Los Alamitos, Calif., 1991.</description></item>
/// <item><description>Yozo Hida, Xiaoye S. Li and David H. Bailey,
/// <i>Quad-Double Arithmetic: Algorithms, Implementation, and Application</i>,
/// manuscript, Oct 2000; Lawrence Berkeley National Laboratory Report BNL-46996.</description></item>
/// <item><description>David Bailey, <i>High Precision Software Directory</i>;
/// <tt>http://crd.lbl.gov/~dhbailey/mpdist/index.html</tt></description></item>
/// </list>
/// </summary>
/// <author>Martin Davis</author>
[Serializable]
public struct DD : IComparable, IComparable<DD> /*, IFormattable*/
{
/// <summary>The value nearest to the constant Pi.</summary>
public static readonly DD PI = new DD(
3.141592653589793116e+00,
1.224646799147353207e-16);
/// <summary>The value nearest to the constant 2 * Pi.</summary>
public static readonly DD TwoPi = new DD(
6.283185307179586232e+00,
2.449293598294706414e-16);
/// <summary>The value nearest to the constant Pi / 2.</summary>
public static readonly DD PiHalf = new DD(
1.570796326794896558e+00,
6.123233995736766036e-17);
/// <summary>
/// The value nearest to the constant e (the natural logarithm base).
/// </summary>
public static readonly DD E = new DD(
2.718281828459045091e+00,
1.445646891729250158e-16);
/// <summary>
/// A value representing the result of an operation which does not return a valid number.
/// </summary>
public static readonly DD NaN = new DD(double.NaN, double.NaN);
/// <summary>
/// The smallest representable relative difference between two <see cref="DD"/> values
/// </summary>
public static readonly double Epsilon = 1.23259516440783e-32; /* = 2^-106 */
private static DD CreateNaN()
{
return new DD(double.NaN, double.NaN);
}
/// <summary>
/// Converts the string argument to a DoubleDouble number.
/// </summary>
/// <param name="str">A string containing a representation of a numeric value</param>
/// <returns>The extended precision version of the value</returns>
/// <exception cref="FormatException">Thrown if <paramref name="str"/> is not a valid representation of a number</exception>
public static DD ValueOf(string str)
{
return Parse(str);
}
/// <summary>
/// Operator to parse a <tt>DoubleDouble</tt> from a string
/// </summary>
/// <param name="val">The DoubleDouble string</param>
public static explicit operator DD (string val)
{
return Parse(val);
}
/// <summary>
/// Converts the <tt>double</tt> argument to a <tt>DoubleDouble</tt> number.
/// </summary>
/// <param name="x">A numeric value</param>
/// <returns>The extended precision version of the value</returns>
public static DD ValueOf(double x)
{
return new DD(x);
}
/// <summary>
/// Operator to convert the <tt>double</tt> value to a <tt>DoubleDouble</tt> value.
/// </summary>
/// <param name="val">The DoubleDouble string</param>
public static implicit operator DD(double val)
{
return new DD(val);
}
/// <summary>
/// The value to split a double-precision value on during multiplication
/// </summary>
private const double Split = 134217729.0D; // 2^27+1, for IEEE double
/// <summary>
/// The high-order component of the double-double precision value.
/// </summary>
private readonly double _hi;
/// <summary>
/// The low-order component of the double-double precision value.
/// </summary>
private readonly double _lo;
/// <summary>
/// Creates a new <see cref="DD"/> with value x.
/// </summary>
/// <param name="x">The initial value</param>
public DD(double x)
:this(x, 0d)
{
}
/// <summary>
/// Creates a new <see cref="DD"/> with value (hi, lo).
/// </summary>
/// <param name="hi">The high order component</param>
/// <param name="lo">The low order component</param>
public DD(double hi, double lo)
{
_hi = hi;
_lo = lo;
}
/// <summary>
/// Creates a <see cref="DD"/> with a value equal to the argument
/// </summary>
/// <param name="dd">The initial value</param>
public DD(DD dd)
{
_hi = dd._hi;
_lo = dd._lo;
}
/// <summary>
/// Creates a new <see cref="DD"/> with value equal to the argument.
/// </summary>
/// <param name="str">The value to initialize by</param>
/// <exception cref="FormatException"> if <paramref name="str"/> is not a valid representation of a number</exception>
public DD(string str)
: this(Parse(str))
{
}
/// <summary>
/// Creates a new <see cref="DD"/> with the value of the argument.
/// </summary>
/// <param name="dd">The value to copy</param>
/// <returns>A copy of <paramref name="dd"/></returns>
public static DD Copy(DD dd)
{
return new DD(dd);
}
/// <summary>
/// Creates and returns a copy of this value.
/// </summary>
/// <returns>A copy of this value</returns>
public object Clone()
{
return new DD(_hi, _lo);
}
/// <summary>
/// Returns the sum of <paramref name="lhs"/> and <paramref name="rhs"/>.
/// </summary>
/// <param name="lhs">The left hand side</param>
/// <param name="rhs">The right hand side</param>
/// <returns>The sum of <paramref name="lhs"/> and <paramref name="rhs"/></returns>
public static DD operator +(DD lhs, DD rhs)
{
double H, h, T, t, S, s, e, f;
S = lhs._hi + rhs._hi;
T = lhs._lo + rhs._lo;
e = S - lhs._hi;
f = T - lhs._lo;
s = S - e;
t = T - f;
s = (rhs._hi - e) + (lhs._hi - s);
t = (rhs._lo - f) + (lhs._lo - t);
e = s + T;
H = S + e;
h = e + (S - H);
e = t + h;
double zhi = H + e;
return new DD(zhi, e + (H - zhi));
}
/// <summary>
/// Returns the sum of <paramref name="lhs"/> and <paramref name="rhs"/>.
/// </summary>
/// <param name="lhs">The left hand side</param>
/// <param name="rhs">The right hand side</param>
/// <returns>The sum of <paramref name="lhs"/> and <paramref name="rhs"/></returns>
public static DD operator +(DD lhs, double rhs)
{
return lhs + new DD(rhs, 0);
}
/// <summary>
/// Returns the difference of <paramref name="lhs"/> and <paramref name="rhs"/>.
/// </summary>
/// <param name="lhs">The left hand side</param>
/// <param name="rhs">The right hand side</param>
/// <returns>The difference of <paramref name="lhs"/> and <paramref name="rhs"/></returns>
public static DD operator -(DD lhs, DD rhs)
{
return lhs + (-rhs);
}
/// <summary>
/// Returns the difference of <paramref name="lhs"/> and <paramref name="rhs"/>.
/// </summary>
/// <param name="lhs">The left hand side</param>
/// <param name="rhs">The right hand side</param>
/// <returns>The difference of <paramref name="lhs"/> and <paramref name="rhs"/></returns>
public static DD operator -(DD lhs, double rhs)
{
return lhs + new DD(-rhs, 0);
}
/// <summary>
/// Subtracts the argument from the value of <tt>this</tt>.
/// </summary>
/// <param name="val">The subtrahend</param>
/// <returns>The result of this - y</returns>
public static DD operator-(DD val)
{
if (IsNaN(val)) return val;
return new DD(-val._hi, -val._lo);
}
/// <summary>
/// Multiplies <paramref name="lhs"/> by <paramref name="rhs"/>.
/// </summary>
/// <param name="lhs">A DoubleDouble value.</param>
/// <param name="rhs">A double value.</param>
/// <returns>The result of the multiplication.<para/></returns>
public static DD operator *(DD lhs, double rhs)
{
return lhs*new DD(rhs, 0d);
}
/// <summary>
/// Multiplies <paramref name="lhs"/> by <paramref name="rhs"/>.
/// </summary>
/// <param name="lhs">A DoubleDouble value.</param>
/// <param name="rhs">A DoubleDouble value.</param>
/// <returns>The result of the multiplication.<para/></returns>
public static DD operator *(DD lhs, DD rhs)
{
if (IsNaN(rhs)) return CreateNaN();
double C = Split * lhs._hi;
double hx = C - lhs._hi;
double c = Split * rhs._hi;
hx = C - hx;
double tx = lhs._hi - hx;
double hy = c - rhs._hi;
C = lhs._hi * rhs._hi;
hy = c - hy;
double ty = rhs._hi - hy;
c = ((((hx * hy - C) + hx * ty) + tx * hy) + tx * ty) + (lhs._hi * rhs._lo + lhs._lo * rhs._hi);
double zhi = C + c;
hx = C - zhi;
double zlo = c + hx;
return new DD(zhi, zlo);
}
/// <summary>
/// Divides <paramref name="lhs"/> by <paramref name="rhs"/>.
/// </summary>
/// <param name="lhs">A DoubleDouble numerator.</param>
/// <param name="rhs">A double divisor.</param>
/// <returns>The result of the division.<para/></returns>
public static DD operator /(DD lhs, double rhs)
{
return lhs/new DD(rhs, 0d);
}
/// <summary>
/// Divides <paramref name="lhs"/> by <paramref name="rhs"/>.
/// </summary>
/// <param name="lhs">A DoubleDouble numerator.</param>
/// <param name="rhs">A DoubleDouble divisor.</param>
/// <returns>The result of the division.<para/></returns>
public static DD operator /(DD lhs, DD rhs)
{
if (IsNaN(rhs)) return CreateNaN();
double hc, tc, hy, ty, C, c, U, u;
C = lhs._hi / rhs._hi;
c = Split * C;
hc = c - C;
u = Split * rhs._hi;
hc = c - hc;
tc = C - hc;
hy = u - rhs._hi;
U = C * rhs._hi;
hy = u - hy;
ty = rhs._hi - hy;
u = (((hc * hy - U) + hc * ty) + tc * hy) + tc * ty;
c = ((((lhs._hi - U) - u) + lhs._lo) - C * rhs._lo) / rhs._hi;
u = C + c;
return new DD(u, (C - u) + c);
}
/// <summary>
/// Returns a <see cref="DD"/> whose value is <c>1 / this</c>.
/// </summary>
/// <returns>The reciprocal of this value</returns>
public DD Reciprocal()
{
double hc, tc, hy, ty, C, c, U, u;
C = 1.0/_hi;
c = Split*C;
hc = c - C;
u = Split*_hi;
hc = c - hc;
tc = C - hc;
hy = u - _hi;
U = C*_hi;
hy = u - hy;
ty = _hi - hy;
u = (((hc*hy - U) + hc*ty) + tc*hy) + tc*ty;
c = ((((1.0 - U) - u)) - C*_lo)/_hi;
double zhi = C + c;
double zlo = (C - zhi) + c;
return new DD(zhi, zlo);
}
/// <summary>
/// Computes the determinant of the 2x2 matrix with the given entries.
/// </summary>
/// <param name="x1">A matrix entry</param>
/// <param name="y1">A matrix entry</param>
/// <param name="x2">A matrix entry</param>
/// <param name="y2">A matrix entry</param>
/// <returns>The determinant of the matrix of values</returns>
public static DD Determinant(double x1, double y1, double x2, double y2)
{
return Determinant(ValueOf(x1), ValueOf(y1), ValueOf(x2), ValueOf(y2));
}
/// <summary>
/// Computes the determinant of the 2x2 matrix with the given entries.
/// </summary>
/// <param name="x1">A matrix entry</param>
/// <param name="y1">A matrix entry</param>
/// <param name="x2">A matrix entry</param>
/// <param name="y2">A matrix entry</param>
/// <returns>The determinant of the matrix of values</returns>
public static DD Determinant(DD x1, DD y1, DD x2, DD y2)
{
var det = x1 * y2 - y1 * x2;
return det;
}
#region Ordering Functions
/// <summary>
/// Computes the minimum of this and another DD number.
/// </summary>
/// <param name="x">A DD number</param>
/// <returns>The minimum of the two numbers</returns>
public DD Min(DD x)
{
return LessThan(x) ? this : x;
}
/// <summary>
/// Computes the maximum of this and another DD number.
/// </summary>
/// <param name="x">A DD number</param>
/// <returns>The maximum of the two numbers</returns>
public DD Max(DD x)
{
return GreaterOrEqualThan(x) ? this : x;
}
#endregion
/// <summary>
/// Returns the largest (closest to positive infinity)
/// value that is not greater than the argument
/// and is equal to a mathematical integer.
/// Special cases:
/// <list type="bullet">
/// <item><description>If this value is NaN, returns NaN.</description></item>
/// </list>
/// </summary>
/// <returns>The largest (closest to positive infinity)
/// value that is not greater than the argument
/// and is equal to a mathematical integer.
/// </returns>
public DD Floor()
{
if (IsNaN(this)) return NaN;
double fhi = Math.Floor(_hi);
double flo = 0.0;
// Hi is already integral. Floor the low word
if (fhi == _hi)
{
flo = Math.Floor(_lo);
}
// do we need to renormalize here?
return new DD(fhi, flo);
}
/// <summary>
/// Returns the smallest (closest to negative infinity) value
/// that is not less than the argument and is equal to a mathematical integer.
/// Special cases:
/// <list type="bullet">
/// <item><description>If this value is NaN, returns NaN.</description></item>
/// </list>
/// </summary>
/// <returns>
/// The smallest (closest to negative infinity) value
/// that is not less than the argument and is equal to a mathematical integer.
/// </returns>
public DD Ceiling()
{
if (IsNaN(this)) return NaN;
double fhi = Math.Ceiling(_hi);
double flo = 0.0;
// Hi is already integral. Ceil the low word
if (fhi == _hi)
{
flo = Math.Ceiling(_lo);
// do we need to renormalize here?
}
return new DD(fhi, flo);
}
/// <summary>
/// Returns an integer indicating the sign of this value.
/// <para>
/// <list type="bullet">
/// <item><description>if this value is > 0, returns 1</description></item>
/// <item><description>if this value is < 0, returns -1</description></item>
/// <item><description>if this value is = 0, returns 0</description></item>
/// <item><description>if this value is NaN, returns 0</description></item>
/// </list>
/// </para>
/// </summary>
/// <returns>An integer indicating the sign of this value</returns>
public int Signum()
{
if (_hi > 0) return 1;
if (_hi < 0) return -1;
if (_lo > 0) return 1;
if (_lo < 0) return -1;
return 0;
}
/// <summary>
/// Rounds this value to the nearest integer.
/// The value is rounded to an integer by adding 1/2 and taking the floor of the result.
/// Special cases:
/// <list type="bullet">
/// <item><description>If this value is NaN, returns NaN.</description></item>
/// </list>
/// </summary>
/// <returns>This value rounded to the nearest integer</returns>
public DD Rint()
{
if (IsNaN(this)) return this;
// may not be 100% correct
var plus5 = this + 0.5d;
return plus5.Floor();
}
/// <summary>
/// Returns the integer which is largest in absolute value and not further
/// from zero than this value.
/// <para/>
/// Special cases:
/// <list type="bullet">
/// <item><description>If this value is NaN, returns NaN.</description></item>
/// </list>
/// </summary>
/// <returns>
/// The integer which is largest in absolute value and not further from zero than this value
/// </returns>
public DD Truncate()
{
if (IsNaN(this)) return NaN;
return IsPositive() ? Floor() : Ceiling();
}
/// <summary>
/// Returns the absolute value of this value.
/// <para/>
/// Special cases:
/// <list type="bullet">
/// <item><description>if this value is NaN, it is returned.</description></item>
/// </list>
/// </summary>
/// <returns>The absolute value of this value</returns>
public DD Abs()
{
if (IsNaN(this)) return NaN;
return IsNegative ? -this : new DD(this);
}
/// <summary>
/// Computes the square of this value.
/// </summary>
/// <returns>The square of this value</returns>
public DD Sqr()
{
return this * this;
}
/// <summary>
/// Computes the square of this value.
/// </summary>
/// <returns>The square of this value.</returns>
public static DD Sqr(double x)
{
return ValueOf(x) * x;
}
/// <summary>
/// Computes the positive square root of this value.<para/>
/// If the number is NaN or negative, NaN is returned.
/// </summary>
/// <returns>If this is NaN or less than zero, the result is NaN.</returns>
public DD Sqrt()
{
/* Strategy: Use Karp's trick: if x is an approximation
to sqrt(a), then
sqrt(a) = a*x + [a - (a*x)^2] * x / 2 (approx)
The approximation is accurate to twice the accuracy of x.
Also, the multiplication (a*x) and [-]*x can be done with
only half the precision.
*/
if (IsZero)
return ValueOf(0.0);
if (IsNegative)
{
return NaN;
}
double x = 1.0/Math.Sqrt(_hi);
double ax = _hi*x;
var axdd = ValueOf(ax);
var diffSq = this - axdd.Sqr();
double d2 = diffSq._hi*(x*0.5);
return axdd + d2;
}
/// <summary>
/// Computes the positive square root of a <tt>DoubleDouble</tt> value.<para/>
/// If the number is NaN or negative, NaN is returned.
/// </summary>
/// <param name="x">A numeric value</param>
/// <returns>the positive square root of this number.<para/>
/// If the argument is NaN or less than zero, the result is NaN.</returns>
public static DD Sqrt(double x)
{
return ValueOf(x).Sqrt();
}
/// <summary>
/// Computes the value of this number raised to an integral power.
/// Follows semantics of .Net Math.Pow as closely as possible.
/// </summary>
/// <param name="exp">The integer exponent</param>
/// <returns>x raised to the integral power exp</returns>
[Pure]
public DD Pow(int exp)
{
if (exp == 0.0)
return ValueOf(1.0);
var r = new DD(this);
var s = ValueOf(1.0);
int n = Math.Abs(exp);
if (n > 1)
{
/* Use binary exponentiation */
while (n > 0)
{
if (n%2 == 1)
{
s *= r;
}
n /= 2;
if (n > 0)
r = r.Sqr();
}
}
else
{
s = r;
}
/* Compute the reciprocal if n is negative. */
if (exp < 0)
return s.Reciprocal();
return s;
}
/*------------------------------------------------------------
* Conversion Functions
*------------------------------------------------------------
*/
/// <summary>
/// Converts this value to the nearest <see cref="double"/> number.
/// </summary>
/// <returns>The nearest <see cref="double"/> value</returns>
public double ToDoubleValue()
{
return _hi + _lo;
}
/// <summary>
/// Converts this value to the nearest <see cref="int"/> value.
/// </summary>
/// <returns>The nearest <see cref="int"/> value</returns>
public int ToIntValue()
{
return (int) _hi;
}
/*------------------------------------------------------------
* Predicates
*------------------------------------------------------------
*/
/// <summary>
/// Gets a value indicating whether this object is zero (0) or not
/// </summary>
public bool IsZero => _hi == 0.0 && _lo == 0.0;
/// <summary>
/// Gets a value indicating whether this object is negative or not
/// </summary>
public bool IsNegative => _hi < 0.0 || (_hi == 0.0 && _lo < 0.0);
/// <summary>
/// Gets a value indicating whether this object is positive or not
/// </summary>
public bool IsPositive()
{
return _hi > 0.0 || (_hi == 0.0 && _lo > 0.0);
}
/// <summary>
/// Gets a value indicating whether this object is positive or not
/// </summary>
public static bool IsNaN(DD value)
{
return double.IsNaN(value._hi);
}
/// <summary>
/// Checks if <paramref name="value"/> is infinity.
/// </summary>
/// <param name="value">A DoubleDouble value</param>
/// <returns><c>true</c> if <c>value</c> is infinity.</returns>
public static bool IsInfinity(DD value)
{
return double.IsInfinity(value._hi);
}
/// <summary>
/// Tests whether this value is equal to another <tt>DoubleDouble</tt> value.
/// </summary>
/// <param name="y">A DoubleDouble value</param>
/// <returns><c>true</c> if this value == <paramref name="y"/>.</returns>
public bool Equals(DD y)
{
return y._hi.Equals(_hi) && y._lo.Equals(_lo);
}
/// <summary>
/// Equality operator for <tt>DoubleDouble</tt> values
/// </summary>
/// <param name="lhs">A DoubleDouble value</param>
/// <param name="rhs">A DoubleDouble value</param>
/// <returns><c>true</c> if <paramref name="lhs"/> == <paramref name="rhs"/>.</returns>
public static bool operator == (DD lhs, DD rhs)
{
return lhs._hi == rhs._hi && lhs._lo == rhs._lo;
}
/// <summary>
/// Inequality operator for <tt>DoubleDouble</tt> values
/// </summary>
/// <param name="lhs">A DoubleDouble value</param>
/// <param name="rhs">A DoubleDouble value</param>
/// <returns><c>true</c> if <paramref name="lhs"/> != <paramref name="rhs"/>.</returns>
public static bool operator !=(DD rhs, DD lhs)
{
return !(rhs == lhs);
}
/// <summary>
/// Tests whether this value is greater than another <tt>DoubleDouble</tt> value.
/// </summary>
/// <param name="y">A DoubleDouble value</param>
/// <returns><c>true</c> if this value > <paramref name="y"/>.</returns>
public bool GreaterThan(DD y)
{
return (_hi > y._hi) || (_hi == y._hi && _lo > y._lo);
}
/// <summary>
/// Tests whether this value is greater than or equals to another <tt>DoubleDouble</tt> value.
/// </summary>
/// <param name="y">A DoubleDouble value</param>
/// <returns><c>true</c> if this value >= <paramref name="y"/>.</returns>
public bool GreaterOrEqualThan(DD y)
{
return (_hi > y._hi) || (_hi == y._hi && _lo >= y._lo);
}
/// <summary>
/// Tests whether this value is less than another <tt>DoubleDouble</tt> value.
/// </summary>
/// <param name="y">A DoubleDouble value</param>
/// <returns><c>true</c> if this value is < <paramref name="y"/> </returns>
public bool LessThan(DD y)
{
return (_hi < y._hi) || (_hi == y._hi && _lo < y._lo);
}
/// <summary>
/// Tests whether this value is less than or equal to another <tt>DoubleDouble</tt> value.
/// </summary>
/// <param name="y">A <tt>DoubleDouble</tt></param>
/// <returns><c>true</c> if this value is <= <paramref name="y"/></returns>
public bool LessOrEqualThan(DD y)
{
return (_hi < y._hi) || (_hi == y._hi && _lo <= y._lo);
}
/// <summary>
/// Compares two <tt>DoubleDouble</tt> objects numerically.
/// </summary>
/// <param name="other">An other <tt>DoubleDouble</tt> value</param>
/// <returns>
/// <c>-1,0</c> or <c>1</c> depending on whether this value is less than, equal to
/// or greater than the value of <paramref name="other"/></returns>
public int CompareTo(DD other)
{
if (_hi < other._hi) return -1;
if (_hi > other._hi) return 1;
if (_lo < other._lo) return -1;
if (_lo > other._lo) return 1;
return 0;
}
/// <inheritdoc cref="IComparable.CompareTo"/>
public int CompareTo(object o)
{
var other = (DD) o;
if (_hi < other._hi) return -1;
if (_hi > other._hi) return 1;
if (_lo < other._lo) return -1;
if (_lo > other._lo) return 1;
return 0;
}
/*------------------------------------------------------------
* Output
*------------------------------------------------------------
*/
private const int MaxPrintDigits = 32;
private static readonly DD Ten = ValueOf(10.0);
private static readonly DD One = ValueOf(1.0);
private static readonly string SciNotExponentChar = "E";
private static readonly string SciNotZero = "0.0E0";
/// <summary>
/// Dumps the components of this number to a string.
/// </summary>
/// <returns>A string showing the components of the number</returns>
public string Dump()
{
return string.Format(NumberFormatInfo.InvariantInfo, "DD<{0}, {1}>", _hi, _lo);
}
/// <summary>
/// Returns a string representation of this number, in either standard or scientific notation.
/// If the magnitude of the number is in the range [ 10<sup>-3</sup>, 10<sup>8</sup> ]
/// standard notation will be used. Otherwise, scientific notation will be used.
/// </summary>
/// <returns>A string representation of this number</returns>
public override string ToString()
{
int mag = Magnitude(_hi);
if (mag >= -3 && mag <= 20)
return ToStandardNotation();
return ToSciNotation();
}
/// <summary>
/// Returns the string representation of this value in standard notation.
/// </summary>
/// <returns>The string representation in standard notation</returns>
public string ToStandardNotation()
{
string specialStr = GetSpecialNumberString();
if (specialStr != null)
return specialStr;
int[] magnitude = new int[1];
string sigDigits = ExtractSignificantDigits(true, magnitude);
int decimalPointPos = magnitude[0] + 1;
string num = sigDigits;
// add a leading 0 if the decimal point is the first char
if (sigDigits[0] == '.')
{
num = "0" + sigDigits;
}
else if (decimalPointPos < 0)
{
num = "0." + new string('0', -decimalPointPos) + sigDigits;
}
else if (sigDigits.IndexOf('.') == -1)
{
// no point inserted - sig digits must be smaller than magnitude of number
// add zeroes to end to make number the correct size
int numZeroes = decimalPointPos - sigDigits.Length;
string zeroes = new string('0', numZeroes);
num = sigDigits + zeroes + ".0";
}
if (IsNegative)
return "-" + num;
return num;
}
/// <summary>
/// Returns the string representation of this value in scientific notation.
/// </summary>
/// <returns>The string representation in scientific notation</returns>
public string ToSciNotation()
{
// special case zero, to allow as
if (IsZero)
return SciNotZero;
string specialStr = GetSpecialNumberString();
if (specialStr != null)
return specialStr;
int[] magnitude = new int[1];
string digits = ExtractSignificantDigits(false, magnitude);
string expStr = SciNotExponentChar + magnitude[0];
// should never have leading zeroes
// MD - is this correct? Or should we simply strip them if they are present?
if (digits[0] == '0')
{
throw new InvalidOperationException("Found leading zero: " + digits);
}
// add decimal point
string trailingDigits = "";
if (digits.Length > 1)
trailingDigits = digits.Substring(1);
string digitsWithDecimal = digits[0] + "." + trailingDigits;
if (IsNegative)
return "-" + digitsWithDecimal + expStr;
return digitsWithDecimal + expStr;
}
/// <summary>
/// Extracts the significant digits in the decimal representation of the argument.
/// A decimal point may be optionally inserted in the string of digits
/// (as long as its position lies within the extracted digits
/// - if not, the caller must prepend or append the appropriate zeroes and decimal point).
/// </summary>
/// <param name="insertDecimalPoint"></param>
/// <param name="magnitudes"></param>
/// <returns>The string containing the significant digits and possibly a decimal point</returns>
private string ExtractSignificantDigits(bool insertDecimalPoint, int[] magnitudes)
{
// The number to extract (>= 0)
var y = this.Abs();
// compute *correct* magnitude of y
int mag = Magnitude(y._hi);
var scale = Ten.Pow(mag);
y /= scale;
// fix magnitude if off by one
if (y.GreaterThan(Ten))
{
y /= Ten;
mag += 1;
}
else if (y.LessThan(One))
{
y *= Ten;
mag -= 1;
}
int decimalPointPos = mag + 1;
var buf = new StringBuilder();
int numDigits = MaxPrintDigits - 1;
for (int i = 0; i <= numDigits; i++)
{
if (insertDecimalPoint && i == decimalPointPos)
{
buf.Append('.');
}
int digit = (int) y._hi;
// System.out.println("printDump: [" + i + "] digit: " + digit + " y: " + y.dump() + " buf: " + buf);
/*
* This should never happen, due to heuristic checks on remainder below
*/
if (digit < 0 || digit > 9)
{
// System.out.println("digit > 10 : " + digit);
// throw new IllegalStateException("Internal errror: found digit = " + digit);
}
/*