mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-17 04:00:42 +00:00
CPDecimalString returns NaN for a NaN and Fix Compare of NaNs and Cocoa compatable handling of overflow/underflows when no exception
This commit is contained in:
1 parent
c02128a1fd
commit
2fe8845842
3 files changed
+23
-19
No files matched your search
+11
-5
@@ -146,7 +146,7 @@ function CPDecimalMakeWithString(string, locale)
|
||||
return CPDecimalMakeNaN();
|
||||
|
||||
// Representation internally starts at most significant digit
|
||||
var m = [CPArray array],
|
||||
var m = [CPMutableArray array],
|
||||
i = 0;
|
||||
for (; i < (intpart?intpart.length:0); i++)
|
||||
{
|
||||
@@ -177,7 +177,7 @@ function CPDecimalMakeWithString(string, locale)
|
||||
*/
|
||||
function CPDecimalMakeWithParts(mantissa, exponent)
|
||||
{
|
||||
var m = [CPArray array],
|
||||
var m = [CPMutableArray array],
|
||||
isNegative = NO;
|
||||
|
||||
if (mantissa < 0 )
|
||||
@@ -310,7 +310,7 @@ function _CPDecimalSet(t,s)
|
||||
|
||||
function _CPDecimalSetZero(result)
|
||||
{
|
||||
result._mantissa = [CPArray arrayWithObject:0];
|
||||
result._mantissa = [CPMutableArray arrayWithObject:0];
|
||||
result._exponent = 0;
|
||||
result._isNegative = NO;
|
||||
result._isCompact = YES;
|
||||
@@ -319,7 +319,7 @@ function _CPDecimalSetZero(result)
|
||||
|
||||
function _CPDecimalSetOne(result)
|
||||
{
|
||||
result._mantissa = [CPArray arrayWithObject:1];
|
||||
result._mantissa = [CPMutableArray arrayWithObject:1];
|
||||
result._exponent = 0;
|
||||
result._isNegative = NO;
|
||||
result._isCompact = YES;
|
||||
@@ -362,6 +362,9 @@ function CPDecimalCopy(dcm)
|
||||
*/
|
||||
function CPDecimalCompare(leftOperand, rightOperand)
|
||||
{
|
||||
if (leftOperand._isNaN && rightOperand._isNaN)
|
||||
return CPOrderedSame;
|
||||
|
||||
if (leftOperand._isNegative != rightOperand._isNegative)
|
||||
{
|
||||
if (rightOperand._isNegative)
|
||||
@@ -756,7 +759,7 @@ function _SimpleDivide(result, leftOperand, rightOperand, roundingMode)
|
||||
|
||||
_CPDecimalSetZero(result);
|
||||
|
||||
n1._mantissa = [CPArray array];
|
||||
n1._mantissa = [CPMutableArray array];
|
||||
|
||||
while ((k < [leftOperand._mantissa count]) || ([n1._mantissa count]
|
||||
&& !(([n1._mantissa count] == 1) && (n1._mantissa[0] == 0))))
|
||||
@@ -1432,6 +1435,9 @@ function CPDecimalString(dcm, locale)
|
||||
{
|
||||
// Cocoa seems to just add all the zeros... this maybe controlled by locale,
|
||||
// will check.
|
||||
if (dcm._isNaN)
|
||||
return @"NaN";
|
||||
|
||||
var string = @"",
|
||||
i = 0;
|
||||
|
||||
|
||||
@@ -172,21 +172,19 @@ var CPDefaultDcmHandler = nil;
|
||||
{
|
||||
switch (error)
|
||||
{
|
||||
case CPCalculationNoError: return nil;
|
||||
case CPCalculationNoError: break;
|
||||
case CPCalculationOverflow: if (_raiseOnOverflow)
|
||||
[CPException raise:CPDecimalNumberOverflowException reason:("A CPDecimalNumber overflow has occured. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
else
|
||||
return [CPDecimalNumber maximumDecimalNumber]; // there was overflow return largest possible val
|
||||
return [CPDecimalNumber notANumber];
|
||||
break;
|
||||
case CPCalculationUnderflow: if (_raiseOnUnderflow)
|
||||
[CPException raise:CPDecimalNumberUnderflowException reason:("A CPDecimalNumber underflow has occured. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
else
|
||||
return [CPDecimalNumber minimumDecimalNumber]; // there was underflow, return smallest possible value
|
||||
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 + "')") ];
|
||||
else
|
||||
return nil; // Ignore.
|
||||
break;
|
||||
case CPCalculationDivideByZero: if (_raiseOnDivideByZero)
|
||||
[CPException raise:CPDecimalNumberDivideByZeroException reason:("A CPDecimalNumber divide by zero has occured. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
@@ -196,7 +194,7 @@ var CPDefaultDcmHandler = nil;
|
||||
default: [CPException raise:CPInvalidArgumentException reason:("An unknown CPDecimalNumber error has occured. (Left operand= '" + [leftOperand descriptionWithLocale:nil] + "' Right operand= '" + [rightOperand descriptionWithLocale:nil] + "' Selector= '" + operation + "')") ];
|
||||
}
|
||||
|
||||
return nil;
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -581,7 +579,7 @@ var CPDecimalNumberHandlerRoundingModeKey = @"CPDecimalNumberHandlerRoundi
|
||||
// Thus the type is checked to send the operand to the correct compare function.
|
||||
- (CPComparisonResult)compare:(CPNumber)decimalNumber
|
||||
{
|
||||
if ([decimalNumber class] != CPDecimalNumber)
|
||||
if (![decimalNumber isKindOfClass:[CPDecimalNumber class]])
|
||||
decimalNumber = [CPDecimalNumber decimalNumberWithString:decimalNumber.toString()];
|
||||
return CPDecimalCompare([self decimalValue], [decimalNumber decimalValue]);
|
||||
}
|
||||
|
||||
@@ -140,25 +140,25 @@
|
||||
[self assert:true equals:dcm message:"boolValue: - should be true"];
|
||||
|
||||
dcmn = [[CPDecimalNumber alloc] initWithString:@"111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"];
|
||||
[self assertTrue:[dcm compare:[CPDecimalNumber notANumber]] message:"initWithString: overflow should return NaN"];
|
||||
[self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 1 overflow should return NaN"];
|
||||
|
||||
dcmn = [[CPDecimalNumber alloc] initWithString:@"foo"];
|
||||
[self assertTrue:[dcm compare:[CPDecimalNumber notANumber]] message:"initWithString: overflow should return NaN"];
|
||||
[self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 2 overflow should return NaN"];
|
||||
|
||||
dcmn = [[CPDecimalNumber alloc] initWithString:@".123"];
|
||||
[self assertTrue:[dcm compare:[CPDecimalNumber notANumber]] message:"initWithString: overflow should return NaN"];
|
||||
[self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 3 overflow should return NaN"];
|
||||
|
||||
dcmn = [[CPDecimalNumber alloc] initWithString:@"-.123"];
|
||||
[self assertTrue:[dcm compare:[CPDecimalNumber notANumber]] message:"initWithString: overflow should return NaN"];
|
||||
[self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 4 overflow should return NaN"];
|
||||
|
||||
dcmn = [[CPDecimalNumber alloc] initWithString:@"0123"];
|
||||
[self assertTrue:[dcm compare:[CPDecimalNumber notANumber]] message:"initWithString: overflow should return NaN"];
|
||||
[self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 5 overflow should return NaN"];
|
||||
|
||||
dcmn = [[CPDecimalNumber alloc] initWithString:@"1e200"];
|
||||
[self assertTrue:[dcm compare:[CPDecimalNumber notANumber]] message:"initWithString: overflow should return NaN"];
|
||||
[self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 6 overflow should return NaN"];
|
||||
|
||||
dcmn = [[CPDecimalNumber alloc] initWithString:@"12312e-23421"];
|
||||
[self assertTrue:[dcm compare:[CPDecimalNumber notANumber]] message:"initWithString: overflow should return NaN"];
|
||||
[self assert:CPOrderedSame equals:[dcmn compare:[CPDecimalNumber notANumber]] message:"initWithString: 7 overflow should return NaN"];
|
||||
}
|
||||
|
||||
- (void)testAdd
|
||||
|
||||
Reference in new issue
Block a user