Mercurial > projects > decimal
comparison src/decimal/math.d @ 4:b37c218c1442
Initial development of BCD integers
author | Paul (paul.d.anderson@comcast.net) |
---|---|
date | Thu, 18 Mar 2010 18:10:25 -0700 |
parents | a984d3056cc4 |
children |
comparison
equal
deleted
inserted
replaced
3:bfda0b347c07 | 4:b37c218c1442 |
---|---|
32 | 32 |
33 module decimal.math; | 33 module decimal.math; |
34 | 34 |
35 import decimal.decimal; | 35 import decimal.decimal; |
36 import decimal.context; | 36 import decimal.context; |
37 //import std.math; | |
37 | 38 |
38 public: | 39 public: |
39 | 40 |
40 //-------------------------------- | 41 //-------------------------------- |
41 // | 42 // |
42 // CONSTANTS | 43 // CONSTANTS |
43 // | 44 // |
44 //-------------------------------- | 45 //-------------------------------- |
46 | |
47 /+ public Decimal HALF; | |
48 public Decimal ONE = Decimal(1); | |
49 public Decimal ZERO = Decimal(0); | |
50 | |
51 static { | |
52 HALF = Decimal("0.5"); | |
53 }+/ | |
45 | 54 |
46 /** | 55 /** |
47 * Returns the value of e to the default precision. | 56 * Returns the value of e to the default precision. |
48 */ | 57 */ |
49 Decimal e() { | 58 Decimal e() { |
57 Decimal e(uint precision) { | 66 Decimal e(uint precision) { |
58 Decimal result; | 67 Decimal result; |
59 return result; | 68 return result; |
60 } | 69 } |
61 | 70 |
71 /+ | |
62 /** | 72 /** |
63 * Returns the value of pi to the default precision. | 73 * Returns the value of pi to the default precision. |
64 */ | 74 */ |
65 Decimal pi() { | 75 Decimal pi() { |
66 Decimal result; | 76 Decimal ONE = Decimal(1); |
67 return result; | 77 writeln("1 = ", ONE); |
68 } | 78 Decimal TWO = Decimal(2); |
69 | 79 writeln("2 = ", TWO); |
80 Decimal HALF = Decimal(0.5); | |
81 writeln("0.5 = ", HALF); | |
82 Decimal x = sqrt(TWO); | |
83 writeln("x = ", x); | |
84 Decimal y = sqrt(x); | |
85 writeln("y = ", y); | |
86 Decimal p = TWO + x; | |
87 writeln("p = ", p); | |
88 x = y; | |
89 int i = 0; | |
90 while (true) { | |
91 writeln("i = ", i); | |
92 x = HALF * (x + ONE/x); | |
93 writeln("step 1"); | |
94 Decimal np = p * ((x + ONE)/(y + ONE)); | |
95 writeln("step 2"); | |
96 writeln("np = ", np); | |
97 if (p == np) return p; | |
98 writeln("step 3"); | |
99 p = np; | |
100 writeln("step 4"); | |
101 x = sqrt(x); | |
102 writeln("step 5"); | |
103 // writeln("x = ", x); | |
104 Decimal oox = ONE/x; | |
105 // writeln("ONE/x = ", oox); | |
106 // writeln ("x + ONE/x = ", x + oox); | |
107 // Decimal t1 = oox + x; | |
108 // writeln("t1 = ", t1); | |
109 // Decimal t1 = x + oox; // ONE + oox; //x + x; // + ONE/x; | |
110 writeln("step 6"); | |
111 // Decimal t2 = (y * x) + ONE; ///x; //ONE / (y + ONE); | |
112 // y = ONE/x + y * x; | |
113 writeln("step 7"); | |
114 y = (ONE/x + y * x) / (y + ONE); //t1 / t2; | |
115 writeln("step 8"); | |
116 i++; | |
117 // break; | |
118 } | |
119 return p; | |
120 } | |
121 | |
122 unittest { | |
123 write("pi...."); | |
124 writeln("pi = ", pi()); | |
125 } | |
126 +/ | |
70 /** | 127 /** |
71 * Returns the value of pi to the specified precision. | 128 * Returns the value of pi to the specified precision. |
72 */ | 129 */ |
73 Decimal pi(uint precision) {; | 130 Decimal pi(uint precision) {; |
74 Decimal result; | 131 Decimal result; |
75 return result; | 132 return result; |
76 } | 133 } |
77 | 134 |
78 /** | 135 /** |
79 * Returns the square root of the argument to the default precision. | 136 * Returns the square root of the argument to the current precision. |
80 */ | 137 */ |
81 Decimal sqrt(Decimal arg) { | 138 Decimal sqrt(Decimal arg) { |
82 Decimal result; | 139 return sqrt(arg, context.precision); |
83 return result; | 140 } |
84 } | 141 |
142 | |
143 unittest { | |
144 write("sqrt....."); | |
145 Decimal dcm = Decimal(4); | |
146 assert(sqrt(dcm) == Decimal(2)); | |
147 writeln("sqrt(2) = ", sqrt(Decimal(2))); | |
148 } | |
85 | 149 |
86 /** | 150 /** |
87 * Returns the square root of the argument to the specified precision. | 151 * Returns the square root of the argument to the specified precision. |
88 */ | 152 */ |
89 Decimal sqrt(Decimal arg, uint precision) { | 153 Decimal sqrt(Decimal arg, uint precision) { |
90 Decimal result; | 154 pushContext(); |
91 return result; | 155 context.precision = precision + 2; |
156 Decimal HALF = Decimal(0.5); | |
157 Decimal ONE = Decimal(1); | |
158 Decimal x = HALF * (arg + ONE); | |
159 Decimal xp; | |
160 while(true){ | |
161 xp = x; | |
162 x = HALF * (x + arg/x); | |
163 if (x == xp) break; | |
164 } | |
165 popContext(); | |
166 round(xp); | |
167 return xp; | |
92 } | 168 } |
93 | 169 |
94 //-------------------------------- | 170 //-------------------------------- |
95 // | 171 // |
96 // EXPONENTIAL AND LOGARITHMIC FUNCTIONS | 172 // EXPONENTIAL AND LOGARITHMIC FUNCTIONS |
310 Decimal atanh(Decimal arg) { | 386 Decimal atanh(Decimal arg) { |
311 Decimal result; | 387 Decimal result; |
312 return result; | 388 return result; |
313 } | 389 } |
314 | 390 |
315 //-------------------------------- | 391 /+ //-------------------------------- |
316 // | 392 // |
317 // General Decimal Arithmetic Specification Functions | 393 // General Decimal Arithmetic Specification Functions |
318 // | 394 // |
319 //-------------------------------- | 395 //-------------------------------- |
320 | 396 |
551 */ | 627 */ |
552 Decimal compareTotal(Decimal op1, Decimal op2) { | 628 Decimal compareTotal(Decimal op1, Decimal op2) { |
553 Decimal result; | 629 Decimal result; |
554 return result; | 630 return result; |
555 } | 631 } |
556 | 632 +/ |
557 | 633 |
558 | 634 |