mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-12-01 18:58:22 +00:00
Formatting, fixes for CPDecimal/CPDecimalNumber
- CPDecimalRound should return a result in all cases - Fixed test of CPDecimalRound with CPDecimalNoScale
This commit is contained in:
1 parent
117dda3bb5
commit
63ae5848f6
4 files changed
+177
-75
No files matched your search
+106
-50
@@ -117,6 +117,7 @@ function CPDecimalMakeWithString(string, locale)
|
||||
// If yes simply add '?' after integer part group, i.e. ([+\-]?)((?:0|[1-9]\d*)?)
|
||||
// Note: now it accept .01 style.
|
||||
var matches = string.match(/^([+\-]?)((?:0|[1-9]\d*)?)(?:\.(\d*))?(?:[eE]([+\-]?)(\d+))?$/);
|
||||
|
||||
if (!matches)
|
||||
return CPDecimalMakeNaN();
|
||||
|
||||
@@ -127,10 +128,12 @@ function CPDecimalMakeWithString(string, locale)
|
||||
exp = matches[5];
|
||||
|
||||
var isNegative = NO;
|
||||
|
||||
if (ds && ds === "-")
|
||||
isNegative = YES;
|
||||
|
||||
var exponent = 0;
|
||||
|
||||
if (exp)
|
||||
exponent = parseInt(exp) * ((es && es === "-")?-1:1);
|
||||
|
||||
@@ -138,6 +141,7 @@ function CPDecimalMakeWithString(string, locale)
|
||||
exponent -= decpart.length;
|
||||
|
||||
var inputlength = (intpart?intpart.length:0) + (decpart?decpart.length:0);
|
||||
|
||||
if (inputlength > CPDecimalMaxDigits)
|
||||
{
|
||||
// input is too long, increase exponent and truncate
|
||||
@@ -154,17 +158,21 @@ function CPDecimalMakeWithString(string, locale)
|
||||
// Representation internally starts at most significant digit
|
||||
var m = [],
|
||||
i = 0;
|
||||
|
||||
for (; i < (intpart?intpart.length:0); i++)
|
||||
{
|
||||
if (i >= CPDecimalMaxDigits)
|
||||
break; // truncate
|
||||
Array.prototype.push.call(m, parseInt(intpart.charAt(i)));
|
||||
}
|
||||
|
||||
var j = 0;
|
||||
|
||||
for (; j < (decpart?decpart.length:0); j++)
|
||||
{
|
||||
if ((i + j) >= CPDecimalMaxDigits)
|
||||
break; // truncate
|
||||
|
||||
Array.prototype.push.call(m, parseInt(decpart.charAt(j)));
|
||||
}
|
||||
|
||||
@@ -199,7 +207,7 @@ function CPDecimalMakeWithParts(mantissa, exponent)
|
||||
return CPDecimalMakeNaN();
|
||||
|
||||
// remaining digits are disposed of via truncation
|
||||
while ((mantissa > 0) && (m.length < CPDecimalMaxDigits)) // count selector here could be optimised away
|
||||
while ((mantissa > 0) && (m.length < CPDecimalMaxDigits))
|
||||
{
|
||||
Array.prototype.unshift.call(m, parseInt(mantissa % 10));
|
||||
mantissa = FLOOR(mantissa / 10);
|
||||
@@ -294,6 +302,7 @@ function CPDecimalIsZero(dcm)
|
||||
function CPDecimalIsOne(dcm)
|
||||
{
|
||||
CPDecimalCompact(dcm);
|
||||
|
||||
// exponent doesn't matter as long as mantissa = 0
|
||||
if (!dcm._isNaN)
|
||||
{
|
||||
@@ -304,7 +313,7 @@ function CPDecimalIsOne(dcm)
|
||||
}
|
||||
|
||||
//private method to copy attribute values
|
||||
function _CPDecimalSet(t,s)
|
||||
function _CPDecimalSet(t, s)
|
||||
{
|
||||
// should all be [x copy] ?
|
||||
t._exponent = s._exponent;
|
||||
@@ -528,20 +537,22 @@ function CPDecimalAdd(result, leftOperand, rightOperand, roundingMode, longMode)
|
||||
result._isNaN = YES;
|
||||
return CPCalculationNoError;
|
||||
}
|
||||
|
||||
// check for zero
|
||||
if (CPDecimalIsZero(leftOperand))
|
||||
{
|
||||
_CPDecimalSet(result, rightOperand);
|
||||
return CPCalculationNoError;
|
||||
}
|
||||
|
||||
if (CPDecimalIsZero(rightOperand))
|
||||
{
|
||||
_CPDecimalSet(result, leftOperand);
|
||||
return CPCalculationNoError;
|
||||
}
|
||||
|
||||
var n1 = CPDecimalCopy(leftOperand),
|
||||
n2 = CPDecimalCopy(rightOperand);
|
||||
var n1 = CPDecimalCopy(leftOperand),
|
||||
n2 = CPDecimalCopy(rightOperand);
|
||||
|
||||
// For different signs use subtraction
|
||||
if (leftOperand._isNegative != rightOperand._isNegative)
|
||||
@@ -564,6 +575,7 @@ function CPDecimalAdd(result, leftOperand, rightOperand, roundingMode, longMode)
|
||||
var comp = 0,
|
||||
ll = n1._mantissa.length,
|
||||
lr = n2._mantissa.length;
|
||||
|
||||
if (ll == lr)
|
||||
comp = CPOrderedSame;
|
||||
else if (ll > lr)
|
||||
@@ -576,6 +588,7 @@ function CPDecimalAdd(result, leftOperand, rightOperand, roundingMode, longMode)
|
||||
{
|
||||
n1._isNegative = NO;
|
||||
n2._isNegative = NO;
|
||||
|
||||
// SimpleCompare does not look at sign
|
||||
if (comp == CPOrderedDescending)
|
||||
{
|
||||
@@ -585,7 +598,9 @@ function CPDecimalAdd(result, leftOperand, rightOperand, roundingMode, longMode)
|
||||
{
|
||||
adderror = _SimpleAdd(result, n2, n1, roundingMode, longMode);
|
||||
}
|
||||
|
||||
result._isNegative = YES;
|
||||
|
||||
// swap sign over over/underflow exception
|
||||
if (CPCalculationUnderflow == adderror)
|
||||
adderror = CPCalculationOverflow;
|
||||
@@ -627,6 +642,7 @@ function _SimpleSubtract(result, leftOperand, rightOperand, roundingMode)
|
||||
for (; i >= 0; i--)
|
||||
{
|
||||
var d = result._mantissa[i + j] - rightOperand._mantissa[i] - borrow;
|
||||
|
||||
if (d < 0)
|
||||
{
|
||||
d = d + 10;
|
||||
@@ -683,15 +699,16 @@ function CPDecimalSubtract(result, leftOperand, rightOperand, roundingMode)
|
||||
result._isNegative = !result._isNegative;
|
||||
return CPCalculationNoError;
|
||||
}
|
||||
|
||||
if (CPDecimalIsZero(rightOperand))
|
||||
{
|
||||
_CPDecimalSet(result, leftOperand);
|
||||
return CPCalculationNoError;
|
||||
}
|
||||
|
||||
var n1 = CPDecimalCopy(leftOperand),
|
||||
n2 = CPDecimalCopy(rightOperand),
|
||||
error1 = CPCalculationNoError;
|
||||
var n1 = CPDecimalCopy(leftOperand),
|
||||
n2 = CPDecimalCopy(rightOperand),
|
||||
error1 = CPCalculationNoError;
|
||||
|
||||
// For different signs use addition
|
||||
if (leftOperand._isNegative != rightOperand._isNegative)
|
||||
@@ -701,10 +718,12 @@ function CPDecimalSubtract(result, leftOperand, rightOperand, roundingMode)
|
||||
n1._isNegative = NO;
|
||||
error1 = CPDecimalAdd(result, n1, rightOperand, roundingMode);
|
||||
result._isNegative = YES;
|
||||
|
||||
if (error1 == CPCalculationUnderflow)
|
||||
error1 = CPCalculationOverflow;
|
||||
else if (error1 == CPCalculationOverflow) // gnustep has bug here
|
||||
error1 = CPCalculationUnderflow;
|
||||
|
||||
return error1;
|
||||
}
|
||||
else
|
||||
@@ -728,6 +747,7 @@ function CPDecimalSubtract(result, leftOperand, rightOperand, roundingMode)
|
||||
{
|
||||
n1._isNegative = NO;
|
||||
n2._isNegative = NO;
|
||||
|
||||
if (comp == CPOrderedAscending)
|
||||
{
|
||||
error1 = _SimpleSubtract(result, n1, n2, roundingMode);
|
||||
@@ -780,6 +800,7 @@ function _SimpleDivide(result, leftOperand, rightOperand, roundingMode)
|
||||
{
|
||||
if (stopk == k)
|
||||
break;
|
||||
|
||||
if (n1._exponent)
|
||||
{
|
||||
// Put back zeros removed by compacting
|
||||
@@ -798,6 +819,7 @@ function _SimpleDivide(result, leftOperand, rightOperand, roundingMode)
|
||||
Array.prototype.push.call(n1._mantissa, (leftOperand._mantissa[used]));
|
||||
n1._isCompact = NO;
|
||||
}
|
||||
|
||||
used++;
|
||||
}
|
||||
else
|
||||
@@ -808,6 +830,7 @@ function _SimpleDivide(result, leftOperand, rightOperand, roundingMode)
|
||||
k = stopk;
|
||||
break;
|
||||
}
|
||||
|
||||
// Borrow one digit
|
||||
Array.prototype.push.call(n1._mantissa, 0);
|
||||
result._exponent--;
|
||||
@@ -842,6 +865,7 @@ function _SimpleDivide(result, leftOperand, rightOperand, roundingMode)
|
||||
}
|
||||
|
||||
error1 = CPDecimalSubtract(n1, n1, rightOperand, roundingMode);
|
||||
|
||||
if (error1 != CPCalculationNoError)
|
||||
error = error1;
|
||||
|
||||
@@ -878,6 +902,7 @@ function CPDecimalDivide(result, leftOperand, rightOperand, roundingMode)
|
||||
result._isNaN = YES;
|
||||
return CPCalculationDivideByZero;
|
||||
}
|
||||
|
||||
if (CPDecimalIsZero(leftOperand))
|
||||
{
|
||||
_CPDecimalSetZero(result);
|
||||
@@ -917,6 +942,7 @@ function CPDecimalDivide(result, leftOperand, rightOperand, roundingMode)
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
result._exponent += exp;
|
||||
result._isNegative = neg;
|
||||
return error;
|
||||
@@ -933,8 +959,7 @@ function _SimpleMultiply(result, leftOperand, rightOperand, roundingMode, powerM
|
||||
_CPDecimalSetZero(result);
|
||||
|
||||
// Do every digit of the second number
|
||||
var i = 0;
|
||||
for (; i < rightOperand._mantissa.length; i++)
|
||||
for (var i = 0; i < rightOperand._mantissa.length; i++)
|
||||
{
|
||||
_CPDecimalSetZero(n);
|
||||
|
||||
@@ -945,10 +970,10 @@ function _SimpleMultiply(result, leftOperand, rightOperand, roundingMode, powerM
|
||||
if (d == 0)
|
||||
continue;
|
||||
|
||||
var j = 0;
|
||||
for (j = leftOperand._mantissa.length - 1; j >= 0; j--)
|
||||
for (var j = leftOperand._mantissa.length - 1; j >= 0; j--)
|
||||
{
|
||||
e = leftOperand._mantissa[j] * d + carry;
|
||||
|
||||
if (e >= 10)
|
||||
{
|
||||
carry = FLOOR(e / 10);
|
||||
@@ -960,6 +985,7 @@ function _SimpleMultiply(result, leftOperand, rightOperand, roundingMode, powerM
|
||||
// This is one off to allow final carry
|
||||
n._mantissa[j + 1] = e;
|
||||
}
|
||||
|
||||
n._mantissa[0] = carry;
|
||||
|
||||
CPDecimalCompact(n);
|
||||
@@ -976,17 +1002,20 @@ function _SimpleMultiply(result, leftOperand, rightOperand, roundingMode, powerM
|
||||
result._isNaN = YES;
|
||||
return CPCalculationOverflow;
|
||||
}
|
||||
|
||||
result._exponent += exp;
|
||||
|
||||
// perform round to CPDecimalMaxDigits
|
||||
if (result._mantissa.length > CPDecimalMaxDigits && !powerMode)
|
||||
{
|
||||
result._isCompact = NO;
|
||||
|
||||
var scale = CPDecimalMaxDigits - (result._mantissa.length + result._exponent);
|
||||
CPDecimalRound(result, result, scale, roundingMode); // calls compact
|
||||
|
||||
error = CPCalculationLossOfPrecision;
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
@@ -1023,6 +1052,7 @@ function CPDecimalMultiply(result, leftOperand, rightOperand, roundingMode, powe
|
||||
if (exp > CPDecimalMaxExponent)
|
||||
{
|
||||
result._isNaN = YES;
|
||||
|
||||
if (neg)
|
||||
return CPCalculationUnderflow;
|
||||
else
|
||||
@@ -1041,6 +1071,7 @@ function CPDecimalMultiply(result, leftOperand, rightOperand, roundingMode, powe
|
||||
var comp = 0,
|
||||
ll = n1._mantissa.length,
|
||||
lr = n2._mantissa.length;
|
||||
|
||||
if (ll == lr)
|
||||
comp = CPOrderedSame;
|
||||
else if (ll > lr)
|
||||
@@ -1048,7 +1079,6 @@ function CPDecimalMultiply(result, leftOperand, rightOperand, roundingMode, powe
|
||||
else
|
||||
comp = CPOrderedAscending;
|
||||
|
||||
|
||||
if (comp == CPOrderedDescending)
|
||||
{
|
||||
error = _SimpleMultiply(result, n1, n2, roundingMode, powerMode);
|
||||
@@ -1063,6 +1093,7 @@ function CPDecimalMultiply(result, leftOperand, rightOperand, roundingMode, powe
|
||||
if (result._exponent + exp > CPDecimalMaxExponent)
|
||||
{
|
||||
result._isNaN = YES;
|
||||
|
||||
if (neg)
|
||||
return CPCalculationUnderflow;
|
||||
else
|
||||
@@ -1099,17 +1130,21 @@ function CPDecimalMultiply(result, leftOperand, rightOperand, roundingMode, powe
|
||||
function CPDecimalMultiplyByPowerOf10(result, dcm, power, roundingMode)
|
||||
{
|
||||
_CPDecimalSet(result, dcm);
|
||||
|
||||
var p = result._exponent + power;
|
||||
|
||||
if (p > CPDecimalMaxExponent)
|
||||
{
|
||||
result._isNaN = YES;
|
||||
return CPCalculationOverflow;
|
||||
}
|
||||
|
||||
if (p < CPDecimalMinExponent)
|
||||
{
|
||||
result._isNaN = YES;
|
||||
return CPCalculationUnderflow;
|
||||
}
|
||||
|
||||
result._exponent += power;
|
||||
return CPCalculationNoError;
|
||||
}
|
||||
@@ -1134,6 +1169,7 @@ function CPDecimalPower(result, dcm, power, roundingMode)
|
||||
_CPDecimalSetOne(result);
|
||||
|
||||
var e = power;
|
||||
|
||||
while (e)
|
||||
{
|
||||
if (e & 1)
|
||||
@@ -1177,7 +1213,7 @@ function CPDecimalPower(result, dcm, power, roundingMode)
|
||||
*/
|
||||
function CPDecimalNormalize(dcm1, dcm2, roundingMode, longMode)
|
||||
{
|
||||
var factor = (longMode)?2:1;
|
||||
var factor = (longMode) ? 2 : 1;
|
||||
|
||||
if (dcm1._isNaN || dcm2._isNaN)
|
||||
return CPCalculationNoError; // FIXME: correct behavior?
|
||||
@@ -1185,6 +1221,7 @@ function CPDecimalNormalize(dcm1, dcm2, roundingMode, longMode)
|
||||
// ensure compact
|
||||
if (!dcm1._isCompact)
|
||||
CPDecimalCompact(dcm1);
|
||||
|
||||
if (!dcm2._isCompact)
|
||||
CPDecimalCompact(dcm2);
|
||||
|
||||
@@ -1200,6 +1237,7 @@ function CPDecimalNormalize(dcm1, dcm2, roundingMode, longMode)
|
||||
l = 0;
|
||||
|
||||
var e = 0;
|
||||
|
||||
if (e2 > e1 && e1 >= 0 && e2 >= 0)
|
||||
e = e2 - e1;
|
||||
else if (e2 > e1 && e1 < 0 && e2 >= 0)
|
||||
@@ -1225,6 +1263,7 @@ function CPDecimalNormalize(dcm1, dcm2, roundingMode, longMode)
|
||||
else
|
||||
Array.prototype.push.call(dcm1._mantissa, 0);
|
||||
}
|
||||
|
||||
if (e2 > e1)
|
||||
{
|
||||
dcm2._exponent -= l;
|
||||
@@ -1279,6 +1318,7 @@ function CPDecimalNormalize(dcm1, dcm2, roundingMode, longMode)
|
||||
dcm2._isCompact = NO;
|
||||
}
|
||||
}
|
||||
|
||||
return CPCalculationLossOfPrecision;
|
||||
}
|
||||
|
||||
@@ -1297,8 +1337,10 @@ function CPDecimalNormalize(dcm1, dcm2, roundingMode, longMode)
|
||||
@param roundingMode the rounding mode for the operation
|
||||
@return a CPCalculationError status value.
|
||||
*/
|
||||
function CPDecimalRound(result, dcm, scale ,roundingMode)
|
||||
function CPDecimalRound(result, dcm, scale, roundingMode)
|
||||
{
|
||||
_CPDecimalSet(result, dcm);
|
||||
|
||||
if (dcm._isNaN)
|
||||
return;
|
||||
|
||||
@@ -1311,18 +1353,19 @@ function CPDecimalRound(result, dcm, scale ,roundingMode)
|
||||
if (scale == CPDecimalNoScale)
|
||||
return;
|
||||
|
||||
_CPDecimalSet(result,dcm);
|
||||
|
||||
var mc = result._mantissa.length,
|
||||
l = mc + scale + result._exponent;
|
||||
|
||||
if (mc <= l)
|
||||
return;
|
||||
|
||||
else if (l <= 0)
|
||||
{
|
||||
_CPDecimalSetZero(result);
|
||||
return;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
var c = 0,
|
||||
n = 0,
|
||||
up = 0;
|
||||
@@ -1332,39 +1375,46 @@ function CPDecimalRound(result, dcm, scale ,roundingMode)
|
||||
|
||||
switch (roundingMode)
|
||||
{
|
||||
case CPRoundDown:
|
||||
up = result._isNegative;
|
||||
break;
|
||||
case CPRoundUp:
|
||||
up = !result._isNegative;
|
||||
break;
|
||||
case CPRoundPlain:
|
||||
n = result._mantissa[l];
|
||||
up = (n >= 5);
|
||||
break;
|
||||
case _CPRoundHalfDown:
|
||||
n = result._mantissa[l];
|
||||
up = (n > 5);
|
||||
break;
|
||||
case CPRoundBankers:
|
||||
n = result._mantissa[l];
|
||||
if (n > 5)
|
||||
up = YES;
|
||||
else if (n < 5)
|
||||
up = NO;
|
||||
else
|
||||
{
|
||||
if (l == 0)
|
||||
c = 0;
|
||||
case CPRoundDown:
|
||||
up = result._isNegative;
|
||||
break;
|
||||
|
||||
case CPRoundUp:
|
||||
up = !result._isNegative;
|
||||
break;
|
||||
|
||||
case CPRoundPlain:
|
||||
n = result._mantissa[l];
|
||||
up = (n >= 5);
|
||||
break;
|
||||
|
||||
case _CPRoundHalfDown:
|
||||
n = result._mantissa[l];
|
||||
up = (n > 5);
|
||||
break;
|
||||
|
||||
case CPRoundBankers:
|
||||
n = result._mantissa[l];
|
||||
|
||||
if (n > 5)
|
||||
up = YES;
|
||||
else if (n < 5)
|
||||
up = NO;
|
||||
else
|
||||
c = result._mantissa[l - 1];
|
||||
up = ((c % 2) != 0);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
up = NO;
|
||||
break;
|
||||
{
|
||||
if (l == 0)
|
||||
c = 0;
|
||||
else
|
||||
c = result._mantissa[l - 1];
|
||||
up = ((c % 2) != 0);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
up = NO;
|
||||
break;
|
||||
}
|
||||
|
||||
// cut mantissa
|
||||
result._mantissa = Array.prototype.slice.call(result._mantissa, 0, l);
|
||||
|
||||
@@ -1377,13 +1427,16 @@ function CPDecimalRound(result, dcm, scale ,roundingMode)
|
||||
result._mantissa[i]++;
|
||||
break;
|
||||
}
|
||||
|
||||
result._mantissa[i] = 0;
|
||||
}
|
||||
|
||||
// Final overflow?
|
||||
if (i == -1)
|
||||
{
|
||||
// As all digits are zeros, just change the first
|
||||
result._mantissa[0] = 1;
|
||||
|
||||
if (result._exponent >= CPDecimalMaxExponent)
|
||||
{
|
||||
// Overflow in rounding.
|
||||
@@ -1417,17 +1470,18 @@ function CPDecimalCompact(dcm)
|
||||
_CPDecimalSetZero(dcm);
|
||||
return;
|
||||
}
|
||||
|
||||
// leading zeros, when exponent is zero these mean we need to move our decimal point to compact
|
||||
// if exp is zero does it make sense to have them? don't think so so delete them
|
||||
while (dcm._mantissa[0] === 0)
|
||||
{
|
||||
Array.prototype.shift.call(dcm._mantissa);
|
||||
}
|
||||
|
||||
// trailing zeros, strip them
|
||||
while (dcm._mantissa[dcm._mantissa.length - 1] === 0)
|
||||
{
|
||||
Array.prototype.pop.call(dcm._mantissa);
|
||||
dcm._exponent++;
|
||||
|
||||
if (dcm._exponent + 1 > CPDecimalMaxExponent)
|
||||
{
|
||||
// TODO: test case for this
|
||||
@@ -1481,6 +1535,7 @@ function CPDecimalString(dcm, locale)
|
||||
{
|
||||
string += dcm._mantissa[i];
|
||||
}
|
||||
|
||||
if (l < k)
|
||||
{
|
||||
string += ".";
|
||||
@@ -1489,6 +1544,7 @@ function CPDecimalString(dcm, locale)
|
||||
string += dcm._mantissa[i];
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < dcm._exponent; i++)
|
||||
{
|
||||
string += "0";
|
||||
|
||||
@@ -123,6 +123,7 @@ var CPDefaultDcmHandler = nil;
|
||||
{
|
||||
if (!CPDefaultDcmHandler)
|
||||
CPDefaultDcmHandler = [[CPDecimalNumberHandler alloc] init];
|
||||
|
||||
return CPDefaultDcmHandler;
|
||||
}
|
||||
|
||||
@@ -172,26 +173,37 @@ var CPDefaultDcmHandler = nil;
|
||||
{
|
||||
switch (error)
|
||||
{
|
||||
case CPCalculationNoError: break;
|
||||
case CPCalculationOverflow: if (_raiseOnOverflow)
|
||||
[CPException raise:CPDecimalNumberOverflowException reason:("A CPDecimalNumber overflow has occurred. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
else
|
||||
return [CPDecimalNumber notANumber];
|
||||
break;
|
||||
case CPCalculationUnderflow: if (_raiseOnUnderflow)
|
||||
[CPException raise:CPDecimalNumberUnderflowException reason:("A CPDecimalNumber underflow has occurred. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
else
|
||||
return [CPDecimalNumber notANumber];
|
||||
break;
|
||||
case CPCalculationLossOfPrecision: if (_raiseOnExactness)
|
||||
[CPException raise:CPDecimalNumberExactnessException reason:("A CPDecimalNumber has been rounded off during a calculation. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
break;
|
||||
case CPCalculationDivideByZero: if (_raiseOnDivideByZero)
|
||||
[CPException raise:CPDecimalNumberDivideByZeroException reason:("A CPDecimalNumber divide by zero has occurred. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
else
|
||||
return [CPDecimalNumber notANumber]; // Div by zero returns NaN
|
||||
break;
|
||||
default: [CPException raise:CPInvalidArgumentException reason:("An unknown CPDecimalNumber error has occurred. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
case CPCalculationNoError:
|
||||
break;
|
||||
|
||||
case CPCalculationOverflow:
|
||||
if (_raiseOnOverflow)
|
||||
[CPException raise:CPDecimalNumberOverflowException reason:("A CPDecimalNumber overflow has occurred. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
else
|
||||
return [CPDecimalNumber notANumber];
|
||||
break;
|
||||
|
||||
case CPCalculationUnderflow:
|
||||
if (_raiseOnUnderflow)
|
||||
[CPException raise:CPDecimalNumberUnderflowException reason:("A CPDecimalNumber underflow has occurred. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
else
|
||||
return [CPDecimalNumber notANumber];
|
||||
break;
|
||||
|
||||
case CPCalculationLossOfPrecision:
|
||||
if (_raiseOnExactness)
|
||||
[CPException raise:CPDecimalNumberExactnessException reason:("A CPDecimalNumber has been rounded off during a calculation. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
break;
|
||||
|
||||
case CPCalculationDivideByZero:
|
||||
if (_raiseOnDivideByZero)
|
||||
[CPException raise:CPDecimalNumberDivideByZeroException reason:("A CPDecimalNumber divide by zero has occurred. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
else
|
||||
return [CPDecimalNumber notANumber]; // Div by zero returns NaN
|
||||
break;
|
||||
|
||||
default:
|
||||
[CPException raise:CPInvalidArgumentException reason:("An unknown CPDecimalNumber error has occurred. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')")];
|
||||
}
|
||||
|
||||
return nil;
|
||||
@@ -537,6 +549,7 @@ var CPDecimalNumberHandlerRoundingModeKey = @"CPDecimalNumberHandlerRoundi
|
||||
if (res != nil)
|
||||
return res;
|
||||
}
|
||||
|
||||
return [CPDecimalNumber decimalNumberWithDecimal:result];
|
||||
}
|
||||
|
||||
@@ -570,9 +583,11 @@ var CPDecimalNumberHandlerRoundingModeKey = @"CPDecimalNumberHandlerRoundi
|
||||
if (error > CPCalculationNoError)
|
||||
{
|
||||
var res = [behavior exceptionDuringOperation:_cmd error:error leftOperand:self rightOperand:decimalNumber];
|
||||
|
||||
if (res != nil)
|
||||
return res;
|
||||
}
|
||||
|
||||
return [CPDecimalNumber decimalNumberWithDecimal:result];
|
||||
}
|
||||
|
||||
@@ -609,6 +624,7 @@ var CPDecimalNumberHandlerRoundingModeKey = @"CPDecimalNumberHandlerRoundi
|
||||
if (res != nil)
|
||||
return res;
|
||||
}
|
||||
|
||||
return [CPDecimalNumber decimalNumberWithDecimal:result];
|
||||
}
|
||||
|
||||
@@ -642,9 +658,11 @@ var CPDecimalNumberHandlerRoundingModeKey = @"CPDecimalNumberHandlerRoundi
|
||||
if (error > CPCalculationNoError)
|
||||
{
|
||||
var res = [behavior exceptionDuringOperation:_cmd error:error leftOperand:self rightOperand:decimalNumber];
|
||||
|
||||
if (res != nil)
|
||||
return res;
|
||||
}
|
||||
|
||||
return [CPDecimalNumber decimalNumberWithDecimal:result];
|
||||
}
|
||||
|
||||
@@ -678,9 +696,11 @@ var CPDecimalNumberHandlerRoundingModeKey = @"CPDecimalNumberHandlerRoundi
|
||||
if (error > CPCalculationNoError)
|
||||
{
|
||||
var res = [behavior exceptionDuringOperation:_cmd error:error leftOperand:self rightOperand:[CPDecimalNumber decimalNumberWithString:power.toString()]];
|
||||
|
||||
if (res != nil)
|
||||
return res;
|
||||
}
|
||||
|
||||
return [CPDecimalNumber decimalNumberWithDecimal:result];
|
||||
}
|
||||
|
||||
@@ -717,9 +737,11 @@ var CPDecimalNumberHandlerRoundingModeKey = @"CPDecimalNumberHandlerRoundi
|
||||
if (error > CPCalculationNoError)
|
||||
{
|
||||
var res = [behavior exceptionDuringOperation:_cmd error:error leftOperand:self rightOperand:[CPDecimalNumber decimalNumberWithString:power.toString()]];
|
||||
|
||||
if (res != nil)
|
||||
return res;
|
||||
}
|
||||
|
||||
return [CPDecimalNumber decimalNumberWithDecimal:result];
|
||||
}
|
||||
|
||||
@@ -751,6 +773,7 @@ var CPDecimalNumberHandlerRoundingModeKey = @"CPDecimalNumberHandlerRoundi
|
||||
// aNumber type is checked to convert if appropriate
|
||||
if (![aNumber isKindOfClass:[CPDecimalNumber class]])
|
||||
aNumber = [CPDecimalNumber decimalNumberWithString:aNumber.toString()];
|
||||
|
||||
return CPDecimalCompare([self decimalValue], [aNumber decimalValue]);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
{
|
||||
if (!_dcmnh)
|
||||
_dcmnh = [CPDecimalNumberHandler decimalNumberHandlerWithRoundingMode:CPRoundPlain scale:0 raiseOnExactness:NO raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
|
||||
|
||||
if (!_dcmnhWithExactness)
|
||||
_dcmnhWithExactness = [CPDecimalNumberHandler decimalNumberHandlerWithRoundingMode:CPRoundPlain scale:0 raiseOnExactness:YES raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
|
||||
}
|
||||
@@ -42,6 +43,7 @@
|
||||
// CPDecimalNumber class initialisers
|
||||
var dcmn = [[CPDecimalNumber alloc] initWithString:@"10.72342"],
|
||||
dcm = [dcmn decimalValue];
|
||||
|
||||
[self assert:-5 equals:dcm._exponent message:"initWithString: - exponent"];
|
||||
[self assert:[1,0,7,2,3,4,2] equals:dcm._mantissa message:"initWithString: - mantissa"];
|
||||
[self assert:NO equals:dcm._isNegative message:"initWithString: - sign"];
|
||||
@@ -157,11 +159,11 @@
|
||||
|
||||
- (void)testAdd
|
||||
{
|
||||
|
||||
var dcmn1 = [CPDecimalNumber decimalNumberWithString:@"123"],
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"321"],
|
||||
dcmn3 = [dcmn1 decimalNumberByAdding:dcmn2],
|
||||
d1 = [dcmn3 decimalValue];
|
||||
|
||||
[self assert:0 equals:d1._exponent message:"decimalNumberByAdding: - exponent"];
|
||||
[self assert:[4,4,4] equals:d1._mantissa message:"decimalNumberByAdding: - mantissa"];
|
||||
[self assert:NO equals:d1._isNegative message:"decimalNumberByAdding: - sign"];
|
||||
@@ -170,6 +172,7 @@
|
||||
// precision throw - with behavior
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"10000000000000000000000000000000000001"];
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"1.00001"];
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByAdding:dcmn2 withBehavior:_dcmnhWithExactness];
|
||||
@@ -202,7 +205,6 @@
|
||||
if ((e.isa) && [e name] == AssertionFailedError)
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
- (void)testSubtract
|
||||
@@ -211,6 +213,7 @@
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"153"],
|
||||
dcmn3 = [dcmn1 decimalNumberBySubtracting:dcmn2],
|
||||
d1 = [dcmn3 decimalValue];
|
||||
|
||||
[self assert:0 equals:d1._exponent message:"decimalNumberBySubtracting: - exponent"];
|
||||
[self assert:[1,4,8] equals:d1._mantissa message:"decimalNumberBySubtracting: - mantissa"];
|
||||
[self assert:NO equals:d1._isNegative message:"decimalNumberBySubtracting: - sign"];
|
||||
@@ -219,6 +222,7 @@
|
||||
// precision throw - with behavior
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"1e-128"];
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"1"];
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberBySubtracting:dcmn2 withBehavior:_dcmnhWithExactness];
|
||||
@@ -247,6 +251,7 @@
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"1235"],
|
||||
dcmn3 = [dcmn1 decimalNumberByDividingBy:dcmn2],
|
||||
d1 = [dcmn3 decimalValue];
|
||||
|
||||
[self assert:-34 equals:d1._exponent message:"decimalNumberByDividingBy: - exponent"];
|
||||
[self assert:[5,7,7,0,2,9,3,9,2,7,1,2,5,5,0,6,0,7,2,8,7,4,4,9,3,9,2,7,1,2,5,5,0,6,0,7,2,8] equals:d1._mantissa message:"decimalNumberByDividingBy: T1: mantissa"];
|
||||
[self assert:NO equals:d1._isNegative message:"decimalNumberByDividingBy: - sign"];
|
||||
@@ -264,6 +269,7 @@
|
||||
// Exceptions
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"1"];
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"3"];
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByDividingBy:dcmn2 withBehavior:_dcmnhWithExactness];
|
||||
@@ -286,7 +292,9 @@
|
||||
// underflow , if dcnm2 is < 10 this is LossOfPrecision, is this correct? check vs cocoa.
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"-1e-128"];
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"10"];
|
||||
try {
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByDividingBy:dcmn2];
|
||||
[self fail:"decimalNumberByDividingBy: TEX3 - should have thrown underflow error "];
|
||||
}
|
||||
@@ -298,6 +306,7 @@
|
||||
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"-1e-128"];
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"0"];
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByDividingBy:dcmn2];
|
||||
@@ -322,11 +331,11 @@
|
||||
|
||||
- (void)testMultiply
|
||||
{
|
||||
|
||||
var dcmn1 = [CPDecimalNumber decimalNumberWithString:@"17"],
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"512"],
|
||||
dcmn3 = [dcmn1 decimalNumberByMultiplyingBy:dcmn2],
|
||||
d1 = [dcmn3 decimalValue];
|
||||
|
||||
[self assert:0 equals:d1._exponent message:"decimalNumberByMultiplyingBy: - exponent"];
|
||||
[self assert:[8,7,0,4] equals:d1._mantissa message:"decimalNumberByMultiplyingBy: - mantissa"];
|
||||
[self assert:NO equals:d1._isNegative message:"decimalNumberByMultiplyingBy: - sign"];
|
||||
@@ -344,6 +353,7 @@
|
||||
// Exceptions
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"18302741890374918"];
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"123847190277982223422346878934534"];
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByMultiplyingBy:dcmn2 withBehavior:_dcmnhWithExactness];
|
||||
@@ -354,6 +364,7 @@
|
||||
if ((e.isa) && [e name] == AssertionFailedError)
|
||||
throw e;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByMultiplyingBy:dcmn2];
|
||||
@@ -366,6 +377,7 @@
|
||||
// overflow
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"23e100"];
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"-13e76"];
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByMultiplyingBy:dcmn2];
|
||||
@@ -384,6 +396,7 @@
|
||||
p = 21,
|
||||
dcmn3 = [dcmn1 decimalNumberByMultiplyingByPowerOf10:p],
|
||||
d1 = [dcmn3 decimalValue];
|
||||
|
||||
[self assert:21 equals:d1._exponent message:"decimalNumberByMultiplyingByPowerOf10: - exponent"];
|
||||
[self assert:[1,7] equals:d1._mantissa message:"decimalNumberByMultiplyingByPowerOf10: - mantissa"];
|
||||
[self assert:NO equals:d1._isNegative message:"decimalNumberByMultiplyingByPowerOf10: - sign"];
|
||||
@@ -402,7 +415,9 @@
|
||||
// overflow
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"1e23"];
|
||||
p = 127;
|
||||
try {
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByMultiplyingByPowerOf10:p];
|
||||
[self fail:"decimalNumberByMultiplyingByPowerOf10: TEX1 - should have overflowed "];
|
||||
}
|
||||
@@ -420,6 +435,7 @@
|
||||
p = 10,
|
||||
dcmn3 = [dcmn1 decimalNumberByRaisingToPower:p],
|
||||
d1 = [dcmn3 decimalValue];
|
||||
|
||||
[self assert:0 equals:d1._exponent message:"decimalNumberByRaisingToPower: - exponent"];
|
||||
[self assert:[2,0,1,5,9,9,3,9,0,0,4,4,9] equals:d1._mantissa message:"decimalNumberByRaisingToPower: - mantissa"];
|
||||
[self assert:NO equals:d1._isNegative message:"decimalNumberByRaisingToPower: - sign"];
|
||||
@@ -437,6 +453,7 @@
|
||||
// Exceptions
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"123198771093489073413"];
|
||||
p = 2;
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByRaisingToPower:p withBehavior:_dcmnhWithExactness];
|
||||
@@ -447,6 +464,7 @@
|
||||
if ((e.isa) && [e name] == AssertionFailedError)
|
||||
throw e;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByRaisingToPower:p];
|
||||
@@ -459,6 +477,7 @@
|
||||
// overflow
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"1234"];
|
||||
p = 123;
|
||||
|
||||
try
|
||||
{
|
||||
dcmn3 = [dcmn1 decimalNumberByRaisingToPower:p];
|
||||
@@ -477,6 +496,7 @@
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"1.4"],
|
||||
dcmn3 = [dcmn1 decimalNumberByRoundingAccordingToBehavior:h],
|
||||
d1 = [dcmn3 decimalValue];
|
||||
|
||||
[self assert:0 equals:d1._exponent message:"decimalNumberByRoundingAccordingToBehavior: - exponent"];
|
||||
[self assert:[1] equals:d1._mantissa message:"decimalNumberByRoundingAccordingToBehavior: - mantissa"];
|
||||
[self assert:NO equals:d1._isNegative message:"decimalNumberByRoundingAccordingToBehavior: - sign"];
|
||||
@@ -493,6 +513,7 @@
|
||||
var dcmn1 = [CPDecimalNumber decimalNumberWithString:@"17"],
|
||||
dcmn2 = [CPDecimalNumber decimalNumberWithString:@"512"],
|
||||
c = [dcmn1 compare:dcmn2];
|
||||
|
||||
[self assert:c equals:CPOrderedAscending message:"compare: - ascending"];
|
||||
|
||||
dcmn1 = [CPDecimalNumber decimalNumberWithString:@"1234"];
|
||||
@@ -509,6 +530,7 @@
|
||||
- (void)testStringValue
|
||||
{
|
||||
var dcmn = [CPDecimalNumber decimalNumberWithString:@"512"];
|
||||
|
||||
[self assert:"512" equals:[dcmn stringValue] message:"stringValue: - small number"];
|
||||
dcmn = [CPDecimalNumber decimalNumberWithString:@"10823478917236482"];
|
||||
[self assert:"10823478917236482" equals:[dcmn stringValue] message:"stringValue: - medium number"];
|
||||
|
||||
@@ -304,6 +304,7 @@
|
||||
[self assert:-3 equals:dcm1._exponent message:"CPDecimalRound() Tb5: exponent"];
|
||||
|
||||
// Noscale
|
||||
dcm1 = CPDecimalMakeZero();
|
||||
CPDecimalRound(dcm1,dcm,CPDecimalNoScale,CPRoundPlain);
|
||||
[self assert:[1,2,3,4,5,6] equals:dcm1._mantissa message:"CPDecimalRound() Tns1: mantissa"];
|
||||
[self assert:-3 equals:dcm1._exponent message:"CPDecimalRound() Tns1: exponent"];
|
||||
|
||||
Reference in new issue
Block a user