Decimal floating point number. More...
#import <CPDecimalNumber.h>
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 324 of file CPDecimalNumber.j.
|
implementation |
Returns a JS bool representation.
Reimplemented from CPNumber.
Definition at line 864 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 873 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 792 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 549 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 562 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 623 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 637 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 660 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 674 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 698 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 712 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 736 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 750 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 776 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 585 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 599 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 415 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 428 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 440 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 454 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimal object (which effectively contains the internal decimal number representation).
Reimplemented from CPNumber.
Definition at line 844 of file CPDecimalNumber.j.
|
implementation |
Return the default CPDecimalNumberHandler
object.
Definition at line 463 of file CPDecimalNumber.j.
|
implementation |
Returns a string representation of the decimal number.
Reimplemented from CPNumber.
Definition at line 814 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 825 of file CPDecimalNumber.j.
|
implementation |
Returns a JS float representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 854 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 1285 of file CPDecimalNumber.j.
|
implementation |
Returns a JS float representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 883 of file CPDecimalNumber.j.
|
implementation |
Initialise a CPDecimalNumber object with NaN
Reimplemented from CPObject.
Definition at line 335 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with a boolean value.
value | a JS boolean value |
Reimplemented from CPNumber.
Definition at line 1116 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1128 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 1265 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 345 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with a float value.
value | a JS float value |
Reimplemented from CPNumber.
Definition at line 1138 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with a float value.
value | a JS float value |
Reimplemented from CPNumber.
Definition at line 1148 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1158 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1168 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1178 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 365 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1188 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 385 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 399 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1198 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1208 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1218 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Definition at line 1228 of file CPDecimalNumber.j.
|
implementation |
Initialise the receiver with an int value.
value | a JS int value |
Reimplemented from CPNumber.
Definition at line 1238 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 893 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 976 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 903 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 913 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 484 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 495 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber initialised to NaN.
Definition at line 504 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aBoolean.
aBoolean | a JS boolean value |
Reimplemented from CPNumber.
Definition at line 986 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aChar.
aChar | a JS int value |
Reimplemented from CPNumber.
Definition at line 996 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aDouble.
aDouble | a JS float value |
Reimplemented from CPNumber.
Definition at line 1006 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aFloat.
aFloat | a JS float value |
Reimplemented from CPNumber.
Definition at line 1016 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with anInt.
anInt | a JS int value |
Reimplemented from CPNumber.
Definition at line 1026 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aLong.
aLong | a JS int value |
Reimplemented from CPNumber.
Definition at line 1036 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aLongLong.
aLongLong | a JS int value |
Reimplemented from CPNumber.
Definition at line 1046 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aShort.
aShort | a JS int value |
Reimplemented from CPNumber.
Definition at line 1056 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aChar.
aChar | a JS int value |
Reimplemented from CPNumber.
Definition at line 1066 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with anUnsignedInt.
anUnsignedInt | a JS int value |
Reimplemented from CPNumber.
Definition at line 1076 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with aChar.
aChar | a JS int value |
Reimplemented from CPNumber.
Definition at line 1086 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with anUnsignedLongLong.
anUnsignedLongLong | a JS int value |
Definition at line 1096 of file CPDecimalNumber.j.
|
implementation |
Create a new CPDecimalNumber initialised with anUnsignedShort.
anUnsignedShort | a JS int value |
Reimplemented from CPNumber.
Definition at line 1106 of file CPDecimalNumber.j.
|
implementation |
The objective C type string. For compatibility reasons
Definition at line 805 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber initialised to one (1.0).
Definition at line 522 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 473 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 923 of file CPDecimalNumber.j.
|
implementation |
Returns a string representation of the decimal number.
Reimplemented from CPNumber.
Definition at line 834 of file CPDecimalNumber.j.
|
implementation |
Reimplemented from CPNumber.
Definition at line 529 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 933 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 943 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 953 of file CPDecimalNumber.j.
|
implementation |
Returns a JS int representation. Truncation may occur.
Reimplemented from CPNumber.
Definition at line 963 of file CPDecimalNumber.j.
|
implementation |
Returns a new CPDecimalNumber initialised to zero (0.0).
Definition at line 513 of file CPDecimalNumber.j.