-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbcd.cpp
4705 lines (4216 loc) · 104 KB
/
bcd.cpp
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
/////////////////////////////////////////////////////////////////////////////////
//
// SourceFile: bcd.cpp
//
// Copyright (c) 2014-2021 ir. W.E. Huisman
// All rights reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////
//
// BCD
//
// Floating Point Precision Number class (Binary Coded Decimal)
// An Arbitrary float always has the format [sign][digit][.[digit]*][E[sign][digits]+]
// where sign is either '+' or '-' or is missing ('+' implied)
// And is always stored in normalized mode after an operation or conversion
// with an implied decimal point between the first and second position
//
// Copyright (c) 2014-2022 ir W. E. Huisman
// Version 1.5 of 03-01-2022
//
// Examples:
// E+03 15456712 45000000 00000000 -> 1545.671245
// E+01 34125600 00000000 00000000 -> 3.41256
// E-05 78976543 12388770 00000000 -> 0.0000789765431238877
//
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h" // Precompiled headers
#include "bcd.h" // OUR INTERFACE
#include "StdException.h" // Exceptions
#include <math.h> // Still needed for conversions of double
#include <locale.h>
#include <winnls.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Theoretical maximum of numerical separators
#define SEP_LEN 10
// string format number and money format functions
bool g_locale_valutaInit = false;
TCHAR g_locale_decimalSep [SEP_LEN + 1];
TCHAR g_locale_thousandSep[SEP_LEN + 1];
TCHAR g_locale_strCurrency[SEP_LEN + 1];
int g_locale_decimalSepLen = 0;
int g_locale_thousandSepLen = 0;
int g_locale_strCurrencyLen = 0;
// Error handling throws or we silently return -INF, INF, NaN
bool g_throwing = true;
// One-time initialization for printing numbers in the current locale
void
InitValutaString()
{
if(g_locale_valutaInit == false)
{
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, g_locale_decimalSep, SEP_LEN);
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, g_locale_thousandSep,SEP_LEN);
GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, g_locale_strCurrency,SEP_LEN);
g_locale_decimalSepLen = (int)_tclen(g_locale_decimalSep);
g_locale_thousandSepLen = (int)_tclen(g_locale_thousandSep);
g_locale_strCurrencyLen = (int)_tclen(g_locale_strCurrency);
g_locale_valutaInit = true;
}
}
//////////////////////////////////////////////////////////////////////////
//
// CONSTRUCTORS OF BCD
//
//////////////////////////////////////////////////////////////////////////
// bcd::bcd
// Description: Default constructor
// Technical: Initialize the number at zero (0)
bcd::bcd()
{
Zero();
}
// bcd::bcd(bcd& arg)
// Description: Copy constructor of a bcd
// Technical: Copies all data members
bcd::bcd(const bcd& p_arg)
{
m_sign = p_arg.m_sign;
m_exponent = p_arg.m_exponent;
// Create and copy mantissa
memcpy(m_mantissa,p_arg.m_mantissa,bcdLength * sizeof(long));
}
// bcd::bcd(value)
// Description: BCD from a char value
bcd::bcd(const TCHAR p_value)
{
SetValueInt((int)p_value);
}
#ifndef UNICODE
// bcd::bcd(value)
// Description: BCD from an unsigned char value
bcd::bcd(const _TUCHAR p_value)
{
SetValueInt((int)p_value);
}
#endif
// bcd::bcd(value)
// Description: BCD from a short value
//
bcd::bcd(const short p_value)
{
SetValueInt((int)p_value);
}
// bcd::bcd(value)
// Description: BCD from an unsigned short value
//
bcd::bcd(const unsigned short p_value)
{
SetValueInt((int)p_value);
}
// bcd::bcd(value)
// BCD from an integer
bcd::bcd(const int p_value)
{
SetValueInt(p_value);
}
// bcd::bcd(value)
// BCD from an unsigned integer
bcd::bcd(const unsigned int p_value)
{
SetValueInt64((int64)p_value,0);
}
// bcd::bcd(value,value)
// Description: Construct a BCD from a long and an option long
// Technical: See description of SetValueLong
//
bcd::bcd(const long p_value, const long p_restValue /*= 0*/)
{
SetValueLong(p_value,p_restValue);
}
// bcd::bcd(value,value)
// Description: Construct a BCD from an unsigned long and an unsigned optional long
// Technical: See description of SetValueLong
bcd::bcd(const unsigned long p_value, const unsigned long p_restValue /*= 0*/)
{
SetValueInt64((int64)p_value,(int64)p_restValue);
}
// bcd::bcd(value,value)
// Description: Construct a BCD from a 64bit long and an optional long
// Technical: See description of SetValueInt64
//
bcd::bcd(const int64 p_value,const int64 p_restvalue /*= 0*/)
{
SetValueInt64(p_value,p_restvalue);
}
bcd::bcd(const uint64 p_value,const int64 p_restvalue)
{
SetValueUInt64(p_value,p_restvalue);
}
// bcd::bcd(float)
// Description: Construct a bcd from a float
//
bcd::bcd(const float p_value)
{
SetValueDouble((double)p_value);
}
// bcd::bcd(double)
// Description: Construct a bcd from a double
//
bcd::bcd(const double p_value)
{
SetValueDouble(p_value);
}
// BCD From a character string
// Description: Assignment-constructor from an elementary character data pointer
// Parameters: p_string -> Input character pointer (containing a number)
// p_fromDB -> Input comes from a database (always American format)
bcd::bcd(LPCTSTR p_string,bool p_fromDB /*= false*/)
{
SetValueString(p_string,p_fromDB);
}
// bcd::bcd(numeric)
// Description: Assignment-constructor for bcd from a SQL NUMERIC
// Parameters: p_numeric -> Input from a SQL ODBC database
// from a NUMERIC field
//
bcd::bcd(const SQL_NUMERIC_STRUCT* p_numeric)
{
SetValueNumeric(p_numeric);
}
// bcd::bcd(Sign)
// Description: Construct a bcd from a NULL in the database
// Parameters: p_sign : BUT GETS IGNORED!!
//
bcd::bcd(const bcd::Sign /*p_sign*/)
{
Zero();
// We ignore the argument!!
m_sign = Sign::ISNULL;
}
//////////////////////////////////////////////////////////////////////////
//
// END OF CONSTRUCTORS OF BCD
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// CONSTANTS OF BCD
//
//////////////////////////////////////////////////////////////////////////
// bcd::PI
// Description: Circumference/Radius ratio of a circle
// Technical: Nature constant that never changes
bcd
bcd::PI()
{
bcd pi;
// PI in 120 decimals:
// +31415926_53589793_23846264_33832795_02884197
// 16939937 51058209 74944592 30781640 62862089
// 98628034 82534211 70679821 48086513 28230665
pi.m_mantissa[0] = 31415926L;
pi.m_mantissa[1] = 53589793L;
pi.m_mantissa[2] = 23846264L;
pi.m_mantissa[3] = 33832795L;
pi.m_mantissa[4] = 2884197L;
return pi;
}
// bcd::LN2
// Description: Natural logarithm of two
// Technical: Mathematical constant that never changes
bcd
bcd::LN2()
{
bcd ln2;
// LN2 in 120 decimals: Use if you expand bcdLength
// +0.69314718_05599453_09417232_12145817_65680755
// 00134360 25525412 06800094 93393621 96969471
// 56058633 26996418 68754200 14810205 70685714
ln2.m_exponent = -1;
ln2.m_mantissa[0] = 69314718L;
ln2.m_mantissa[1] = 5599453L;
ln2.m_mantissa[2] = 9417232L;
ln2.m_mantissa[3] = 12145817L;
ln2.m_mantissa[4] = 65680755L;
return ln2;
}
// bcd::LN10
// Description: Natural logarithm of ten
// Technical: Mathematical constant that never changes
bcd
bcd::LN10()
{
bcd ln10;
// LN10 in 120 decimals: Use if you expand bcdLength
// +2.3025850_92994045_68401799_14546843_64207601
// 10148862 87729760 33327900 96757260 96773524
// 80235997 20508959 82983419 67784042 28624865
ln10.m_mantissa[0] = 23025850L;
ln10.m_mantissa[1] = 92994045L;
ln10.m_mantissa[2] = 68401799L;
ln10.m_mantissa[3] = 14546843L;
ln10.m_mantissa[4] = 64207601L;
return ln10;
}
//////////////////////////////////////////////////////////////////////////
//
// END OF CONSTANTS OF BCD
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// ERROR HANDLING
//
//////////////////////////////////////////////////////////////////////////
/*static */ void
bcd::ErrorThrows(bool p_throws /*= true*/)
{
g_throwing = p_throws;
}
//////////////////////////////////////////////////////////////////////////
//
// OPERATORS OF BCD
//
//////////////////////////////////////////////////////////////////////////
// bcd::+
// Description: Addition operator
//
const bcd
bcd::operator+(const bcd& p_value) const
{
return Add(p_value);
}
const bcd
bcd::operator+(const int p_value) const
{
return Add(bcd(p_value));
}
const bcd
bcd::operator+(const double p_value) const
{
return Add(bcd(p_value));
}
const bcd
bcd::operator+(LPCTSTR p_value) const
{
return Add(bcd(p_value));
}
// bcd::-
// Description: Subtraction operator
const bcd
bcd::operator-(const bcd& p_value) const
{
return Sub(p_value);
}
const bcd
bcd::operator-(const int p_value) const
{
return Sub(bcd(p_value));
}
const bcd
bcd::operator-(const double p_value) const
{
return Sub(bcd(p_value));
}
const bcd
bcd::operator-(LPCTSTR p_value) const
{
return Sub(bcd(p_value));
}
// bcd::*
// Description: Multiplication operator
const bcd
bcd::operator*(const bcd& p_value) const
{
return Mul(p_value);
}
const bcd
bcd::operator*(const int p_value) const
{
return Mul(bcd(p_value));
}
const bcd
bcd::operator*(const double p_value) const
{
return Mul(bcd(p_value));
}
const bcd
bcd::operator*(LPCTSTR p_value) const
{
return Mul(bcd(p_value));
}
// bcd::/
// Description: Division operator
const bcd
bcd::operator/(const bcd& p_value) const
{
return Div(p_value);
}
const bcd
bcd::operator/(const int p_value) const
{
return Div(bcd(p_value));
}
const bcd
bcd::operator/(const double p_value) const
{
return Div(bcd(p_value));
}
const bcd
bcd::operator/(LPCTSTR p_value) const
{
return Div(bcd(p_value));
}
// bcd::%
// Description: Modulo operator
const bcd
bcd::operator%(const bcd& p_value) const
{
return Mod(p_value);
}
const bcd
bcd::operator%(const int p_value) const
{
return Mod(bcd(p_value));
}
const bcd
bcd::operator%(const double p_value) const
{
return Mod(bcd(p_value));
}
const bcd
bcd::operator%(LPCTSTR p_value) const
{
return Mod(bcd(p_value));
}
// bcd::operator +=
// Description: Operator to add a bcd to this one
bcd&
bcd::operator+=(const bcd& p_value)
{
*this = Add(p_value);
return *this;
}
bcd&
bcd::operator+=(const int p_value)
{
*this = Add(bcd(p_value));
return *this;
}
bcd&
bcd::operator+=(const double p_value)
{
*this = Add(bcd(p_value));
return *this;
}
bcd&
bcd::operator+=(LPCTSTR p_value)
{
*this = Add(bcd(p_value));
return *this;
}
// bcd::operator -=
// Description: Operator to subtract a bcd from this one
bcd&
bcd::operator-=(const bcd& p_value)
{
*this = Sub(p_value);
return *this;
}
bcd&
bcd::operator-=(const int p_value)
{
*this = Sub(bcd(p_value));
return *this;
}
bcd&
bcd::operator-=(const double p_value)
{
*this = Sub(bcd(p_value));
return *this;
}
bcd&
bcd::operator-=(LPCTSTR p_value)
{
*this = Sub(bcd(p_value));
return *this;
}
// bcd::operator *=
// Description: Operator to multiply a bcd with this one
bcd&
bcd::operator*=(const bcd& p_value)
{
*this = Mul(p_value);
return *this;
}
bcd&
bcd::operator*=(const int p_value)
{
*this = Mul(bcd(p_value));
return *this;
}
bcd&
bcd::operator*=(const double p_value)
{
*this = Mul(bcd(p_value));
return *this;
}
bcd&
bcd::operator*=(LPCTSTR p_value)
{
*this = Mul(bcd(p_value));
return *this;
}
// bcd::operator /=
// Description: Operator to divide a bcd with another
bcd&
bcd::operator/=(const bcd& p_value)
{
*this = Div(p_value);
return *this;
}
bcd&
bcd::operator/=(const int p_value)
{
*this = Div(bcd(p_value));
return *this;
}
bcd&
bcd::operator/=(const double p_value)
{
*this = Div(bcd(p_value));
return *this;
}
bcd&
bcd::operator/=(LPCTSTR p_value)
{
*this = Div(bcd(p_value));
return *this;
}
// bcd::operator %=
// Description: Operator to do a modulo on this one
bcd&
bcd::operator%=(const bcd& p_value)
{
*this = Mod(p_value);
return *this;
}
bcd&
bcd::operator%=(const int p_value)
{
*this = Mod(bcd(p_value));
return *this;
}
bcd&
bcd::operator%=(const double p_value)
{
*this = Mod(bcd(p_value));
return *this;
}
bcd&
bcd::operator%=(LPCTSTR p_value)
{
*this = Mod(bcd(p_value));
return *this;
}
// bd::-
// Description: prefix unary minus (negation)
//
bcd
bcd::operator-() const
{
bcd result(*this);
// Null can never be negative
if(!result.IsZero() && result.IsValid() && !result.IsNULL())
{
// Swap signs
if(result.m_sign == Sign::Positive)
{
result.m_sign = Sign::Negative;
}
else
{
result.m_sign = Sign::Positive;
}
}
return result;
}
// bcd::postfix ++
//
bcd
bcd::operator++(int)
{
// Return result first, than do the add 1
bcd res(*this);
++*this;
return res;
}
// bcd::prefix++
bcd&
bcd::operator++()
{
//++x is equal to x+=1
bcd number_1(1);
*this += number_1;
return *this;
}
// bcd::Postfix decrement
//
bcd
bcd::operator--(int)
{
// Return result first, than do the subtract
bcd res(*this);
--*this;
return res;
}
// bcd::Prefix decrement
bcd&
bcd::operator--()
{
// --x is equal to x-=1
bcd number_1(1);
*this -= number_1;
return *this;
}
// bcd::=
// Description: Assignment operator from another bcd
bcd&
bcd::operator=(const bcd& p_value)
{
if(this != &p_value)
{
m_sign = p_value.m_sign;
m_exponent = p_value.m_exponent;
memcpy(m_mantissa,p_value.m_mantissa,bcdLength * sizeof(long));
}
return *this;
}
// bcd::=
// Description: Assignment operator from a long
bcd&
bcd::operator=(const int p_value)
{
SetValueLong(p_value,0);
return *this;
}
// bcd::=
// Description: Assignment operator from a double
bcd&
bcd::operator=(const double p_value)
{
SetValueDouble(p_value);
return *this;
}
// bcd::=
// Description: Assignment operator from a string
bcd&
bcd::operator=(const PCTSTR p_value)
{
SetValueString(p_value);
return *this;
}
// bcd::=
// Description: Assignment operator from an __int64
bcd&
bcd::operator=(const __int64 p_value)
{
SetValueInt64(p_value,0);
return *this;
}
// bcd::operator==
// Description: Equality comparison of two bcd numbers
//
bool
bcd::operator==(const bcd& p_value) const
{
// Shortcut: the same number is equal to itself
if(this == &p_value)
{
return true;
}
// Signs must be equal
if(m_sign != p_value.m_sign)
{
return false;
}
// Exponents must be equal
if(m_exponent != p_value.m_exponent)
{
return false;
}
// Mantissa's must be equal
for(int ind = 0;ind < bcdLength; ++ind)
{
if(m_mantissa[ind] != p_value.m_mantissa[ind])
{
return false;
}
}
// Everything is equal
return true;
}
bool
bcd::operator==(const int p_value) const
{
bcd value(p_value);
return *this == value;
}
bool
bcd::operator==(const double p_value) const
{
bcd value(p_value);
return *this == value;
}
bool
bcd::operator==(LPCTSTR p_value) const
{
bcd value(p_value);
return *this == value;
}
// bcd::operator!=
// Description: Inequality comparison of two bcd numbers
//
bool
bcd::operator!=(const bcd& p_value) const
{
// (x != y) is equal to !(x == y)
return !(*this == p_value);
}
bool
bcd::operator!=(const int p_value) const
{
bcd value(p_value);
return !(*this == value);
}
bool
bcd::operator!=(const double p_value) const
{
bcd value(p_value);
return !(*this == value);
}
bool
bcd::operator!=(LPCTSTR p_value) const
{
bcd value(p_value);
return !(*this == value);
}
bool
bcd::operator<(const bcd& p_value) const
{
// Check if we can do a comparison
// Infinity compares to nothing!!
if(!IsValid() || !p_value.IsValid() || IsNULL() || p_value.IsNULL())
{
return false;
}
// Shortcut: Negative numbers are smaller than positive ones
if(m_sign != p_value.m_sign)
{
// Signs are not equal
// If this one is negative "smaller than" is true
// If this one is positive "smaller than" is false
return (m_sign == Sign::Negative);
}
// Issue #2 at github
// Zero is always smaller than everything else
if(IsZero() && !p_value.IsZero())
{
return (m_sign == Sign::Positive);
}
// Shortcut: If the exponent differ, the mantissa's don't matter
if(m_exponent != p_value.m_exponent)
{
if(m_exponent < p_value.m_exponent)
{
return (m_sign == Sign::Positive);
}
else
{
return (m_sign == Sign::Negative);
}
}
// Signs are the same and exponents are the same
// Now compare the mantissa
for(int ind = 0;ind < bcdLength; ++ind)
{
// Find the first position not equal to the other
if(m_mantissa[ind] != p_value.m_mantissa[ind])
{
// Result by comparing the mantissa positions
return (m_mantissa[ind] < p_value.m_mantissa[ind]);
}
}
// Numbers are exactly the same
return false;
}
bool
bcd::operator<(const int p_value) const
{
bcd value(p_value);
return *this < value;
}
bool
bcd::operator<(const double p_value) const
{
bcd value(p_value);
return *this < value;
}
bool
bcd::operator<(LPCTSTR p_value) const
{
bcd value(p_value);
return *this < value;
}
bool
bcd::operator>(const bcd& p_value) const
{
// Check if we can do a comparison
// Infinity compares to nothing!!
if(!IsValid() || !p_value.IsValid() || IsNULL() || p_value.IsNULL())
{
return false;
}
// Shortcut: Negative numbers are smaller than positive ones
if(m_sign != p_value.m_sign)
{
// Signs are not equal.
// If this one is positive "greater than" is true
// If this one is negative "greater than" is false
return (m_sign == Sign::Positive);
}
// Shortcut: if value is zero
if(IsZero())
{
return (p_value.m_sign == Sign::Negative);
}
// Shortcut: If the exponent differ, the mantissa's don't matter
if(m_exponent != p_value.m_exponent)
{
if(m_exponent > p_value.m_exponent || p_value.IsZero())
{
return (m_sign == Sign::Positive);
}
else
{
return (m_sign == Sign::Negative);
}
}
// Signs are the same and exponents are the same
// Now compare the mantissa
for(int ind = 0;ind < bcdLength; ++ind)
{
// Find the first position not equal to the other
if(m_mantissa[ind] != p_value.m_mantissa[ind])
{
// Result by comparing the mantissa positions
return (m_mantissa[ind] > p_value.m_mantissa[ind]);
}
}
// Numbers are exactly the same
return false;
}
bool
bcd::operator>(const int p_value) const
{
bcd value(p_value);
return *this > value;
}
bool
bcd::operator>(const double p_value) const
{
bcd value(p_value);
return *this > value;
}
bool
bcd::operator>(LPCTSTR p_value) const
{
bcd value(p_value);
return *this > value;
}
bool
bcd::operator<=(const bcd& p_value) const
{
// (x <= y) equals !(x > y)
return !(*this > p_value);
}
bool
bcd::operator<=(const int p_value) const
{
bcd value(p_value);
return !(*this > value);
}
bool
bcd::operator<=(const double p_value) const
{
bcd value(p_value);
return !(*this > value);
}
bool
bcd::operator<=(LPCTSTR p_value) const
{
bcd value(p_value);
return !(*this > value);
}
bool
bcd::operator>=(const bcd& p_value) const
{
// (x >= y) equals !(x < y)
return !(*this < p_value);
}