comparison src/decimal/decimal.d @ 3:bfda0b347c07

Initial development of BCD integers
author Paul (paul.d.anderson@comcast.net)
date Thu, 18 Mar 2010 18:09:20 -0700
parents a984d3056cc4
children
comparison
equal deleted inserted replaced
2:abf9d7974708 3:bfda0b347c07
2040 // only addend is infinite 2040 // only addend is infinite
2041 if (addend.isInfinite) { 2041 if (addend.isInfinite) {
2042 return addend; 2042 return addend;
2043 } 2043 }
2044 2044
2045 // writeln("aligning");
2045 // align the operands 2046 // align the operands
2046 alignOps(augend, addend); 2047 alignOps(augend, addend);
2047 2048
2048 // add(0, 0) 2049 // add(0, 0)
2049 if (augend.isZero && addend.isZero) { 2050 if (augend.isZero && addend.isZero) {
2056 // (before rounding) 2057 // (before rounding)
2057 sum.clear(); 2058 sum.clear();
2058 2059
2059 // if operands have the same sign... 2060 // if operands have the same sign...
2060 if (augend.sign == addend.sign) { 2061 if (augend.sign == addend.sign) {
2062 // writeln("same sign");
2063 // writeln("augend = ", augend.ceff);
2064 // writeln("addend = ", addend.ceff);
2061 sum.ceff = augend.ceff + addend.ceff; 2065 sum.ceff = augend.ceff + addend.ceff;
2066 // writeln("sum = ", sum.ceff);
2062 sum.sign = augend.sign; 2067 sum.sign = augend.sign;
2068 // writeln("set sign");
2063 } 2069 }
2064 // ...else operands have different signs 2070 // ...else operands have different signs
2065 else { 2071 else {
2072 // writeln("signs differ");
2066 sum.ceff = augend.ceff - addend.ceff; 2073 sum.ceff = augend.ceff - addend.ceff;
2067 sum.sign = augend.sign; 2074 sum.sign = augend.sign;
2068 if (sum.ceff < BIG_ZERO) { 2075 if (sum.ceff < BIG_ZERO) {
2069 sum.ceff = -sum.ceff; 2076 sum.ceff = -sum.ceff;
2070 sum.sign = !sum.sign; 2077 sum.sign = !sum.sign;
2072 } 2079 }
2073 // set the number of digits and the exponent 2080 // set the number of digits and the exponent
2074 sum.digits = numDigits(sum.ceff, augend.digits); 2081 sum.digits = numDigits(sum.ceff, augend.digits);
2075 sum.expo = augend.expo; 2082 sum.expo = augend.expo;
2076 2083
2084 // writeln("rounding");
2077 // round the result 2085 // round the result
2078 round(sum); 2086 round(sum);
2087 // writeln("rounded");
2079 return sum; 2088 return sum;
2080 } // end add(augend, addend) 2089 } // end add(augend, addend)
2081 2090
2082 /** 2091 /**
2083 * Subtracts two Decimal numbers. 2092 * Subtracts two Decimal numbers.
3222 assert(numDigits(big) == str.length); 3231 assert(numDigits(big) == str.length);
3223 } 3232 }
3224 writeln("passed"); 3233 writeln("passed");
3225 } 3234 }
3226 3235
3227 public void main() { 3236
3228 writeln("Hello, world!");
3229 /+ writeln("eTiny = ", context.eTiny);
3230 writeln("tiny min = ", Decimal(1, context.eTiny));
3231 writeln("tiny min = ", Decimal(1, context.eTiny - 1));
3232 writeln("max = ", context.max());
3233 writeln("max1 = ", Decimal(999999999, 99));
3234 writeln("dig = ", context.dig());
3235 writeln("eps = ", context.epsilon());
3236 writeln("smallest = ", context.min_normal()*context.epsilon());
3237 writeln("1/epsilon = ", Decimal(1)/context.epsilon());
3238 writeln("max * min = ", context.max * context.min_normal);
3239 writeln("mant_dig = ", context.mant_dig);
3240 writeln("min_exp = ", context.min_exp);
3241 writeln("max_exp = ", context.max_exp);+/
3242
3243
3244 // TODO: this next one goes crazy -- shows need for checks on construction
3245 // TODO: turns out this is being converted to a double (or real) and
3246 // then it's really weird.
3247 // writeln("bigger = ", Decimal(999999999999));
3248 // float f = float.max;
3249 // TODO: convert these to assserts
3250 /+ writeln("f.max = ", f);
3251 writeln("f.min = ", float.min);
3252 writeln("f = ", 2 * float.min);
3253 writeln("d.max = ", Decimal.max());
3254 writeln("d.min = ", Decimal.min_normal());
3255 writeln("2 * d.min = ", 2 * Decimal.min_normal());
3256 writeln("0.1 * d.min = ", 0.1 * Decimal.min_normal());+/
3257 // TODO: move this to unittesting
3258 /+ Decimal dec = Decimal(PI);
3259 writeln("pi = ", dec );
3260 dec = Decimal(PI, 19);
3261 writeln("pi = ", dec );
3262 dec = Decimal(1.3, 25);
3263 writeln("pi = ", dec );
3264 dec = Decimal(0.1, 25);
3265 writeln("pi = ", dec );
3266 dec = Decimal(2.0, 25);
3267 writeln("pi = ", dec );
3268 dec = Decimal(200.5, 25);
3269 writeln("pi = ", dec );
3270 writeln(double.dig);
3271 writeln(real.dig);+/
3272 }
3273