-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIcd.cpp
3531 lines (3231 loc) · 84.3 KB
/
Icd.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
// ICD = INTEGER CODED DECIMAL
// A class to work with large decimal numbers
// in exact precisions in decimals as opposed to
// the built in data types (float, double) that
// do a binary approximation
//
#include "Stdafx.h"
#include <math.h> // Needed for conversions and estimates
#include <float.h> // Needed for estimates
#include <limits.h> // For max sizes of int, long and int64
#include "Icd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Undefine to test blackjetrock code
// #define SLOW_SQUAREROOT
//////////////////////////////////////////////////////////////////////////
//
// ICD CONSTRUCTORS / DESTRUCTORS
//
//////////////////////////////////////////////////////////////////////////
// Icd::Icd
// Description: default constructor
// What it does: Initializes the icd to 0.0
//
icd::icd()
{
MakeEmpty();
}
// Icd::Icd
// Description: copy constructor
// Parameters: const icd&
// What it does: copies all data members
//
icd::icd(const icd& p_icd)
{
m_sign = p_icd.m_sign;
m_length = p_icd.m_length;
m_precision = p_icd.m_precision;
memcpy_s(m_data,sizeof(long) * icdLength,p_icd.m_data,sizeof(long) * icdLength);
}
// Icd::Icd
//
// Description: copy constructor with one or two longs (32bits)
// Parameters: const long value // value before the '.'
// const long remainder // value behind the '.'
// What it does: Uses SetValue long to initialize the icd
//
icd::icd(const long value, const long remainder)
{
SetValueLong(value,remainder);
}
// Icd::Icd
//
// Description: copy constructor with one or two unsigned longs (32bits)
// Parameters: const unsigned long value // value before the '.'
// const unsigned long remainder // value behind the '.'
// What it does: Uses SetValue long to initialize the icd
//
icd::icd(const ulong value, const ulong remainder /*= 0*/)
{
SetValueULong(value,remainder);
}
// Icd::Icd
// Copy constructor with an int64 (64 bits) and an optional remainder (64 bit)
icd::icd(const int64 value, const int64 remainder)
{
SetValueInt64(value, remainder);
}
// Icd::Icd
// Copy constructor with an unsigned int64 (64 bits) and an optional remainder (64 bit)
icd::icd(const UINT64 value, const UINT64 remainder /*= 0*/)
{
SetValueUInt64(value, remainder);
}
// Icd::Icd
// Description: copy constructor from a double. Beware for rounding errors
// Parameters: const double value
// What it does: Uses SetValueDouble
//
icd::icd(const double value)
{
SetValueDouble(value);
}
// Icd::Icd
// Description: copy constructor from a string
// Parameters: const String& // String to be copied
// bool fromDB // True if the value came from a database
// What it does: Uses SetValueString
//
icd::icd(const CString& str)
{
SetValueString(str);
}
// ICD of a byte/char
icd::icd(const char value)
{
SetValueLong((long)value,0L);
}
// ICD of a byte/char
icd::icd(const uchar value)
{
SetValueULong((ulong)value, 0L);
}
// ICD of a short
icd::icd(const short value)
{
SetValueLong((long)value, 0L);
}
// ICD of a short
icd::icd(const ushort value)
{
SetValueLong((long)value, 0L);
}
// Icd::Icd
// Description: copy constructor from a database NUMERIC
// Parameters p_numeric // Pointer to ODBC structure
//
icd::icd(SQL_NUMERIC_STRUCT* p_numeric)
{
SetValueNumeric(p_numeric);
}
// Icd::~Icd
// Description: destructor
// What it does: Does nothing (yet)
//
icd::~icd()
{
}
//////////////////////////////////////////////////////////////////////////
//
// END ICD CONSTRUCTORS / DESTRUCTORS
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// ICD CONSTANTS
//
//////////////////////////////////////////////////////////////////////////
// Icd::PI
// Description: Ratio between the radius and the circumference of a circle
// What it does: Natural constant that never changes
icd
icd::PI()
{
icd pi;
pi.m_length = icdLength * icdDigits;
pi.m_precision = icdLength * icdDigits / 2;
pi.m_sign = Positive;
// PI in 40 decimals
// +3.14159265_35897932_38462643_38327950_28841972
pi.m_data[icdPointPosition ] = 3;
pi.m_data[icdPointPosition - 1] = 14159265L;
pi.m_data[icdPointPosition - 2] = 35897932L;
pi.m_data[icdPointPosition - 3] = 38462643L;
pi.m_data[icdPointPosition - 4] = 38327950L;
pi.m_data[icdPointPosition - 5] = 28841972L;
return pi;
}
// Icd::LN2
// Description: Natural logarithm of two
// What it does: Mathematical constant that never changes
icd
icd::LN2()
{
icd ln2;
ln2.m_length = icdLength * icdDigits;
ln2.m_precision = icdLength * icdDigits / 2;
ln2.m_sign = Positive;
// LN2 in 40 decimals
// +0.69314718_05599453_09417232_12145817_65680756
ln2.m_data[icdPointPosition ] = 0;
ln2.m_data[icdPointPosition - 1] = 69314718L;
ln2.m_data[icdPointPosition - 2] = 5599453L;
ln2.m_data[icdPointPosition - 3] = 9417232L;
ln2.m_data[icdPointPosition - 4] = 12145817L;
ln2.m_data[icdPointPosition - 5] = 65680756L;
return ln2;
}
// Icd::LN10
// Description: Natural logarithm of ten
// What it does: Mathematical constant that never changes
icd
icd::LN10()
{
icd ln10;
ln10.m_length = icdLength * icdDigits;
ln10.m_precision = icdLength * icdDigits / 2;
ln10.m_sign = Positive;
// LN10 in 40 decimals
// +2.30258509_29940456_84017991_45468436_42076019
ln10.m_data[icdPointPosition ] = 2;
ln10.m_data[icdPointPosition - 1] = 30258509L;
ln10.m_data[icdPointPosition - 2] = 29940456L;
ln10.m_data[icdPointPosition - 3] = 84017991L;
ln10.m_data[icdPointPosition - 4] = 45468436L;
ln10.m_data[icdPointPosition - 5] = 42076019L;
return ln10;
}
//////////////////////////////////////////////////////////////////////////
//
// END ICD CONSTANTS
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// ICD OPERATORS
//
//////////////////////////////////////////////////////////////////////////
// Icd::operator=
// Description: assignment from an icd
// Parameters: const icd&
// What it does: If it is not us, copy the icd
//
const icd&
icd::operator=(const icd& p_icd)
{
if (this != &p_icd)
{
m_sign = p_icd.m_sign;
m_length = p_icd.m_length;
m_precision = p_icd.m_precision;
for (long i = 0; i < icdLength; i++)
{
m_data[i] = p_icd.m_data[i];
}
}
return *this;
}
// Icd::operator=
//
// Description: assignment from a string
// Parameters: const String&
// What it does: Uses SetValueString to get the string in the icd
//
const icd&
icd::operator=(const CString& str)
{
SetValueString(str);
return *this;
}
// Icd::operator=
// Description: assignment from a long
// Parameters: const long
// What it does: Uses SetValueLong to get the long in the icd
//
const icd&
icd::operator=(const long value)
{
SetValueLong(value, 0);
return *this;
}
// Icd::operator=
// Description: assignment from a double
// Parameters: const double
// What it does: Uses SetValueDouble to get the double in the icd
//
const icd&
icd::operator=(const double value)
{
SetValueDouble(value);
return *this;
}
// Icd::operator+=
// Description: Adds the icd and returns the result
// What it does: Uses the + operator and the = operator. (x+=y equivalent to x=x+y)
//
const icd&
icd::operator+=(const icd& p_icd)
{
// x+=y is equal to: x=x+y
*this = *this + p_icd;
return *this;
}
// Icd::operator-=
// Description: Subtracts the icd and returns the result
// What it does: Uses the - operator and the = operator. (x-=y equivalent to x=x-y)
//
const icd&
icd::operator-=(const icd& p_icd)
{
// x-=y is the same as: x=x-y
*this = *this - p_icd;
return *this;
}
// Icd::operator*=
// Description: Multiplies the icd to this one and returns the result
// What it does: Uses the Mul operation
//
const icd&
icd::operator*=(const icd& p_icd)
{
*this = Mul(p_icd);
return *this;
}
// Icd::operator/=
// Description: Divide this icd with the given icd and returns the result
// What it does: Uses the Div operation
//
const icd&
icd::operator/=(const icd& p_icd)
{
*this = Div(p_icd);
return *this;
}
const icd&
icd::operator%=(const icd& p_icd)
{
*this = Mod(p_icd);
return *this;
}
// Icd::operator- (Unary)
// Description: prefix - operator (negation)
// What it does: Copies the icd and then negates the result, if not zero
//
const icd
icd::operator-() const
{
icd number(*this);
// If null, do not change the sign, otherwise flip it
if (!number.IsNull())
{
number.m_sign = (number.m_sign == Positive) ? Negative : Positive;
}
return number;
}
// Icd::operator++
// Description: postfix ++ operator
// What it does: Uses the prefix ++ and the copy constructor
//
const icd
icd::operator++(int)
{
// postfix ++ operator: calculate the result first, do the addition afterward
icd res(*this);
++*this;
return res;
}
// Icd::operator++
// Description: prefix ++ operator
// What it does: Adds 1 (one) and returns the result
//
icd&
icd::operator++()
{
//++x is equivalent to: x+=1
icd number_1(1L, 0);
*this += number_1;
return *this;
}
// Icd::operator--
// Description: postfix -- operator
// What it does Uses the prefix -- and returns the result
//
const icd
icd::operator--(int)
{
// postfix -- operator: calculate result first, subtract later
icd res(*this);
--*this;
return res;
}
// Icd::operator--
// Description: prefix -- operator
// What it does: Subtract 1 (one) and return this
//
icd&
icd::operator--()
{
// --x is same as x-=1
icd number_1(1L);
*this -= number_1;
return *this;
}
// Icd::operator==
// Description: comparison operator is-equal
// What it does: true if sign and all m_data are equal
//
const bool
icd::operator==(const icd& p_icd) const
{
// Only equal is signs are equal
bool isSame = m_sign == p_icd.m_sign;
if(isSame)
{
// Same if all m_data are equal
for(long i = 0; isSame && i < icdLength; i++)
{
isSame = m_data[i] == p_icd.m_data[i];
}
}
return isSame;
}
// Icd::operator!=
// Description: Comparison operator not-equal
// What it does: Uses the inversion of the equality operator
//
const bool
icd::operator!=(const icd& p_icd) const
{
// x!=y is equivalent to !(x==y)
return !(*this == p_icd);
}
// Icd::operator<
// Description: Comparison operator smaller-than
// What it does: If the left side is negative, and the right side positive, already true
// Otherwise: test the m_data for the inequality
//
const bool
icd::operator<(const icd& p_icd) const
{
// Signs are the same
bool isSame = (m_sign == p_icd.m_sign);
// Smaller if the left side is negative, and the right side positive
bool isSmaller = (m_sign == Negative && p_icd.m_sign == Positive);
// Smaller as long as number left is smaller than number right
for (long i = icdLength - 1; i >= 0 && isSame; i--)
{
if (m_data[i] != p_icd.m_data[i])
{
isSame = false;
isSmaller = m_data[i] < p_icd.m_data[i];
break;
}
}
// If both signs are negative and not the same, invert the result
// Example: -5<-3 is equal to 5>3
// -5<-5 is equal to 5<5
if (m_sign == Negative && p_icd.m_sign == Negative && !isSame)
{
isSmaller = !isSmaller;
}
return isSmaller;
}
// Icd::operator>
// Description: Comparison operator greater-than
// What it does: Already greater if left side is positive and right side negative
// Otherwise test the m_data structure
//
const bool
icd::operator>(const icd& p_icd) const
{
// Equality of the signs
bool isSame = (m_sign == p_icd.m_sign);
// Greater if left side is positive and right side not
bool isGreater = (m_sign == Positive && p_icd.m_sign == Negative);
// Test the m_data structure
for (long i = icdLength - 1; i >= 0 && isSame; i--)
{
if (m_data[i] != p_icd.m_data[i])
{
isSame = false;
isGreater = m_data[i] > p_icd.m_data[i];
break;
}
}
// If both sides are negative and not the same, invert the result
// Example: -5>-3 is same as 5<3
// -5>-5 is same as 5>5
if (m_sign == Negative && p_icd.m_sign == Negative && !isSame)
{
isGreater = !isGreater;
}
return isGreater;
}
// Icd::operator<=
// Description: Comparison operator smaller-than-or-equal-to
// What it does: Uses the inversion of the greater-than operator
//
const bool
icd::operator<=(const icd& p_icd) const
{
// x<=y is equivalent to !(x>y)
return !(*this>p_icd);
}
// Icd::operator>=
// Description: Comparison operator greater-than-or-equal-to
// What it does: Uses the inversion of the smaller-than operator
//
const bool
icd::operator>=(const icd& p_icd) const
{
// x>=y is equivalent to !(x<y)
return !(*this < p_icd);
}
// operator<<
// Description: standard output operator
// What it does: Uses the string 'AsString' method to serialize an icd
//
ostream& operator<<(ostream& os, const icd& p_icd)
{
os << p_icd.AsString().operator LPCTSTR();
return os;
}
//////////////////////////////////////////////////////////////////////////
//
// END ICD OPERATORS
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// MATHEMATICAL ICD FUNCTIONS
//
//////////////////////////////////////////////////////////////////////////
// Icd::Floor
// Description: First natural number not greater than the input
// What it does: Remove the decimal part and subtract 1 for negative numbers
//
icd
icd::Floor() const
{
icd res;
res.m_sign = m_sign;
for (int n = icdPointPosition; n < icdLength; n++)
{
res.m_data[n] = m_data[n];
}
if (m_sign == Negative)
{
for (int n = 0; n < icdPointPosition; n++)
{
if(m_data[n])
{
// ICD not zero behind the decimal point, and it's a negative number
// so the 'floored' version is the truncation MINUS ONE!
--res;
break;
}
}
}
return res;
}
// Icd::Ceiling
// Description: First natural number greater than the input
// What it does: Remove decimal part and add 1 for negative numbers
//
icd
icd::Ceil() const
{
icd res;
res.m_sign = m_sign;
for (int n = icdPointPosition; n < icdLength; n++)
{
res.m_data[n] = m_data[n];
}
if (m_sign == Positive)
{
for (int n = 0; n < icdPointPosition; n++)
{
if (m_data[n])
{
// ICD was non-zero behind the decimal point
// so the 'ceiled' version is truncated PLUS ONE!
++res;
break;
}
}
}
return res;
}
// Icd::sqrt
// Description: Square root of the number
// What it does: Does an estimation through a double and then
// Uses the Newton estimation to calculate the root
#ifdef SLOW_SQUAREROOT
icd
icd::SquareRoot() const
{
icd number(0L, 0L);
icd half(("0.5"));
icd two(2L);
icd three(3L);
int sqrti = 0;
// Optimization: sqrt(0) = 0
if(IsNull())
{
return number;
}
#if DEBUG_SQRT
printf("\nSQRT:Number:%s", number.AsString().c_str());
#endif
// Getting the breaking criterion
icd epsilon = Epsilon(10);
number = *this; // Number to get the square-root from
if (number.GetSign() == -1)
{
throw new std::string(("Cannot calculate a square-root from a negative number"));
}
#if DEBUG_SQRT
printf("\nNumber now:%s", number.AsString().c_str());
#endif
// First estimate
double estimate1 = number.AsDouble() / 2;
double estimate2 = 1 / sqrt(estimate1);
icd result(estimate1);
icd between;
// Newton's iteration
icd last_result("0.0");
while (true)
{
result = (result + number / result) / two;
between = last_result - result;
if (between.AbsoluteValue() < epsilon)
{
break;
}
last_result = result;
#if DEBUG_SQRT
printf("\nNumber now:%s", number.AsString().c_str());
printf("\nBetween: %s", between.AsString().c_str());
printf("\nResult: %s", result.AsString().c_str());
#endif
}
return result;
}
#else
icd
icd::SquareRoot() const
{
icd number(0L,0L);
icd half(_T("0.5"));
icd two(2L);
icd three(3L);
// Optimization: sqrt(0) = 0
if(IsNull())
{
return number;
}
// Getting the breaking criterion
icd epsilon = Epsilon(10);
number = *this; // Number to get the square-root from
if(number.GetSign() == -1)
{
throw CString(_T("Cannot calculate a square-root from a negative number"));
}
// Reduction by dividing through the square of a whole number
// For speed we use the powers of two
icd reduction(1L);
icd hundred(100L);
while(number / (reduction * reduction) > hundred)
{
reduction *= two;
}
// Reduction by dividing through the square of the reduction
// 'reduction' is in fact sqrt(reduction)
number /= (reduction * reduction);
// First estimate
double estimate1 = number.AsDouble();
double estimate2 = 1 / sqrt(estimate1);
icd result(estimate2);
icd between;
// Newton's iteration
// Un = U(3-VU^2)/2
while(true)
{
between = number * result * result; // VU^2
between = three - between; // 3-VU^2
between *= half; // (3-VU^2)/2
if(between.ValueBehindPoint() < epsilon)
{
break;
}
result *= between;
}
// Final result by calculating number * 1/root
result *= number;
// Adding the reduction by multiplying
result *= reduction;
return result;
}
#endif
// Icd::Power
// Description: icd to the power of another icd
// What it does: x^y = exp(y * ln(x))
icd
icd::Power(const icd& p_icd) const
{
icd result;
result = this->Log() * p_icd;
result = result.Exp();
return result;
}
// Icd::Abs
// Description: Returning the absolute value
// What it does: Setting the sign to positive
icd
icd::AbsoluteValue() const
{
icd icd2 = *this;
icd2.m_sign = Sign::Positive;
return icd2;
}
// Icd::Reciproke
// Description: Inverse of the number = 1 / number
// What it does: Using the standard Div
//
icd
icd::Reciproke() const
{
icd number(1L);
number = number.Div(*this);
return number;
}
// Icd::Log
// Description: Natural Logarithm
// What it does: Use a Taylor series until their is no more change in the result
// Equivalent with the same standard C function call
// ln(x) == 2( z + z^3/3 + z^5/5 ...
// z = (x-1)/(x+1)
//
icd
icd::Log() const
{
long k;
long expo = 0;
icd res, number, z2;
icd number10(10L);
icd fast(_T("1.2"));
icd one(1L);
icd epsilon = Epsilon(5);
if(*this <= icd(0L))
{
throw CString(_T("Cannot get a natural logarithm from a number <= 0"));
}
// Bringing the number under 10 and save the exponent
number = *this;
while(number > number10)
{
number /= number10;
++expo;
}
// In order to get a fast Taylor series result we need to get the fraction closer to one
// The fraction part is [1.xxx-9.999] (base 10) OR [1.xxx-255.xxx] (base 256) at this point
// Repeat a series of square root until 'number' < 1.2
for(k = 0; number > fast; k++)
{
number = sqrt(number);
}
// Calculate the fraction part now at [1.xxx-1.1999]
number = (number - one) / (number + one);
z2 = number * number;
res = number;
// Iterate using taylor series ln(x) == 2( z + z^3/3 + z^5/5 ... )
icd tussen;
for(long stap = 3; ;stap += 2)
{
number *= z2;
tussen = number / icd(stap);
// Breaking criterion
if(tussen.AbsoluteValue() < epsilon)
{
break;
}
res += tussen;
}
// Adding the powers of 2 again (came from " < 1.2")
res *= icd(pow(2.0,(double)(k + 1)));
// Adding the exponent again
if(expo != 0)
{
// Ln(x^y) = Ln(x) + Ln(10^y) = Ln(x) + y * ln(10)
res += icd(expo) * icd::LN10();
}
return res;
}
// Icd::Exp
// Description: Exponent E till the power of this icd
// What it does: Use a Taylor series until their is no more change in the result
// exp(x) == 1 + x + x^2/2!+x^3/3!+....
// Equivalent with the same standard C function call
//
icd
icd::Exp() const
{
long step, k = 0;
long expo;
icd between, result, number;
icd half(_T("0.5"));
icd ten(10L);
icd epsilon = Epsilon(5);
number = *this;
if( number.GetSign() < 0 )
{
number = -number;;
}
for( k = 0; number > half; )
{
expo = number.Exponent();
if( expo > 0 )
{
step = 3 * min( 10, expo ); // 2^3
result = icd((long) (1 << step) );
result = result.Reciproke();
k += step;
}
else
{
result = half;
k++;
}
number *= result;
}
// Do first two iterations
result = icd(1L) + number;
between = number * number * half;
result += between;
// Now iterate
for(step = 3; ;step++)
{
between *= number / icd(step);
// Breaking criterion
if(between < epsilon)
{
break;
}
result += between;
}
// Adding powers of itself again
for( ; k > 0; k-- )
{
result *= result;
}
// Correcting the sign
if(this->GetSign() < 0 )
{
result = icd((long)1) / result;
}
return result;
}
// Icd::Log10
// Description: Logarithm on base 10
// What it does: log10 = ln(x) / ln(10);
icd
icd::Log10() const
{
icd res(0L);
if(GetSign() <= 0)
{
throw CString(_T("Cannot calculate a logarithm from a number <= 0"));
}
res = *this;
res = res.Log() / LN10();
return res;
}
// Ten Power
// Description: icd . 10^n
// Technical: add n to the exponent of the number
icd
icd::TenPower(int n)
{
// Check if we can do this
if (IsNull())
{
return icd();
}
icd res = *this;
if(n > 0)
{
for (int ind = 0;ind < n;++ind)
{
res.Mult10();
}
}
else if (n < 0)
{
for (int ind = 0;ind < n;++ind)
{
res.Div10();
}
}
return res;
}
//////////////////////////////////////////////////////////////////////////
//
// END ICD MATHEMATICAL FUNCTIONS
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//
// ICD TRIGONOMETRIC FUNCTIONS
//
//////////////////////////////////////////////////////////////////////////
// Icd::Sine
// Description: Sine of an angle
// What it does: Use the Taylor series: Sin(x) = x - x^3/3! + x^5/5! ...
// 1) Reduce x first to in between 0..2*PI
// 2) Reduce further until x between 0..PI by means of sin(x+PI) = -Sin(x)
// 3) Do the Taylor expansion series
// This reductions are necessary to speed up the Taylor expansion and
// keep the rounding errors under control
//
icd