-
Notifications
You must be signed in to change notification settings - Fork 3
/
bigfloat.d
1368 lines (1187 loc) · 34.2 KB
/
bigfloat.d
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
// Written in the D programming language
/**
* A D programming language implementation of the
* General Decimal Arithmetic Specification,
* Version 1.70, (25 March 2009).
* http://www.speleotrove.com/decimal/decarith.pdf)
*
* Copyright Paul D. Anderson 2009 - 2012.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
**/
// (B)TODO: write some test cases for flag setting. test the add/sub/mul/div functions
// (B)TODO: to/from real or double (float) values needs definition and implementation.
module decimal.bigfloat;
import std.bigint;
import std.conv;
import std.stdio: write, writeln;
import std.stdio: writefln;
import std.string;
import decimal.context;
import decimal.arithmetic;
//import xint;
alias Decimal.context decimalContext;
alias Decimal.pushContext pushContext;
alias Decimal.popContext popContext;
unittest {
writeln("===================");
writeln("decimal.......begin");
writeln("===================");
}
// special values for NaN, Inf, etc.
private static enum SV {NONE, INF, QNAN, SNAN};
//public static DecimalContext context =
// DecimalContext(9, 99, Rounding.HALF_EVEN);
///
/// A struct representing an arbitrary-precision floating-point number.
///
/// The implementation follows the General Decimal Arithmetic
/// Specification, Version 1.70 (25 Mar 2009),
/// http://www.speleotrove.com/decimal.
/// This specification conforms with IEEE standard 754-2008.
///
struct Decimal {
public static DecimalContext context =
DecimalContext(9, 99, Rounding.HALF_EVEN);
private static ContextStack contextStack;
private SV sval = SV.QNAN; // special values: default value is quiet NaN
private bool signed = false; // true if the value is negative, false otherwise.
private int expo = 0; // the exponent of the Decimal value
private BigInt mant; // the coefficient of the Decimal value
package int digits; // the number of decimal digits in this number.
// (unless the number is a special value)
private:
// common decimal "numbers"
immutable Decimal NAN = Decimal(SV.QNAN);
immutable Decimal SNAN = Decimal(SV.SNAN);
immutable Decimal INFINITY = Decimal(SV.INF);
immutable Decimal NEG_INF = Decimal(SV.INF, true);
immutable Decimal ZERO = Decimal(SV.NONE);
immutable Decimal NEG_ZERO = Decimal(SV.NONE, true);
immutable BigInt BIG_ZERO = cast(immutable)BigInt(0);
immutable BigInt BIG_ONE = cast(immutable)BigInt(1);
immutable BigInt BIG_TWO = cast(immutable)BigInt(2);
immutable Decimal ONE = Decimal(1);
// immutable Decimal PI = Decimal(314156);
// static Decimal DONE = Decimal(BIG_ONE);
// static immutable Decimal TWO = cast(immutable)Decimal(2);
// static immutable Decimal FIVE = cast(immutable)Decimal(5);
// static immutable Decimal TEN = cast(immutable)Decimal(10);
unittest { // special value constants
Decimal num;
num = NAN;
assert(num.toString == "NaN");
num = SNAN;
assert(num.toString == "sNaN");
num = NEG_ZERO;
assert(num.toString == "-0");
num = INFINITY;
assert(num.toString == "Infinity");
}
public:
//--------------------------------
// construction
//--------------------------------
///
/// Constructs a new number given a special value and an optional sign.
///
public this(const SV sv, const bool sign = false) {
this.signed = sign;
this.sval = sv;
}
unittest { // special value construction
Decimal num = Decimal(SV.INF, true);
assert(num.toString == "-Infinity");
assert(num.toAbstract() == "[1,inf]");
}
/// Creates a Decimal from a boolean value.
/// false == 0, true == 1
public this(const bool value)
{
this = zero;
if (value) coefficient = 1;
}
unittest { // boolean construction
Decimal num = Decimal(false);
assert(num.toString == "0");
num = Decimal(true);
assert(num.toString == "1");
}
/// Constructs a number from a boolean sign, a BigInt coefficient and
/// an optional integer exponent.
/// The sign of the number is the value of the sign parameter
/// regardless of the sign of the coefficient.
/// The intial precision of the number is deduced from the number of decimal
/// digits in the coefficient.
this(const bool sign, const BigInt coefficient, const int exponent = 0) {
BigInt big = abs(coefficient);
this = zero();
this.signed = sign;
this.mant = big;
this.expo = exponent;
// (B)TODO: If we specify the number of digits this can be CTFE.
// The numDigits call is not CTFE.
this.digits = numDigits(this.mant);
}
unittest { // bool, BigInt, int construction
Decimal num;
num = Decimal(true, BigInt(7254), 94);
assert(num.toString == "-7.254E+97");
}
/// Constructs a Decimal from a BigInt coefficient and an
/// optional integer exponent. The sign of the number is the sign
/// of the coefficient. The initial precision is determined by the number
/// of digits in the coefficient.
this(const BigInt coefficient, const int exponent = 0) {
BigInt big = mutable(coefficient);
// TODO: why not add sgn to decimal?
bool sign = sgn(big) < 0;
this(sign, big, exponent);
};
unittest { // BigInt, int construction
Decimal num;
num = Decimal(BigInt(7254), 94);
assert(num.toString == "7.254E+97");
num = Decimal(BigInt(-7254));
assert(num.toString == "-7254");
}
/// Constructs a number from a sign, a long integer coefficient and
/// an integer exponent.
this(const bool sign, const long coefficient, const int exponent) {
this(sign, BigInt(coefficient), exponent);
}
/// Constructs a number from a sign, a long integer coefficient and
/// an integer exponent.
this(const bool sign, const BigInt coefficient, const int exponent, const int digits) {
BigInt big = abs(coefficient);
this = zero();
this.signed = sign;
this.mant = big;
this.expo = exponent;
this.digits = digits;
}
unittest {
write("w/digits...");
// const BigInt mant = BigInt(314159);
BigInt mant = BigInt("314159");
Decimal d = Decimal(false, mant, -5, 6);
writefln("d = %s", d);
mant = BigInt("3141590000000000000000000000000000000000000");
d = Decimal(false, mant, -42, 43);
writefln("d = %s", d);
writeln("test missing");
}
/// Constructs a number from an long coefficient
/// and an optional integer exponent.
this(const long coefficient, const int exponent) {
this(BigInt(coefficient), exponent);
}
/// Constructs a number from an long value
this(const long coefficient) {
this(BigInt(coefficient), 0);
}
unittest { // long value construction
Decimal num;
num = Decimal(7254, 94);
assert(num.toString == "7.254E+97");
num = Decimal(-7254L);
assert(num.toString == "-7254");
}
// uint128 constructors:
/// Constructs a number from a sign, a uint128 integer coefficient and
/// an optional integer exponent.
this(const bool sign, const uint128 coefficient, const int exponent = 0) {
this(sign, coefficient.toBigInt(), exponent);
}
/// Constructs a number from an uint128 coefficient
/// and an optional integer exponent.
this(const uint128 coefficient, const int exponent = 0) {
this(coefficient.toBigInt(), exponent);
}
unittest { // uint128 value construction
Decimal num;
num = Decimal(uint128(7254), 94);
assert(num.toString == "7.254E+97");
num = Decimal(uint128(7254L));
assert(num.toString == "7254");
}
// TODO: this(str): add tests for just over/under int.max, int.min
// Constructs a decimal number from a string representation
this(const string str) {
this = decimal.conv.toNumber(str);
};
unittest { // string construction
Decimal num;
num = Decimal("7254E94");
assert(num.toString == "7.254E+97");
num = Decimal("7254.005");
assert(num.toString == "7254.005");
}
unittest { // ctor(string)
Decimal num;
string str;
num = Decimal(1, 12334, -5);
str = "-0.12334";
assert(num.toString == str);
num = Decimal(-23456, 10);
str = "-2.3456E+14";
assert(num.toString == str);
num = Decimal(234568901234);
str = "234568901234";
assert(num.toString == str);
num = Decimal("123.457E+29");
str = "1.23457E+31";
assert(num.toString == str);
num = std.math.E;
str = "2.71828183";
assert(str == num.toString);
num = std.math.std.math.LOG2;
Decimal copy = Decimal(num);
assert(compareTotal!Decimal(num, copy) == 0);
}
// TODO: convert real to decimal w/o going to a string.
/// Constructs a decimal number from a real value.
this(const real r) {
string str = format("%.*G", cast(int)context.precision, r);
//writefln("r = %s", r);
//writefln("str = %s", str);
this(str);
//writefln("this = %s", this);
}
// TODO: add unittest for real value construction
// copy constructor
this(const Decimal that) {
this.signed = that.signed;
this.sval = that.sval;
this.digits = that.digits;
this.expo = that.expo;
this.mant = cast(BigInt) that.mant;
};
/// dup property
const Decimal dup() {
return Decimal(this);
}
unittest { // dup
Decimal num = Decimal(std.math.PI);
Decimal copy = num.dup;
assert(num == copy);
}
//--------------------------------
// assignment
//--------------------------------
/// Assigns a Decimal number (makes a copy)
void opAssign(T:Decimal)(const T that) {
this.signed = that.signed;
this.sval = that.sval;
this.digits = that.digits;
this.expo = that.expo;
this.mant = cast(BigInt) that.mant;
}
/// Assigns a BigInt value.
void opAssign(T:BigInt)(const T that) {
this = Decimal(that);
}
/// Assigns a long point value.
void opAssign(T:long)(const T that) {
this = Decimal(that);
}
/// Assigns a floating point value.
void opAssign(T:real)(const T that) {
this = Decimal(that);
}
/// Assigns a decimal value.
void opAssign(T)(const T that) if (isDecimal!T) {
this = decimal.conv.toBigDecimal!T(that);
}
unittest { // opAssign
Decimal num;
string str;
num = Decimal(1, 245, 8);
str = "-2.45E+10";
assert(num.toString == str);
num = long.max;
str = "9223372036854775807";
assert(num.toString == str);
num = real.max;
str = "1.1897315E+4932";
assert(str == num.toString);
// num = decimal.dec32.Dec32.max;
// str = "9.999999E+96";
// assert(num.toString == str);
num = BigInt("123456098420234978023480");
str = "123456098420234978023480";
assert(num.toString == str);
}
//--------------------------------
// string representations
//--------------------------------
/// Converts a number to an abstract string representation.
public const string toAbstract() {
return decimal.conv.toAbstract!Decimal(this);
}
/// Converts a number to an abstract string representation.
const string toExact() {
return decimal.conv.toExact!Decimal(this);
}
/// Converts a Decimal to a "scientific" string representation.
const string toSciString() {
return decimal.conv.sciForm!Decimal(this);
}
/// Converts a Decimal to an "engineering" string representation.
const string toEngString() {
return decimal.conv.engForm!Decimal(this);
}
/// Converts a number to its string representation.
const string toString() {
return decimal.conv.sciForm!Decimal(this);
}
//--------------------------------
// member properties
//--------------------------------
/// Returns the exponent of this number
@property
const int exponent() {
return this.expo;
}
@property
int exponent(int expo) {
this.expo = expo;
return this.expo;
}
@property
const BigInt coefficient() {
return cast(BigInt)this.mant;
}
@property
BigInt coefficient(BigInt mant) {
this.mant = mant;
return this.mant;
}
@property
BigInt coefficient(long mant) {
this.mant = BigInt(mant);
return this.mant;
}
@property
const ushort payload() {
if (this.isNaN) {
return cast(ushort)(this.mant.toLong);
}
return 0;
}
@property
ushort payload(const ushort value) {
if (this.isNaN) {
this.mant = BigInt(value);
return value;
}
return 0;
}
/// Returns the adjusted exponent of this number
@property const int adjustedExponent() {
return expo + digits - 1;
}
/// Returns the number of decimal digits in the coefficient of this number
const int getDigits() {
return this.digits;
}
@property const bool sign() {
return signed;
}
@property bool sign(bool value) {
signed = value;
return signed;
}
//--------------------------------
// floating point properties
//--------------------------------
/// Returns the default value for this type (NaN)
static Decimal init() {
return NAN.dup;
}
/// Returns NaN
static Decimal nan(ushort payload = 0) {
if (payload) {
Decimal dec = NAN.dup;
dec.payload = payload;
return dec;
}
return NAN.dup;
}
/// Returns signaling NaN
static Decimal snan(ushort payload = 0) {
if (payload) {
Decimal dec = SNAN.dup;
dec.payload = payload;
return dec;
}
return SNAN.dup;
}
/// Returns infinity.
static Decimal infinity(bool signed = false) {
return signed ? NEG_INF.dup : INFINITY.dup;
}
/// Returns zero.
static Decimal zero(bool signed = false) {
return signed ? NEG_ZERO.dup : ZERO.dup;
}
/// Returns 1.
static Decimal one(bool signed = false) {
return signed ? -ONE.dup : ONE.dup;
}
/// Returns 2.
static Decimal two() {
return Decimal(false, 2, 0);
}
/// Returns 1/2.
static Decimal half() {
return Decimal(false, 5, -1);
}
/// Returns the maximum number of decimal digits in this context.
static uint precision(const DecimalContext context = this.context) {
return context.precision;
}
/// Returns the maximum number of decimal digits in this context.
static uint dig(const DecimalContext context = this.context) {
return context.precision;
}
/// Returns the number of binary digits in this context.
static int mant_dig(const DecimalContext context = this.context) {
return cast(int)(context.precision/std.math.LOG2);
}
static int min_exp(const DecimalContext context = this.context) {
return cast(int)(context.minExpo);
}
static int max_exp(const DecimalContext context = this.context) {
return cast (int)(context.maxExpo);
}
// // (B)TODO: is there a way to make this const w/in a context?
// // (B)TODO: This is only used by Decimal -- maybe should move it there?
// // (B)TODO: The mantissa is 10^^(precision - 1), so probably don't need
// // to implement as a string.
// // Returns the maximum representable normal value in the current context.
// const string maxString() {
// string cstr = "9." ~ replicate("9", precision - 1)
// ~ "E" ~ format("%d", maxExpo);
// return cstr;
// }
// Returns the maximum representable normal value in the current context.
// (B)TODO: this is a fairly expensive operation. Can it be fixed?
static Decimal max(const DecimalContext context = this.context) {
return Decimal(context.maxString);
}
// Returns the maximum representable normal value in the current context.
// (B)TODO: this is a fairly expensive operation. Can it be fixed?
// (B)TODO: is this needed?
static Decimal max(const bool sign,
const DecimalContext context = this.context) {
Decimal result = Decimal(context.maxString);
return sign ? -result : result;
}
// /// Returns the minimum representable normal value in this context.
// static Decimal min_normal(const DecimalContext context = this.context) {
// return Decimal(1, context.minExpo);
// }
/// Returns the minimum representable subnormal value in this context.
static Decimal min(const DecimalContext context = this.context) {
return Decimal(1, context.tinyExpo);
}
/// Returns the smallest available increment to 1.0 in this context
static Decimal epsilon(const DecimalContext context = this.context) {
return Decimal(1, -context.precision);
}
static int min_10_exp(const DecimalContext context = this.context) {
return context.minExpo;
}
static int max_10_exp(const DecimalContext context = this.context) {
return context.maxExpo;
}
static DecimalContext pushContext(const DecimalContext context) {
contextStack.push(Decimal.context);
Decimal.context = context;
return context;
}
static DecimalContext pushContext() {
return pushContext(Decimal.context);
}
static DecimalContext pushContext(uint precision) {
DecimalContext context = Decimal.context;
context.precision = precision;
return pushContext(context);
}
static DecimalContext pushContext(Rounding mode) {
DecimalContext context = Decimal.context;
context.rounding = mode;
return pushContext(context);
}
static DecimalContext popContext() {
Decimal.context = contextStack.pop();
return Decimal.context;
}
/// Returns the radix (10)
immutable int radix = 10;
//--------------------------------
// classification properties
//--------------------------------
/// Returns true if this number's representation is canonical (always true).
const bool isCanonical() {
return true;
}
/// Returns the canonical form of the number.
const Decimal canonical() {
return this.dup;
}
unittest { // isCanonical
Decimal num = Decimal("2.50");
assert(num.isCanonical);
Decimal copy = num.canonical;
assert(compareTotal(num, copy) == 0);
}
/// Returns true if this number is exactly one.
const bool isOne() {
if (isOneReduced()) {
return true;
}
if (exponent > 0) {
return false;
}
Decimal test = reduce(this);
if (test.isOneReduced()) {
return true;
}
return false;
}
/// Returns true if this number is exactly (false, 1, 0).
const bool isOneReduced() {
return isFinite && !isSigned && coefficient == 1 && exponent == 0;
}
unittest { // isOne
Decimal num;
num = Decimal("1");
assert(num.isOne);
num = Decimal(false, 10, -1);
assert(num.isOne);
assert(!num.isOneReduced);
}
/// Returns true if this number is + or - zero.
const bool isZero() {
return isFinite && coefficient == 0;
}
unittest { // isZero
Decimal num;
num = Decimal("0");
assert(num.isZero);
num = Decimal("2.50");
assert(!num.isZero);
num = Decimal("-0E+2");
assert(num.isZero);
}
/// Returns true if this number is a quiet or signaling NaN.
const bool isNaN() {
return this.sval == SV.QNAN || this.sval == SV.SNAN;
}
/// Returns true if this number is a signaling NaN.
const bool isSignaling() {
return this.sval == SV.SNAN;
}
/// Returns true if this number is a quiet NaN.
const bool isQuiet() {
return this.sval == SV.QNAN;
}
unittest { // isNaN, isQuiet, isSignaling
Decimal num;
num = Decimal("2.50");
assert(!num.isNaN);
assert(!num.isQuiet);
assert(!num.isSignaling);
num = Decimal("NaN");
assert(num.isNaN);
assert(num.isQuiet);
assert(!num.isSignaling);
num = Decimal("-sNaN");
assert(num.isNaN);
assert(!num.isQuiet);
assert(num.isSignaling);
}
/// Returns true if this number is + or - infinity.
const bool isInfinite() {
return this.sval == SV.INF;
}
/// Returns true if this number is not an infinity or a NaN.
const bool isFinite() {
return sval != SV.INF
&& sval != SV.QNAN
&& sval != SV.SNAN;
}
unittest { // isFinite, isInfinite
Decimal num;
num = Decimal("2.50");
assert(!num.isInfinite);
assert(num.isFinite);
num = Decimal("-0.3");
assert(num.isFinite);
num = 0;
assert(num.isFinite);
num = Decimal("-Inf");
assert(num.isInfinite);
assert(!num.isFinite);
num = Decimal("NaN");
assert(!num.isInfinite);
assert(!num.isFinite);
}
/// Returns true if this number is a NaN or infinity.
const bool isSpecial() {
return sval == SV.INF
|| sval == SV.QNAN
|| sval == SV.SNAN;
}
unittest { // isSpecial
Decimal num;
num = Decimal.infinity(true);
assert(num.isSpecial);
num = Decimal.snan(1234);
assert(num.isSpecial);
num = 12378.34;
assert(!num.isSpecial);
}
/// Returns true if this number is negative. (Includes -0)
const bool isSigned() {
return this.signed;
}
alias isSigned isNegative;
unittest { // isSigned, isNegative
Decimal num;
num = Decimal("2.50");
assert(!num.isSigned);
assert(!num.isNegative);
num = Decimal("-12");
assert(num.isSigned);
assert(num.isNegative);
num = Decimal("-0");
assert(num.isSigned);
assert(num.isNegative);
}
/// Returns true if this number is subnormal.
const bool isSubnormal(const DecimalContext context = this.context) {
if (!isFinite) return false;
return adjustedExponent < context.minExpo;
}
/// Returns true if this number is normal.
const bool isNormal(const DecimalContext context = this.context) {
if (isFinite && !isZero) {
return adjustedExponent >= context.minExpo;
}
return false;
}
unittest { // isNormal, isSubnormal
Decimal num;
num = Decimal("2.50");
assert(num.isNormal);
assert(!num.isSubnormal);
num = Decimal("0.1E-99");
assert(!num.isNormal);
assert(num.isSubnormal);
num = Decimal("0.00");
assert(!num.isSubnormal);
assert(!num.isNormal);
num = Decimal("-Inf");
assert(!num.isNormal);
assert(!num.isSubnormal);
num = Decimal("NaN");
assert(!num.isSubnormal);
assert(!num.isNormal);
}
/* /// Returns true if this number is integral;
/// that is, if its fractional part is zero.
const bool isIntegral() {
// TODO: need to take trailing zeros into account
return expo >= 0;
}*/
/// Returns true if the number is an integer.
const bool isIntegralValued() {
if (isSpecial) return false;
if (exponent >= 0) return true;
uint expo = std.math.abs(exponent);
if (expo >= context.precision) return false;
if (coefficient % 10^^expo == 0) return true;
return false;
}
unittest { // isIntegralValued
Decimal num;
num = 12345;
assert(num.isIntegralValued);
num = BigInt("123456098420234978023480");
assert(num.isIntegralValued);
num = 1.5;
assert(!num.isIntegralValued);
num = 1.5E+1;
assert(num.isIntegralValued);
num = 0;
assert(num.isIntegralValued);
}
/// Returns true if this number is a true value.
/// Non-zero finite numbers are true.
/// Infinity is true and NaN is false.
const bool isTrue() {
return isFinite && !isZero || isInfinite;
}
/// Returns true if this number is a false value.
/// Finite numbers with zero coefficient are false.
/// Infinity is true and NaN is false.
const bool isFalse() {
return isNaN || isZero;
}
unittest { //isTrue/isFalse
assert(Decimal("1").isTrue);
assert(!Decimal("0").isTrue);
assert(infinity.isTrue);
assert(!nan.isTrue);
assert(Decimal("0").isFalse);
assert(!Decimal("1").isFalse);
assert(!infinity.isFalse);
assert(nan.isFalse);
}
const bool isZeroCoefficient() {
return !isSpecial && coefficient == 0;
}
unittest { // isZeroCoefficient
Decimal num;
num = 0;
assert(num.isZeroCoefficient);
num = BigInt("-0");
assert(num.isZeroCoefficient);
num = Decimal("0E+4");
assert(num.isZeroCoefficient);
num = 12345;
assert(!num.isZeroCoefficient);
num = 1.5;
assert(!num.isZeroCoefficient);
num = Decimal.NAN;
assert(!num.isZeroCoefficient);
num = Decimal.INFINITY;
assert(!num.isZeroCoefficient);
}
//--------------------------------
// comparison
//--------------------------------
/// Returns -1, 0 or 1, if this number is less than, equal to,
/// or greater than the argument, respectively.
const int opCmp(const Decimal that) {
return decimal.arithmetic.compare!Decimal(this, that, context);
}
/// Returns true if this number is equal to the argument.
/// Finite numbers are equal if they are numerically equal
/// to the current precision.
/// Infinities are equal if they have the same sign.
/// Zeros are equal regardless of sign.
/// A NaN is not equal to any number, not even to another NaN.
/// A number is not even equal to itself (this != this) if it is a NaN.
const bool opEquals(T:Decimal)(ref const T that) {
return equals!Decimal(this, that, context);
}
/// Returns true if this extended integer is equal to the argument.
const bool opEquals(T)(ref const T that) /*if (isIntegral!T)*/ {
return opEquals(Decimal(that));
}
unittest { // comparison
Decimal num1, num2;
num1 = 105;
num2 = 10.543;
assert(num1 != num2);
assert(num1 > num2);
assert(num2 < num1);
num1 = 10.543;
assert(num1 >= num2);
assert(num2 <= num1);
assert(num1 == num2);
}
//--------------------------------
// unary arithmetic operators
//--------------------------------
/// Returns the result of performing the specified
/// unary operation on this number.
private Decimal opUnary(string op)()
{
static if (op == "+") {
return plus!Decimal(this, context);
}
else static if (op == "-") {
return minus!Decimal(this, context);
}
else static if (op == "++") {
this = add!Decimal(this, Decimal(1), context);
return this;
}
else static if (op == "--") {
this = sub!Decimal(this, Decimal(1), context);
return this;
}
}
unittest { // opUnary
Decimal num, actual, expect;
num = 134;
expect = num;
actual = +num;
assert(actual == expect);
num = 134.02;
expect = -134.02;
actual = -num;
assert(actual == expect);
num = 134;
writefln("num = %s", num);
expect = 135;
actual = ++num;
writefln("num = %s", num);
assert(actual == expect);
num = 1.00E8;
expect = num - 1;
actual = --num;
assert(actual == expect);
num = 1.00E8;
expect = num;
actual = num--;
assert(actual == expect);
num = Decimal(9999999, 90);
expect = num;
actual = num++;
assert(actual == expect);
num = 12.35;
expect = 11.35;
actual = --num;
assert(actual == expect);
}