-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNeslib.MultiPrecision.pas
More file actions
4677 lines (3829 loc) · 141 KB
/
Neslib.MultiPrecision.pas
File metadata and controls
4677 lines (3829 loc) · 141 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
unit Neslib.MultiPrecision;
{ Multi-precision floating-point library.
Uses QD 2.3.22 (https://www.davidhbailey.com/dhbsoftware/)
Adds support for 128-bit (DoubleDouble) and 256-bit (QuadDouble) floating-
point types.
These types offer the same range as the Double type (+/- 10e308) but a lot
more precision (2 and 4 times more):
Type Exponent Bits Mantissa Bits #Decimal digits (precision)
-------------------------------------------------------------------------
Single 9 24 7
Double 12 53 16
DoubleDouble 12 106 32
QuadDouble 12 212 64
The underlying QD library does not use emulation for calculations. Instead, it
uses the existing floating-point capabilities of the CPU to use either 2 or 4
Double values to increase the precision. As a result, these types are much
faster than other arbitrary/high precision math libraries using the same
precision. Also, as opposed to many other libraries, these types don't require
dynamic memory allocations, which further helps performance and also reduces
memory fragmentation.
Customization
-------------
The default configuration of the library is suitable for most applications.
This configuration sacrifices a bit of accuracy for increase speed. If
accuracy is more important than speed for your purposes, then you can compile
the library with the MP_ACCURATE define. This will make many calculations a
bit slower but more accurate. }
{$SCOPEDENUMS ON}
interface
uses
System.SysUtils,
System.Math;
{ Initializes the system to work with the DoubleDouble and QuadDouble types.
Returns:
The old FPU/SSE state.
You *must* call this function before doing any multi-precision math. It sets
the correct precision mode and disables floating-point exceptions.
You can call MultiPrecisionReset to restore the FPU/SSE to the previous state
by passing the value returned from this function. }
function MultiPrecisionInit: UInt32;
{ Resets the FPU/SSE state.
Parameters:
AState: the FPU/SSE state as returned by a previous call to
MultiPrecisionInit }
procedure MultiPrecisionReset(const AState: UInt32);
type
{ Formats used for converting a multi-precision floating-point value to a
string. }
TMPFloatFormat = (
{ Fixed format }
Fixed,
{ Scientific format }
Scientific);
type
{ 128-bit floating-point type, comprised of two double-precision
floating-point values. Can be used much like a regular Double value.
Offers a range of +/- 10e308 and precision of approximately 32 decimal
digits. }
DoubleDouble = record
public
{ The 2 double-precision floating-point values making up this value. }
X: array [0..1] of Double;
{$REGION 'Internal Declarations'}
private
function GetSign: Boolean; inline;
procedure SetSign(const Value: Boolean); inline;
function GetExp: UInt64; inline;
procedure SetExp(const Value: UInt64); inline;
function GetSpecialType: TFloatSpecial; inline;
private
procedure ToDigits(const S: TArray<Char>; var Exponent: Integer;
const Precision: Integer);
{$ENDREGION 'Internal Declarations'}
public
{ Various ways to explicitly initialize a DoubleDouble value.
Parameters:
Value: the Double value to convert to a DoubleDouble.
Hi, Lo: the two Double values that make up this DoubleDouble.
S: a string representation of the DoubleDouble floating-point value.
Uses the system-wide format settings if FormatSettings is not
specified.
FormatSettings: the format settings used for the string S }
procedure Init; overload; inline;
procedure Init(const Value: Double); overload; inline;
procedure Init(const Hi, Lo: Double); overload; inline;
procedure Init(const S: String); overload; inline;
procedure Init(const S: String; const FormatSettings: TFormatSettings); overload;
{ Clears the value to 0 (alias for Init without parameters) }
procedure Clear; inline;
{ Implicitly converts a string to a DoubleDouble.
The string *must* use the period ('.') as a decimal separator. }
class operator Implicit(const S: String): DoubleDouble; inline; static;
// *NO* implicit conversions from Double to DoubleDouble, to avoid
// unintentional conversions that may impact performance.
//class operator Implicit(const Value: Double): DoubleDouble; inline; static;
{ Explicit conversion from a Double or Extended to a DoubleDouble. }
class operator Explicit(const Value: Double): DoubleDouble; inline; static;
class operator Explicit(const Value: Extended): DoubleDouble; inline; static;
{ Equality operators.
Used to compare DoubleDouble values with Double or other DoubleDouble
values. }
class operator Equal(const A, B: DoubleDouble): Boolean; inline; static;
class operator Equal(const A: DoubleDouble; const B: Double): Boolean; inline; static;
class operator Equal(const A: Double; const B: DoubleDouble): Boolean; inline; static;
class operator NotEqual(const A, B: DoubleDouble): Boolean; inline; static;
class operator NotEqual(const A: DoubleDouble; const B: Double): Boolean; inline; static;
class operator NotEqual(const A: Double; const B: DoubleDouble): Boolean; inline; static;
class operator LessThan(const A, B: DoubleDouble): Boolean; inline; static;
class operator LessThan(const A: DoubleDouble; const B: Double): Boolean; inline; static;
class operator LessThan(const A: Double; const B: DoubleDouble): Boolean; inline; static;
class operator LessThanOrEqual(const A, B: DoubleDouble): Boolean; inline; static;
class operator LessThanOrEqual(const A: DoubleDouble; const B: Double): Boolean; inline; static;
class operator LessThanOrEqual(const A: Double; const B: DoubleDouble): Boolean; inline; static;
class operator GreaterThan(const A, B: DoubleDouble): Boolean; inline; static;
class operator GreaterThan(const A: DoubleDouble; const B: Double): Boolean; inline; static;
class operator GreaterThan(const A: Double; const B: DoubleDouble): Boolean; inline; static;
class operator GreaterThanOrEqual(const A, B: DoubleDouble): Boolean; inline; static;
class operator GreaterThanOrEqual(const A: DoubleDouble; const B: Double): Boolean; inline; static;
class operator GreaterThanOrEqual(const A: Double; const B: DoubleDouble): Boolean; inline; static;
{ Aritmethic operators.
You can mix and match Double and DoubleDouble values. }
class operator Negative(const A: DoubleDouble): DoubleDouble; inline; static;
class operator Add(const A, B: DoubleDouble): DoubleDouble; inline; static;
class operator Add(const A: Double; const B: DoubleDouble): DoubleDouble; inline; static;
class operator Add(const A: DoubleDouble; const B: Double): DoubleDouble; inline; static;
class operator Subtract(const A, B: DoubleDouble): DoubleDouble; inline; static;
class operator Subtract(const A: Double; const B: DoubleDouble): DoubleDouble; inline; static;
class operator Subtract(const A: DoubleDouble; const B: Double): DoubleDouble; inline; static;
class operator Multiply(const A, B: DoubleDouble): DoubleDouble; inline; static;
class operator Multiply(const A: Double; const B: DoubleDouble): DoubleDouble; inline; static;
class operator Multiply(const A: DoubleDouble; const B: Double): DoubleDouble; inline; static;
class operator Divide(const A, B: DoubleDouble): DoubleDouble; inline; static;
class operator Divide(const A: Double; const B: DoubleDouble): DoubleDouble; inline; static;
class operator Divide(const A: DoubleDouble; const B: Double): DoubleDouble; inline; static;
{ Whether this value equals 0.
This is a bit faster than comparing against 0. }
function IsZero: Boolean; inline;
{ Whether this value equals 1.
This is a bit faster than comparing against 1. }
function IsOne: Boolean; inline;
{ Whether this value is positive.
This is a bit faster than comparing against > 0. }
function IsPositive: Boolean; inline;
{ Whether this value is negative.
This is a bit faster than comparing against < 0. }
function IsNegative: Boolean; inline;
{ Whether this value is Not-a-Number }
function IsNan: Boolean; inline;
{ Whether this value is infinite (positive or negative) }
function IsInfinity: Boolean; inline;
{ Whether this value is negative infinite }
function IsNegativeInfinity: Boolean; overload; inline;
{ Whether this value is positive infinite }
function IsPositiveInfinity: Boolean; overload; inline;
{ Compares against another Double or DoubleDouble value.
Returns True if equal, or False otherwise. }
function Equals(const A: DoubleDouble): Boolean; overload; inline;
function Equals(const A: Double): Boolean; overload; inline;
{ Converts this DoubleDouble value to an Integer }
function ToInteger: Integer; inline;
{ Converts this DoubleDouble value to a Double }
function ToDouble: Double; inline;
{ Converts this DoubleDouble value to an Extended }
function ToExtended: Extended; inline;
{ Converts this DoubleDouble value to a String.
Parameters:
FormatSettings: (optional) the format settings to use (mostly for the
decimal separator).
Format: (optional) output format. Either Fixed or Scientific.
Precision: (optional) number of digits in the output. Defaults to 31
(the maximum appropriate precision for a DoubleDouble value). }
function ToString(const Format: TMPFloatFormat = TMPFloatFormat.Scientific;
const Precision: Integer = 31): String; overload; inline;
function ToString(const FormatSettings: TFormatSettings;
const Format: TMPFloatFormat = TMPFloatFormat.Scientific;
const Precision: Integer = 31): String; overload;
{ Converts a DoubleDouble value to a String.
Parameters:
Value: the DoubleDouble value to convert.
FormatSettings: (optional) the format settings to use (mostly for the
decimal separator).
Format: (optional) output format. Either Fixed or Scientific.
Precision: (optional) number of digits in the output. Defaults to 31
(the maximum appropriate precision for a DoubleDouble value). }
class function ToString(const Value: DoubleDouble;
const Format: TMPFloatFormat = TMPFloatFormat.Scientific;
const Precision: Integer = 31): String; overload; inline; static;
class function ToString(const Value: DoubleDouble;
const FormatSettings: TFormatSettings;
const Format: TMPFloatFormat = TMPFloatFormat.Scientific;
const Precision: Integer = 31): String; overload; inline; static;
{ Parses a string and converts it to a DoubleDouble.
Parameters
S: the string to parse. Uses the system-wide format settings if
FormatSettings is not specified.
FormatSettings: (optional) format settings to use for parsing.
Returns:
The converted DoubleDouble value, or DoubleDouble.NaN if the string S
could not be converted. }
class function Parse(const S: String): DoubleDouble; overload; inline; static;
class function Parse(const S: String;
const FormatSettings: TFormatSettings): DoubleDouble; overload; inline; static;
{ Tries to parse a string and convert it to a DoubleDouble.
Parameters
S: the string to parse. Uses the system-wide format settings if
FormatSettings is not specified.
Value: is set to the converted DoubleDouble value.
FormatSettings: (optional) format settings to use for parsing.
Returns:
True if S was successfully converted, or False otherwise. }
class function TryParse(const S: String; out Value: DoubleDouble): Boolean; overload; inline; static;
class function TryParse(const S: String; out Value: DoubleDouble;
const FormatSettings: TFormatSettings): Boolean; overload; inline; static;
{ The size of DoubleDouble values. Will always be 16. }
class function Size: Integer; inline; static;
{ The high-order Double value used to make up this value. }
property Hi: Double read X[0];
{ The low-order Double value used to make up this value. }
property Lo: Double read X[1];
{ The sign of this value. True if negative, False otherwise. }
property Sign: Boolean read GetSign write SetSign;
{ The raw exponent part }
property Exp: UInt64 read GetExp write SetExp;
{ The type of this value. }
property SpecialType: TFloatSpecial read GetSpecialType;
end;
PDoubleDouble = ^DoubleDouble;
PPDoubleDouble = ^PDoubleDouble;
type
{ Record helper that defines some DoubleDouble constants }
_DoubleDoubleHelper = record helper for DoubleDouble
const
{ The number of digits of precision for DoubleDouble values }
NumDigits = 31;
{ The upper bound on the relative error due to rounding }
Epsilon = 4.93038065763132e-32;
{ The minimum value of a DoubleDouble }
MinValue = 2.0041683600089728e-292;
const
{ Pi }
Pi : DoubleDouble = (X: (3.141592653589793116e+00,
1.224646799147353207e-16));
{ Pi * 2 }
PiTimes2 : DoubleDouble = (X: (6.283185307179586232e+00,
2.449293598294706414e-16));
{ Pi / 2}
PiOver2 : DoubleDouble = (X: (1.570796326794896558e+00,
6.123233995736766036e-17));
{ Pi / 4}
PiOver4 : DoubleDouble = (X: (7.853981633974482790e-01,
3.061616997868383018e-17));
{ (Pi * 3) / 4}
PiTimes3Over4 : DoubleDouble = (X: (2.356194490192344837e+00,
9.1848509936051484375e-17));
{ Pi / 180 }
PiOver180 : DoubleDouble = (X: (0.017453292519943295e+00,
2.9486522708701687e-19));
{ 180 / Pi }
_180OverPi : DoubleDouble = (X: (5.729577951308232e+01,
-1.9878495670576283e-15));
{ e }
E : DoubleDouble = (X: (2.718281828459045091e+00,
1.445646891729250158e-16));
{ The natural log of 2 }
Log2 : DoubleDouble = (X: (6.931471805599452862e-01,
2.319046813846299558e-17));
{ The natural log of 10 }
Log10 : DoubleDouble = (X: (2.302585092994045901e+00,
-2.170756223382249351e-16));
{ 0 }
Zero : DoubleDouble = (X: (0, 0));
{ 1 }
One : DoubleDouble = (X: (1, 0));
{ Not-a-Number }
NaN : DoubleDouble = (X: (NaN, NaN));
{ Positive infinity }
PositiveInfinity: DoubleDouble = (X: (Infinity, Infinity));
{ Negative infinity }
NegativeInfinity: DoubleDouble = (X: (NegInfinity, NegInfinity));
{ The maximum absolute value }
MaxValue : DoubleDouble = (X: (1.79769313486231570815e+308,
9.97920154767359795037e+291));
{ A safe maximum absolute value that can be used in calculations. }
SafeMaxValue : DoubleDouble = (X: (1.7976931080746007281e+308,
9.97920154767359795037e+291));
end;
type
{ 256-bit floating-point type, comprised of four double-precision
floating-point values. Can be used much like a regular Double value.
Offers a range of +/- 10e308 and precision of approximately 64 decimal
digits. }
QuadDouble = record
public
{ The 4 double-precision floating-point values making up this value. }
X: array [0..3] of Double;
{$REGION 'Internal Declarations'}
private
function GetSign: Boolean; inline;
procedure SetSign(const Value: Boolean); inline;
function GetExp: UInt64; inline;
procedure SetExp(const Value: UInt64); inline;
function GetSpecialType: TFloatSpecial; inline;
private
procedure ToDigits(const S: TArray<Char>; var Exponent: Integer;
const Precision: Integer);
{$ENDREGION 'Internal Declarations'}
public
{ Various ways to explicitly initialize a QuadDouble value.
Parameters:
Value: the Double or DoubleDouble value to convert to a QuadDouble.
X0, X1, X2, X3: the four Double values that make up this QuadDouble.
S: a string representation of the QuadDouble floating-point value.
Uses the system-wide format settings if FormatSettings is not
specified.
FormatSettings: the format settings used for the string S }
procedure Init; overload; inline;
procedure Init(const Value: Double); overload; inline;
procedure Init(const Value: DoubleDouble); overload; inline;
procedure Init(const X0, X1, X2, X3: Double); overload; inline;
procedure Init(const S: String); overload; inline;
procedure Init(const S: String; const FormatSettings: TFormatSettings); overload;
{ Clears the value to 0 (alias for Init without parameters) }
procedure Clear; inline;
{ Implicitly converts a string to a DoubleDouble.
The string *must* use the period ('.') as a decimal separator. }
class operator Implicit(const S: String): QuadDouble; inline; static;
// *NO* implicit conversions from Double/DoubleDouble to QuadDouble, to
// avoid unintentional conversions that may impact performance.
//class operator Implicit(const Value: Double): QuadDouble; inline; static;
//class operator Implicit(const Value: DoubleDouble): QuadDouble; inline; static;
{ Explicit conversion from a Double, Extended or DoubleDouble to a
QuadDouble. }
class operator Explicit(const Value: Double): QuadDouble; inline; static;
class operator Explicit(const Value: Extended): QuadDouble; inline; static;
class operator Explicit(const Value: DoubleDouble): QuadDouble; inline; static;
{ Equality operators.
Used to compare QuadDouble values with Double, DoubleDouble or other
QuadDouble values. }
class operator Equal(const A, B: QuadDouble): Boolean; inline; static;
class operator Equal(const A: QuadDouble; const B: Double): Boolean; inline; static;
class operator Equal(const A: Double; const B: QuadDouble): Boolean; inline; static;
class operator Equal(const A: QuadDouble; const B: DoubleDouble): Boolean; inline; static;
class operator Equal(const A: DoubleDouble; const B: QuadDouble): Boolean; inline; static;
class operator NotEqual(const A, B: QuadDouble): Boolean; inline; static;
class operator NotEqual(const A: QuadDouble; const B: Double): Boolean; inline; static;
class operator NotEqual(const A: Double; const B: QuadDouble): Boolean; inline; static;
class operator NotEqual(const A: QuadDouble; const B: DoubleDouble): Boolean; inline; static;
class operator NotEqual(const A: DoubleDouble; const B: QuadDouble): Boolean; inline; static;
class operator LessThan(const A, B: QuadDouble): Boolean; inline; static;
class operator LessThan(const A: QuadDouble; const B: Double): Boolean; inline; static;
class operator LessThan(const A: Double; const B: QuadDouble): Boolean; inline; static;
class operator LessThan(const A: QuadDouble; const B: DoubleDouble): Boolean; inline; static;
class operator LessThan(const A: DoubleDouble; const B: QuadDouble): Boolean; inline; static;
class operator LessThanOrEqual(const A, B: QuadDouble): Boolean; inline; static;
class operator LessThanOrEqual(const A: QuadDouble; const B: Double): Boolean; inline; static;
class operator LessThanOrEqual(const A: Double; const B: QuadDouble): Boolean; inline; static;
class operator LessThanOrEqual(const A: QuadDouble; const B: DoubleDouble): Boolean; inline; static;
class operator LessThanOrEqual(const A: DoubleDouble; const B: QuadDouble): Boolean; inline; static;
class operator GreaterThan(const A, B: QuadDouble): Boolean; inline; static;
class operator GreaterThan(const A: QuadDouble; const B: Double): Boolean; inline; static;
class operator GreaterThan(const A: Double; const B: QuadDouble): Boolean; inline; static;
class operator GreaterThan(const A: QuadDouble; const B: DoubleDouble): Boolean; inline; static;
class operator GreaterThan(const A: DoubleDouble; const B: QuadDouble): Boolean; inline; static;
class operator GreaterThanOrEqual(const A, B: QuadDouble): Boolean; inline; static;
class operator GreaterThanOrEqual(const A: QuadDouble; const B: Double): Boolean; inline; static;
class operator GreaterThanOrEqual(const A: Double; const B: QuadDouble): Boolean; inline; static;
class operator GreaterThanOrEqual(const A: QuadDouble; const B: DoubleDouble): Boolean; inline; static;
class operator GreaterThanOrEqual(const A: DoubleDouble; const B: QuadDouble): Boolean; inline; static;
{ Aritmethic operators.
You can mix and match Double, DoubleDouble and QuadDouble values. }
class operator Negative(const A: QuadDouble): QuadDouble; inline; static;
class operator Add(const A, B: QuadDouble): QuadDouble; inline; static;
class operator Add(const A: Double; const B: QuadDouble): QuadDouble; inline; static;
class operator Add(const A: QuadDouble; const B: Double): QuadDouble; inline; static;
class operator Add(const A: DoubleDouble; const B: QuadDouble): QuadDouble; inline; static;
class operator Add(const A: QuadDouble; const B: DoubleDouble): QuadDouble; inline; static;
class operator Subtract(const A, B: QuadDouble): QuadDouble; inline; static;
class operator Subtract(const A: Double; const B: QuadDouble): QuadDouble; inline; static;
class operator Subtract(const A: QuadDouble; const B: Double): QuadDouble; inline; static;
class operator Subtract(const A: DoubleDouble; const B: QuadDouble): QuadDouble; inline; static;
class operator Subtract(const A: QuadDouble; const B: DoubleDouble): QuadDouble; inline; static;
class operator Multiply(const A, B: QuadDouble): QuadDouble; inline; static;
class operator Multiply(const A: Double; const B: QuadDouble): QuadDouble; inline; static;
class operator Multiply(const A: QuadDouble; const B: Double): QuadDouble; inline; static;
class operator Multiply(const A: DoubleDouble; const B: QuadDouble): QuadDouble; inline; static;
class operator Multiply(const A: QuadDouble; const B: DoubleDouble): QuadDouble; inline; static;
class operator Divide(const A, B: QuadDouble): QuadDouble; inline; static;
class operator Divide(const A: Double; const B: QuadDouble): QuadDouble; inline; static;
class operator Divide(const A: QuadDouble; const B: Double): QuadDouble; inline; static;
class operator Divide(const A: DoubleDouble; const B: QuadDouble): QuadDouble; inline; static;
class operator Divide(const A: QuadDouble; const B: DoubleDouble): QuadDouble; inline; static;
{ Whether this value equals 0.
This is a bit faster than comparing against 0. }
function IsZero: Boolean; inline;
{ Whether this value equals 1.
This is a bit faster than comparing against 1. }
function IsOne: Boolean; inline;
{ Whether this value is positive.
This is a bit faster than comparing against > 0. }
function IsPositive: Boolean; inline;
{ Whether this value is negative.
This is a bit faster than comparing against < 0. }
function IsNegative: Boolean; inline;
{ Whether this value is Not-a-Number }
function IsNan: Boolean; inline;
{ Whether this value is infinite (positive or negative) }
function IsInfinity: Boolean; inline;
{ Whether this value is negative infinite }
function IsNegativeInfinity: Boolean; overload; inline;
{ Whether this value is positive infinite }
function IsPositiveInfinity: Boolean; overload; inline;
{ Compares against another Double, DoubleDouble or QuadDouble value.
Returns True if equal, or False otherwise. }
function Equals(const A: QuadDouble): Boolean; overload; inline;
function Equals(const A: DoubleDouble): Boolean; overload; inline;
function Equals(const A: Double): Boolean; overload; inline;
{ Converts this QuadDouble value to an Integer }
function ToInteger: Integer; inline;
{ Converts this QuadDouble value to a Double }
function ToDouble: Double; inline;
{ Converts this QuadDouble value to an Extended }
function ToExtended: Extended; inline;
{ Converts this QuadDouble value to a DoubleDouble }
function ToDoubleDouble: DoubleDouble; inline;
{ Converts this QuadDouble value to a String.
Parameters:
FormatSettings: (optional) the format settings to use (mostly for the
decimal separator).
Format: (optional) output format. Either Fixed or Scientific.
Precision: (optional) number of digits in the output. Defaults to 62
(the maximum appropriate precision for a QuadDouble value). }
function ToString(const Format: TMPFloatFormat = TMPFloatFormat.Scientific;
const Precision: Integer = 62): String; overload; inline;
function ToString(const FormatSettings: TFormatSettings;
const Format: TMPFloatFormat = TMPFloatFormat.Scientific;
const Precision: Integer = 62): String; overload;
{ Converts a QuadDouble value to a String.
Parameters:
Value: the QuadDouble value to convert.
FormatSettings: (optional) the format settings to use (mostly for the
decimal separator).
Format: (optional) output format. Either Fixed or Scientific.
Precision: (optional) number of digits in the output. Defaults to 62
(the maximum appropriate precision for a QuadDouble value). }
class function ToString(const Value: QuadDouble;
const Format: TMPFloatFormat = TMPFloatFormat.Scientific;
const Precision: Integer = 62): String; overload; inline; static;
class function ToString(const Value: QuadDouble;
const FormatSettings: TFormatSettings;
const Format: TMPFloatFormat = TMPFloatFormat.Scientific;
const Precision: Integer = 62): String; overload; inline; static;
{ Parses a string and converts it to a QuadDouble.
Parameters
S: the string to parse. Uses the system-wide format settings if
FormatSettings is not specified.
FormatSettings: (optional) format settings to use for parsing.
Returns:
The converted QuadDouble value, or QuadDouble.NaN if the string S
could not be converted. }
class function Parse(const S: String): QuadDouble; overload; inline; static;
class function Parse(const S: String;
const FormatSettings: TFormatSettings): QuadDouble; overload; inline; static;
{ Tries to parse a string and convert it to a QuadDouble.
Parameters
S: the string to parse. Uses the system-wide format settings if
FormatSettings is not specified.
Value: is set to the converted QuadDouble value.
FormatSettings: (optional) format settings to use for parsing.
Returns:
True if S was successfully converted, or False otherwise. }
class function TryParse(const S: String; out Value: QuadDouble): Boolean; overload; inline; static;
class function TryParse(const S: String; out Value: QuadDouble;
const FormatSettings: TFormatSettings): Boolean; overload; inline; static;
{ The size of DoubleDouble values. Will always be 32. }
class function Size: Integer; inline; static;
{ The sign of this value. True if negative, False otherwise. }
property Sign: Boolean read GetSign write SetSign;
{ The raw exponent part }
property Exp: UInt64 read GetExp write SetExp;
{ The type of this value. }
property SpecialType: TFloatSpecial read GetSpecialType;
end;
PQuadDouble = ^QuadDouble;
PPQuadDouble = ^PQuadDouble;
type
{ Record helper that defines some QuadDouble constants }
_QuadDoubleHelper = record helper for QuadDouble
const
{ The number of digits of precision for QuadDouble values }
NumDigits = 62;
{ The upper bound on the relative error due to rounding }
Epsilon = 1.21543267145725e-63;
{ The minimum value of a QuadDouble }
MinValue = 1.6259745436952323e-260;
const
{ Pi }
Pi : QuadDouble = (X: (3.141592653589793116e+00,
1.224646799147353207e-16,
-2.994769809718339666e-33,
1.112454220863365282e-49));
{ Pi * 2 }
PiTimes2 : QuadDouble = (X: (6.283185307179586232e+00,
2.449293598294706414e-16,
-5.989539619436679332e-33,
2.224908441726730563e-49));
{ Pi / 2}
PiOver2 : QuadDouble = (X: (1.570796326794896558e+00,
6.123233995736766036e-17,
-1.497384904859169833e-33,
5.562271104316826408e-50));
{ Pi / 4}
PiOver4 : QuadDouble = (X: (7.853981633974482790e-01,
3.061616997868383018e-17,
-7.486924524295849165e-34,
2.781135552158413204e-50));
{ (Pi * 3) / 4}
PiTimes3Over4 : QuadDouble = (X: (2.356194490192344837e+00,
9.1848509936051484375e-17,
3.9168984647504003225e-33,
-2.5867981632704860386e-49));
{ Pi / 180 }
PiOver180 : QuadDouble = (X: (0.017453292519943295e+00,
2.9486522708701687e-19,
-1.3427726813345382e-35,
1.4287195201441093e-52));
{ 180 / Pi }
_180OverPi : QuadDouble = (X: (5.729577951308232e+01,
-1.9878495670576283e-15,
-1.6833394980391744e-31,
-5.5659577936980826e-49));
{ e }
E : QuadDouble = (X: (2.718281828459045091e+00,
1.445646891729250158e-16,
-2.127717108038176765e-33,
1.515630159841218954e-49));
{ The natural log of 2 }
Log2 : QuadDouble = (X: (6.931471805599452862e-01,
2.319046813846299558e-17,
5.707708438416212066e-34,
-3.582432210601811423e-50));
{ The natural log of 10 }
Log10 : QuadDouble = (X: (2.302585092994045901e+00,
-2.170756223382249351e-16,
-9.984262454465776570e-33,
-4.023357454450206379e-49));
{ 0 }
Zero : QuadDouble = (X: (0, 0, 0, 0));
{ 1 }
One : QuadDouble = (X: (1, 0, 0, 0));
{ Not-a-Number }
NaN : QuadDouble = (X: (NaN, NaN, NaN, NaN));
{ Positive infinity }
PositiveInfinity: QuadDouble = (X: (Infinity, Infinity, Infinity, Infinity));
{ Negative infinity }
NegativeInfinity: QuadDouble = (X: (NegInfinity, NegInfinity, NegInfinity, NegInfinity));
{ The maximum absolute value }
MaxValue : QuadDouble = (X: (1.79769313486231570815e+308,
9.97920154767359795037e+291,
5.53956966280111259858e+275,
3.07507889307840487279e+259));
{ A safe maximum absolute value that can be used in calculations. }
SafeMaxValue : QuadDouble = (X: (1.7976931080746007281e+308,
9.97920154767359795037e+291,
5.53956966280111259858e+275,
3.07507889307840487279e+259));
end;
{ The 4 basic aritmetic operators (+, -, *, /) that work on two Double values
and return a DoubleDouble result.
Parameters:
A: first Double value
B: second Double value
Returns:
The result as a DoubleDouble. }
function Add(const A, B: Double): DoubleDouble; inline;
function Subtract(const A, B: Double): DoubleDouble; inline;
function Multiply(const A, B: Double): DoubleDouble; inline;
function Divide(const A, B: Double): DoubleDouble; inline;
{ Calculates the inverse (1 / A) of a value.
Parameters:
A: the value to invert
Returns:
The inverse }
function Inverse(const A: DoubleDouble): DoubleDouble; overload; inline;
function Inverse(const A: QuadDouble): QuadDouble; overload; inline;
{ Multiplies A with B, where B is a power of 2.
Parameters:
A: the first value
B: the second value. Must be a power of 2.
Returns:
A * B. The return value is undefined if B is not a power of 2.
This function is faster than the multiplication operator if B is guaranteed to
be a power of 2. }
function MulPoT(const A: DoubleDouble; const B: Double): DoubleDouble; overload; inline;
function MulPoT(const A: QuadDouble; const B: Double): QuadDouble; overload; inline;
{ Calculates remainder of Dividend / Divisor, rounded to nearest.
Parameters:
Dividend: the value into which you are dividing
Divisor: the value by which to divide Dividend
Returns:
The remainder of Dividend / Divisor, rounded to nearest (where
Dividend / Divisor is rounded to nearest)
Use FMod instead for rounding towards zero. }
function Rem(const Dividend, Divisor: DoubleDouble): DoubleDouble; overload; inline;
function Rem(const Dividend, Divisor: QuadDouble): QuadDouble; overload; inline;
{ Calculates the result of a division, including the remainder.
Parameters:
Dividend: the value into which you are dividing
Divisor: the value by which to divide Dividend
Remainder: is set to the remainder
Result: is set to the result }
procedure DivRem(const Dividend, Divisor: DoubleDouble; out Remainder, Result: DoubleDouble); overload; inline;
function DivRem(const Dividend, Divisor: DoubleDouble; out Remainder: DoubleDouble): DoubleDouble; overload; inline;
procedure DivRem(const Dividend, Divisor: QuadDouble; out Remainder, Result: QuadDouble); overload; inline;
function DivRem(const Dividend, Divisor: QuadDouble; out Remainder: QuadDouble): QuadDouble; overload; inline;
{ Calculates remainder of Dividend / Divisor, rounded towards zero.
Parameters:
Dividend: the value into which you are dividing
Divisor: the value by which to divide Dividend
Returns:
The remainder of Dividend / Divisor, rounded towards zero (where
Dividend / Divisor is rounded towards zero)
Use Rem instead for rounding to nearest. }
function FMod(const Dividend, Divisor: DoubleDouble): DoubleDouble; overload; inline;
function FMod(const Dividend, Divisor: QuadDouble): QuadDouble; overload; inline;
{ Calculates a square root.
Parameters:
A: the value to calculate the square root of
Returns:
The square root of A. }
function Sqrt(const A: DoubleDouble): DoubleDouble; overload; inline;
function Sqrt(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates a square value.
Parameters:
A: the value to square
Returns:
The square of A. }
function Sqr(const A: DoubleDouble): DoubleDouble; overload; inline;
function Sqr(const A: QuadDouble): QuadDouble; overload; inline;
procedure Sqr(const A: Double; out Result: DoubleDouble); overload; inline;
{ Truncates a value.
Parameters:
A: the value to truncate.
Returns:
The truncated value (that is, with its fraction removed) }
function Trunc(const A: DoubleDouble): DoubleDouble; overload; inline;
function Trunc(const A: QuadDouble): QuadDouble; overload; inline;
{ Rounds a value towards negative infinity.
Parameters:
A: the value to round down.
Returns:
The rounded value }
function Floor(const A: DoubleDouble): DoubleDouble; overload; inline;
function Floor(const A: QuadDouble): QuadDouble; overload; inline;
{ Rounds a value towards positive infinity.
Parameters:
A: the value to round up.
Returns:
The rounded value }
function Ceil(const A: DoubleDouble): DoubleDouble; overload; inline;
function Ceil(const A: QuadDouble): QuadDouble; overload; inline;
{ Rounds a value to its nearest integer.
Parameters:
A: the value to round.
Returns:
The rounded value
NOTE: Unlike Delphi's built-in Round function, this version does *not* use
Banker's rounding: when the fraction is exactly 0.5, it always rounds up
instead of towards an even number. }
function Round(const A: DoubleDouble): DoubleDouble; overload; inline;
function Round(const A: QuadDouble): QuadDouble; overload; inline;
{ Returns the absolute value.
Parameters:
A: the value to take the absolute value of.
Returns:
The absolute value of A }
function Abs(const A: DoubleDouble): DoubleDouble; overload; inline;
function Abs(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates power of a value.
Parameters:
Base: the base value
Exponent: the exponent.
Returns:
The Base raised to Exponent power.
If Exponent is an integer value, then it is faster to use IntPower instead. }
function Power(const Base, Exponent: DoubleDouble): DoubleDouble; overload; inline;
function Power(const Base, Exponent: QuadDouble): QuadDouble; overload; inline;
{ Calculates power of a value, where the exponent is an integer.
Parameters:
Base: the base value
Exponent: the (integer) exponent.
Returns:
The Base raised to Exponent power. }
function IntPower(const Base: DoubleDouble; const Exponent: Integer): DoubleDouble; overload; inline;
function IntPower(const Base: QuadDouble; const Exponent: Integer): QuadDouble; overload; inline;
{ Calculates the Nth root of a value.
Parameters:
A: the value to calculate the Nth root of.
N: the root.
Returns:
The Nth root of A. }
function NRoot(const A: DoubleDouble; const N: Integer): DoubleDouble; overload; inline;
function NRoot(const A: QuadDouble; const N: Integer): QuadDouble; overload; inline;
{ Generate value from significand and exponent.
Calculates A * 2^Exp.
Parameters:
A: the significand.
Exp: the exponent.
Returns:
A * 2^Exp. }
function Ldexp(const A: DoubleDouble; const Exp: Integer): DoubleDouble; overload; inline;
function Ldexp(const A: QuadDouble; Exp: Integer): QuadDouble; overload; inline;
{ Calculates the exponential of A (e^A).
Parameters:
A: the value to calculate the exponential of.
Returns:
The exponential of A. }
function Exp(const A: DoubleDouble): DoubleDouble; overload; inline;
function Exp(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates the natural logarithm of A.
Parameters:
A: the value to calculate the natural logarithm of.
Returns:
The natural logarithm of A. }
function Ln(const A: DoubleDouble): DoubleDouble; overload; inline;
function Ln(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates the base 10 logarithm of A.
Parameters:
A: the value to calculate the base 10 logarithm of.
Returns:
The base 10 logarithm of A. }
function Log10(const A: DoubleDouble): DoubleDouble; overload; inline;
function Log10(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates the sine of the angle A, in radians.
Parameters:
A: the angle, in radians.
Returns:
The sine of A. }
function Sin(const A: DoubleDouble): DoubleDouble; overload; inline;
function Sin(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates the cosine of the angle A, in radians.
Parameters:
A: the angle, in radians.
Returns:
The cosine of A. }
function Cos(const A: DoubleDouble): DoubleDouble; overload; inline;
function Cos(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates the sine and cosine of the angle A, in radians.
Parameters:
A: the angle, in radians.
SinA: is set to the sine of A.
CosA: is set to the cosine of A.
The procedure is faster than calculating the sine and cosine individually. }
procedure SinCos(const A: DoubleDouble; out SinA, CosA: DoubleDouble); overload; inline;
procedure SinCos(const A: QuadDouble; out SinA, CosA: QuadDouble); overload; inline;
{ Calculates the tangent of A, in radians.
Parameters:
A: the angle, in radians.
Returns:
The tangent of A. }
function Tan(const A: DoubleDouble): DoubleDouble; overload; inline;
function Tan(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates the inverse sine.
Parameters:
A: the value, must be between -1 and 1.
Returns:
The inverse sine of A, in the range [-Pi/2..Pi/2] }
function ArcSin(const A: DoubleDouble): DoubleDouble; overload; inline;
function ArcSin(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates the inverse cosine.
Parameters:
A: the value, must be between -1 and 1.
Returns:
The inverse cosine of A, in the range [0..Pi] }
function ArcCos(const A: DoubleDouble): DoubleDouble; overload; inline;
function ArcCos(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates the inverse tangent.
Parameters:
A: the value.
Returns:
The inverse tangent of A }
function ArcTan(const A: DoubleDouble): DoubleDouble; overload; inline;
function ArcTan(const A: QuadDouble): QuadDouble; overload; inline;
{ Calculates the arctangent angle and quadrant.
Parameters:
Y: the vertical value.
X: the horizontal value.
Returns:
The value of ArcTan(Y/X) as an angle in the correct quadrant.
return value will be in the range -Pi to Pi. }
function ArcTan2(const Y, X: DoubleDouble): DoubleDouble; overload; inline;
function ArcTan2(const Y, X: QuadDouble): QuadDouble; overload; inline;
{ Calculates the hyperbolic sine of the angle A, in radians.
Parameters:
A: the angle, in radians.
Returns:
The hyperbolic sine of A. }
function Sinh(const A: DoubleDouble): DoubleDouble; overload; inline;