Mercurial > projects > decimal
comparison src/decimal/test.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 | |
children |
comparison
equal
deleted
inserted
replaced
3:bfda0b347c07 | 4:b37c218c1442 |
---|---|
1 /** | |
2 * A D programming language implementation of the | |
3 * General Decimal Arithmetic Specification, | |
4 * Version 1.70, (25 March 2009). | |
5 * | |
6 * by Paul D. Anderson | |
7 * | |
8 * Boost Software License - Version 1.0 - August 17th, 2003 | |
9 * | |
10 * Permission is hereby granted, free of charge, to any person or organization | |
11 * obtaining a copy of the software and accompanying documentation covered by | |
12 * this license (the "Software") to use, reproduce, display, distribute, | |
13 * execute, and transmit the Software, and to prepare derivative works of the | |
14 * Software, and to permit third-parties to whom the Software is furnished to | |
15 * do so, all subject to the following: | |
16 * | |
17 * The copyright notices in the Software and this entire statement, including | |
18 * the above license grant, this restriction and the following disclaimer, | |
19 * must be included in all copies of the Software, in whole or in part, and | |
20 * all derivative works of the Software, unless such copies or derivative | |
21 * works are solely in the form of machine-executable object code generated by | |
22 * a source language processor. | |
23 * | |
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
27 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
28 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
29 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
30 * DEALINGS IN THE SOFTWARE. | |
31 **/ | |
32 module decimal.test; | |
33 | |
34 import decimal.decimal; | |
35 import decimal.math; | |
36 | |
37 public void main2() { | |
38 Decimal ONE = Decimal(1); | |
39 writeln("1 = ", ONE); | |
40 Decimal TWO = Decimal(2); | |
41 writeln("2 = ", TWO); | |
42 Decimal HALF = Decimal(0.5); | |
43 writeln("0.5 = ", HALF); | |
44 Decimal x = sqrt(TWO); | |
45 writeln("x = ", x); | |
46 Decimal y = sqrt(x); | |
47 writeln("y = ", y); | |
48 Decimal p = TWO + x; | |
49 writeln("p = ", p); | |
50 x = y; | |
51 int i = 0; | |
52 while (true) { | |
53 writeln("i = ", i); | |
54 x = x + ONE/x; | |
55 writeln("step 0"); | |
56 // x = HALF (x + ONE/x); | |
57 x *= HALF; | |
58 writeln("step 1"); | |
59 Decimal np = p * ((x + ONE)/(y + ONE)); | |
60 writeln("step 2"); | |
61 writeln("np = ", np); | |
62 if (p == np) return p; | |
63 writeln("step 3"); | |
64 p = np; | |
65 writeln("step 4"); | |
66 x = sqrt(x); | |
67 writeln("step 5"); | |
68 // writeln("x = ", x); | |
69 Decimal oox = ONE/x; | |
70 // writeln("ONE/x = ", oox); | |
71 // writeln ("x + ONE/x = ", x + oox); | |
72 // Decimal t1 = oox + x; | |
73 // writeln("t1 = ", t1); | |
74 // Decimal t1 = x + oox; // ONE + oox; //x + x; // + ONE/x; | |
75 writeln("step 6"); | |
76 // Decimal t2 = (y * x) + ONE; ///x; //ONE / (y + ONE); | |
77 // y = ONE/x + y * x; | |
78 writeln("step 7"); | |
79 y = (ONE/x + y * x) / (y + ONE); //t1 / t2; | |
80 writeln("step 8"); | |
81 i++; | |
82 // break; | |
83 writeln("i = ", i); | |
84 | |
85 } | |
86 return p; | |
87 } | |
88 | |
89 public void main() { | |
90 writeln("Hello, world!"); | |
91 /+ writeln("eTiny = ", context.eTiny); | |
92 writeln("tiny min = ", Decimal(1, context.eTiny)); | |
93 writeln("tiny min = ", Decimal(1, context.eTiny - 1)); | |
94 writeln("max = ", context.max()); | |
95 writeln("max1 = ", Decimal(999999999, 99)); | |
96 writeln("dig = ", context.dig()); | |
97 writeln("eps = ", context.epsilon()); | |
98 writeln("smallest = ", context.min_normal()*context.epsilon()); | |
99 writeln("1/epsilon = ", Decimal(1)/context.epsilon()); | |
100 writeln("max * min = ", context.max * context.min_normal); | |
101 writeln("mant_dig = ", context.mant_dig); | |
102 writeln("min_exp = ", context.min_exp); | |
103 writeln("max_exp = ", context.max_exp);+/ | |
104 | |
105 | |
106 // TODO: this next one goes crazy -- shows need for checks on construction | |
107 // TODO: turns out this is being converted to a double (or real) and | |
108 // then it's really weird. | |
109 // writeln("bigger = ", Decimal(999999999999)); | |
110 // float f = float.max; | |
111 // TODO: convert these to assserts | |
112 /+ writeln("f.max = ", f); | |
113 writeln("f.min = ", float.min); | |
114 writeln("f = ", 2 * float.min); | |
115 writeln("d.max = ", Decimal.max()); | |
116 writeln("d.min = ", Decimal.min_normal()); | |
117 writeln("2 * d.min = ", 2 * Decimal.min_normal()); | |
118 writeln("0.1 * d.min = ", 0.1 * Decimal.min_normal());+/ | |
119 // TODO: move this to unittesting | |
120 /+ Decimal dec = Decimal(PI); | |
121 writeln("pi = ", dec ); | |
122 dec = Decimal(PI, 19); | |
123 writeln("pi = ", dec ); | |
124 dec = Decimal(1.3, 25); | |
125 writeln("pi = ", dec ); | |
126 dec = Decimal(0.1, 25); | |
127 writeln("pi = ", dec ); | |
128 dec = Decimal(2.0, 25); | |
129 writeln("pi = ", dec ); | |
130 dec = Decimal(200.5, 25); | |
131 writeln("pi = ", dec ); | |
132 writeln(double.dig); | |
133 writeln(real.dig);+/ | |
134 } | |
135 |