Decimal floating point number. More...
#import <CPDecimalNumber.h>
Class Methods | |
(id) | + alloc |
(CPDecimalNumber) | + decimalNumberWithDecimal: |
(CPDecimalNumber) | + decimalNumberWithMantissa:exponent:isNegative: |
(CPDecimalNumber) | + decimalNumberWithString: |
(CPDecimalNumber) | + decimalNumberWithString:locale: |
(id) | + defaultBehavior |
(CPDecimalNumber) | + maximumDecimalNumber |
(CPDecimalNumber) | + minimumDecimalNumber |
(CPDecimalNumber) | + notANumber |
(id) | + numberWithBool: |
(id) | + numberWithChar: |
(id) | + numberWithDouble: |
(id) | + numberWithFloat: |
(id) | + numberWithInt: |
(id) | + numberWithLong: |
(id) | + numberWithLongLong: |
(id) | + numberWithShort: |
(id) | + numberWithUnsignedChar: |
(id) | + numberWithUnsignedInt: |
(id) | + numberWithUnsignedLong: |
(id) | + numberWithUnsignedLongLong: |
(id) | + numberWithUnsignedShort: |
(CPDecimalNumber) | + one |
(void) | + setDefaultBehavior: |
(CPDecimalNumber) | + zero |
Decimal floating point number.
This class represents a decimal floating point number and the relevant mathematical operations to go with it. It guarantees accuracy up to 38 digits in the mantissa/coefficient and can handle numbers in the range: +/- 99999999999999999999999999999999999999 x 10^(127/-128) Methods are available for: Addition, Subtraction, Multiplication, Division, Powers and Rounding. Exceptions can be thrown on: Overflow, Underflow, Loss of Precision (rounding) and Divide by zero, the behaviour of which is controlled via the CPDecimalNumberHandler class.
Note: The aim here is to try to produce the exact same output as Cocoa. However, this is effectively not possible but to get as close as possible we must perform our calculations in a way such that we even get the same rounding errors building up, say when computing large powers which require many multiplications. The code here almost matches the results of Cocoa but there are some small differences as outlined below:
An example where a small rounding error difference creeps in: For the calculation (0.875 ^ 101) the result becomes: In Cocoa: 0.00000(13893554059925661274821814636807535200)1 the 38 digits are bracketed, the extra 39th digit in Cocoa is explained below. In Cappuccino: 0.00000(13893554059925661274821814636807535204) Difference: 4e-41
Since, in Cocoa, NSDecimalNumber uses a binary internal format for the mantissa (coefficient) the maximum it can store before truly losing precision is actually 2^128, which is a 39 digit number. After this rounding and exponent changes occur. In our implementation each digit is stored separately hence the mantissa maximum value is the maximum possible 38 digit number. Obviously Apple can only say precision is guaranteed to 38 digits cause at some point in the 39 digits numbers rounding starts. Hence there will be inherent differences between Cocoa and Cappuccino answers if rounding occurs (see above example). They both still provide the same 38 digit guarantee however.
So the actual range of NSDecimal is +/- 340282366920938463463374607431768211455 x 10^(127/-128) (Notice this is 39 digits) Compared to in Cappuccino: +/- 99999999999999999999999999999999999999 x 10^(127/-128)
Definition at line 2 of file CPDecimalNumber.h.
|
implementation |
Create a new CPDecimalNumber object uninitialised. Note: even though CPDecimalNumber inherits from CPNumber it is not toll free bridged to a JS type as CPNumber is.
Reimplemented from CPNumber.
Definition at line 323 of file CPDecimalNumber.j.
|
implementation |
Returns a JS bool representation.
Reimplemented from CPNumber.
Definition at line 849 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 858 of file CPDecimalNumber.j.
|
implementation |
Compare the receiver CPDecimalNumber to aNumber
. This is a CPNumber or subclass. Returns CPOrderedDescending, CPOrderedAscending or CPOrderedSame.
aNumber | an object of kind CPNumber to compare against. |
Reimplemented from CPNumber.
Definition at line 777 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of the summation of the receiver object and decimalNumber
. If overflow occurs then the consequence depends on the current default CPDecimalNumberHandler.
decimalNumber | the decimal number to add to the receiver |
Definition at line 534 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of the summation of the receiver object and decimalNumber
. If overflow occurs then the consequence depends on the CPDecimalNumberHandler object behavior.
decimalNumber | the decimal number to add to the receiver |
behavior | a CPDecimalNumberHandler object |
Definition at line 547 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of dividing the receiver object by decimalNumber
. If underflow, divide by zero or loss of precision occurs then the consequence depends on the current default CPDecimalNumberHandler object.
decimalNumber | the decimal number to divide the the receiver by |
Definition at line 608 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of dividing the receiver object by decimalNumber
. If underflow, divide by zero or loss of precision occurs then the consequence depends on the CPDecimalNumberHandler object behavior.
decimalNumber | the decimal number to divide the the receiver by |
behavior | a CPDecimalNumberHandler object |
Definition at line 622 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of multiplying the receiver object by decimalNumber
. If overflow or loss of precision occurs then the consequence depends on the current default CPDecimalNumberHandler object.
decimalNumber | the decimal number to multiply the the receiver by |
Definition at line 645 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of multiplying the receiver object by decimalNumber
. If overflow or loss of precision occurs then the consequence depends on the CPDecimalNumberHandler object behavior.
decimalNumber | the decimal number to multiply the the receiver by |
behavior | a CPDecimalNumberHandler object |
Definition at line 659 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of multiplying the receiver object by (10 ^ power
). If overflow, underflow or loss of precision occurs then the consequence depends on the current default CPDecimalNumberHandler object.
power | the power of 10 to multiply the receiver by |
Definition at line 683 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of multiplying the receiver object by (10 ^ power
). If overflow, underflow or loss of precision occurs then the consequence depends on the CPDecimalNumberHandler object behavior.
power | the power of 10 to multiply the receiver by |
behavior | a CPDecimalNumberHandler object |
Definition at line 697 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of raising the receiver object to the power power
. If overflow, underflow or loss of precision occurs then the consequence depends on the current default CPDecimalNumberHandler object.
power | the power to raise the receiver by |
Definition at line 721 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of raising the receiver object to the power power
. If overflow, underflow or loss of precision occurs then the consequence depends on the CPDecimalNumberHandler object behavior.
power | the power to raise the receiver by |
behavior | a CPDecimalNumberHandler object |
Definition at line 735 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of rounding the number according to the rounding behavior specified by the CPDecimalNumberHandler object behavior.
behavior | a CPDecimalNumberHandler object |
Definition at line 761 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of the subtraction of decimalNumber
from the receiver object. If underflow or loss of precision occurs then the consequence depends on the current default CPDecimalNumberHandler.
decimalNumber | the decimal number to subtract from the receiver |
Definition at line 570 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber object with the result of the subtraction of decimalNumber
from the receiver object. If underflow or loss of precision occurs then the consequence depends on the CPDecimalNumberHandler object behavior.
decimalNumber | the decimal number to subtract from the receiver |
behavior | a CPDecimalNumberHandler object |
Definition at line 584 of file CPDecimalNumber.j.
|
implementation |
Return a new CPDecimalNumber object with the contents of a CPDecimal object
dcm | the CPDecimal object to copy |
Definition at line 414 of file CPDecimalNumber.j.
|
implementation |
Creates a new CPDecimalNumber object with the given mantissa and exponent. See -initWithMantissa
:exponent:isNegative: for some extra notes.
mantissa | the mantissa of the decimal number |
exponent | the exponent of the number |
flag | true if number is negative |
Definition at line 427 of file CPDecimalNumber.j.
|
implementation |
Creates a new CPDecimalNumber with a string. If the string is badly formed or outside of the acceptable range of a CPDecimal then the number is initialised to NaN.
numberValue | the string to parse. |
Definition at line 439 of file CPDecimalNumber.j.
|
implementation |
Creates a new CPDecimalNumber with a string using the given locale. If the string is badly formed or outside of the acceptable range of a CPDecimal then the number is initialised to NaN. NOTE: Locales are currently not supported.
numberValue | the string to parse |
locale | the CPLocale object to use when parsing the number string |
Definition at line 453 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimal object (which effectively contains the internal decimal number representation).
Reimplemented from CPNumber.
Definition at line 829 of file CPDecimalNumber.j.
|
implementation |
Return the default CPDecimalNumberHandler
object.
Definition at line 462 of file CPDecimalNumber.j.
|
implementation |
Returns a string representation of the decimal number.
Reimplemented from CPNumber.
Definition at line 799 of file CPDecimalNumber.j.
|
implementation |
Returns a string representation of the decimal number given the specified locale. Note: locales are currently unsupported
locale | the locale |
Reimplemented from CPNumber.
Definition at line 810 of file CPDecimalNumber.j.
|
implementation |
Returns a JS float representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 839 of file CPDecimalNumber.j.
|
implementation |
Called by CPCoder's encodeObject: to archive the object instance.
aCoder | a CPCoder instance |
Reimplemented from CPNumber.
Provided by category CPDecimalNumber(CPCoding).
Definition at line 1270 of file CPDecimalNumber.j.
|
implementation |
Returns a JS float representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 868 of file CPDecimalNumber.j.
|
implementation |
Initialise a CPDecimalNumber object with NaN
Reimplemented from CPObject.
Definition at line 334 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with a boolean value.
value | a JS boolean value |
Reimplemented from CPNumber.
Definition at line 1101 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1113 of file CPDecimalNumber.j.
|
implementation |
Called by CPCoder's decodeObject: to initialise the object with an archived one.
aCoder | a CPCoder instance |
Reimplemented from CPNumber.
Provided by category CPDecimalNumber(CPCoding).
Definition at line 1250 of file CPDecimalNumber.j.
|
implementation |
Initialise a CPDecimalNumber object with the contents of a CPDecimal object
dcm | the CPDecimal object to copy |
Definition at line 344 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with a float value.
value | a JS float value |
Reimplemented from CPNumber.
Definition at line 1123 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with a float value.
value | a JS float value |
Reimplemented from CPNumber.
Definition at line 1133 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1143 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1153 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1163 of file CPDecimalNumber.j.
|
implementation |
Initialise a CPDecimalNumber object with the given mantissa and exponent. Note: that since 'long long' doesn't exist in JS the mantissa is smaller than possible in Cocoa and can thus not create the full number range possible for a CPDecimal. Also note that at extreme cases where overflow or truncation will occur to the parameters in Cocoa this method produces different results to its Cocoa counterpart.
mantissa | the mantissa of the decimal number |
exponent | the exponent of the number |
flag | true if number is negative |
Definition at line 364 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1173 of file CPDecimalNumber.j.
|
implementation |
Initialise a CPDecimalNumber with a string. If the string is badly formed or outside of the acceptable range of a CPDecimal then the number is initialised to NaN.
numberValue | the string to parse. |
Definition at line 384 of file CPDecimalNumber.j.
|
implementation |
Initialise a CPDecimalNumber with a string using the given locale. If the string is badly formed or outside of the acceptable range of a CPDecimal then the number is initialised to NaN. NOTE: Locales are currently not supported.
numberValue | the string to parse |
locale | the CPLocale object to use when parsing the number string |
Definition at line 398 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1183 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1193 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1203 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Definition at line 1213 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1223 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 878 of file CPDecimalNumber.j.
|
implementation |
Compare the receiver CPDecimalNumber to aNumber
and return YES if equal.
aNumber | an object of kind CPNumber to compare against. |
Reimplemented from CPNumber.
Definition at line 961 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 888 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 898 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber with the maximum permissible decimal number value. Note: this is different to the number Cocoa returns. See CPDecimalNumber class description for details.
Definition at line 483 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber with the minimum permissible decimal number value. Note: this is different to the number Cocoa returns. See CPDecimalNumber class description for details.
Definition at line 494 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber initialised to NaN.
Definition at line 503 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aBoolean.
aBoolean | a JS boolean value |
Reimplemented from CPNumber.
Definition at line 971 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aChar.
aChar | a JS int value |
Reimplemented from CPNumber.
Definition at line 981 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aDouble.
aDouble | a JS float value |
Reimplemented from CPNumber.
Definition at line 991 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aFloat.
aFloat | a JS float value |
Reimplemented from CPNumber.
Definition at line 1001 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with anInt.
anInt | a JS int value |
Reimplemented from CPNumber.
Definition at line 1011 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aLong.
aLong | a JS int value |
Reimplemented from CPNumber.
Definition at line 1021 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aLongLong.
aLongLong | a JS int value |
Reimplemented from CPNumber.
Definition at line 1031 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aShort.
aShort | a JS int value |
Reimplemented from CPNumber.
Definition at line 1041 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aChar.
aChar | a JS int value |
Reimplemented from CPNumber.
Definition at line 1051 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with anUnsignedInt.
anUnsignedInt | a JS int value |
Reimplemented from CPNumber.
Definition at line 1061 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aChar.
aChar | a JS int value |
Reimplemented from CPNumber.
Definition at line 1071 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with anUnsignedLongLong.
anUnsignedLongLong | a JS int value |
Definition at line 1081 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with anUnsignedShort.
anUnsignedShort | a JS int value |
Reimplemented from CPNumber.
Definition at line 1091 of file CPDecimalNumber.j.
|
implementation |
The objective C type string. For compatibility reasons
Definition at line 790 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber initialised to one (1.0).
Definition at line 521 of file CPDecimalNumber.j.
|
implementation |
Set the default CPDecimalNumberHandler
object. This is a framework wide setting. All subsequent decimal number operations will use this behaviour.
behavior | the new default CPDecimalNumberHandler object |
Definition at line 472 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 908 of file CPDecimalNumber.j.
|
implementation |
Returns a string representation of the decimal number.
Reimplemented from CPNumber.
Definition at line 819 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 918 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 928 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 938 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 948 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber initialised to zero (0.0).
Definition at line 512 of file CPDecimalNumber.j.