|
Decimal | abs () const |
|
Decimal | add (Mixed aOther) const |
| Add two values and return result. More...
|
|
Decimal | ceil () const |
| If the value is not integer, round up to next integer (towards +Infinity). More...
|
|
| Decimal () |
| Default constructor.
|
|
| Decimal (Mixed aValue) |
| Initializing constructor. More...
|
|
Decimal | div (Mixed aOther) const |
| Divide by other value and return result. More...
|
|
Decimal | down () const |
| If the value is not integer, round down to next integer (towards 0). More...
|
|
Decimal | exp () const |
| Calculate e to the power of this value and return the result. More...
|
|
Decimal | floor () const |
| If the value is not integer, round down to next integer (towards -Infinity). More...
|
|
Decimal | idiv (Mixed aOther) const |
| Divides this value by the other value and returns the integer part of the result. More...
|
|
Boolean | isEQ (Mixed aOther) const |
| Compare with other value for equality. More...
|
|
Boolean | isFinite () const |
| Check if value is neither infinite nor a NaN. More...
|
|
Boolean | isGE (Mixed aOther) const |
| Compare if greater than or equal to other value. More...
|
|
Boolean | isGT (Mixed aOther) const |
| Compare if greater than other value. More...
|
|
Boolean | isInfinite () const |
| Check if value is infinite. More...
|
|
Boolean | isInteger () const |
| Check if value is finite and has exponent=0. More...
|
|
Boolean | isLE (Mixed aOther) const |
| Compare if less than or equal to other value. More...
|
|
Boolean | isLT (Mixed aOther) const |
| Compare if less than other value. More...
|
|
Boolean | isNaN () const |
| Check if value is not a number. More...
|
|
Boolean | isNE (Mixed aOther) const |
| Compare with other value for un-equality. More...
|
|
Boolean | isNegative () const |
| Check if value is negative. More...
|
|
Decimal | ln () const |
| Calculate the natural logarithm (to base e) of this value and return the result. More...
|
|
Decimal | log10 () const |
| Calculate the logarithm to base ten of this value and return the result. More...
|
|
Decimal | max (Mixed aOther) const |
| Compare this value with the other and return the larger (nearer towards +Infinity). More...
|
|
Decimal | min (Mixed aOther) const |
| Compare this value with the other and return the smaller (nearer towards -Infinity). More...
|
|
Decimal | mod (Mixed aOther) const |
| Integer divide (see idiv) by other value and return remainder as result. More...
|
|
Decimal | mul (Mixed aOther) const |
| Multiply with other value and return result. More...
|
|
Decimal | power (Mixed aOther) const |
| Raises this value to the power of the other value and returns the result. More...
|
|
Decimal | rounded (Mixed aDecPlaces) const |
| Round to an number of decimal places. More...
|
|
Decimal | sqrt () const |
| Calculate the square root of this value and return the result. More...
|
|
Decimal | sub (Mixed aOther) const |
| Subtract second value and return result. More...
|
|
String | toString () const |
| Converts the value to a string. More...
|
|
String | toString (Number aDecPlaces, Number aMaxSize=36) const |
| Converts a decimal value to a string of format [-]9.9. More...
|
|
Decimal | up () const |
| If the value is not integer, round up to next integer (away from 0). More...
|
|
A class for decimal arithmetics.
The arithmetic of the standard Number values in Javascript is mathematically correct, however for many applications the results are inconvenient. It becomes then an issue when real (non-integer) numbers are involved in calculations:
var x1 = 1000.43;
var y1 = 1000;
void print(...)
Print the expressions (applying toString() if necessary) to the console standard output (stdout),...
Despite the result your computer is not broken, nor does it calculate inexact. The reason for this surprising output is that standard floating point works binary and with a finite number of digits, and there is a difference when the last digit must be rounded in calculations. Often you won't notice a delta like in the example above, but because it happens and gets multiplied with the number of operations, it's a serious problem even if the final result is rounded to few decimal places and so seems correct. But one day there could be a case where it is not.
And now the same calculation with the decimal class:
A class for decimal arithmetics.
Definition: bps.Decimal.js:80
The bps extension is a namespace assembling general BPS properties and functions.
Definition: bps.AsyncIO.js:1
USAGE HINTS
None of the regular methods change the variable value, only an assignment to the value property can change it. Also there is a technical difference between assigning to the variable, and assigning to the value property:
a.value = 11;
a = b;
a = 11;
Number value
Value of the variable.
Definition: bps.Decimal.js:84
Since the value stays unchanged by the regular methods, translating a calculation from Number to Decimal is easy, however the notation needs to be rearranged to emulate the use of brackets:
var n = 3;
n = 2 * (n + 5);
print( d.add(5).mul(2) );
d.value = d.add(5).mul(2);
Decimal's will automatically be converted to Number's in calculations using standard math operators, for example:
var y = x + 10;
x = x * 2;
While decimal arithmetic is best suited for commercial or financial purposes, binary arithmetic is faster because it is the native number representation for computers and is handled by hardware floating point processors. Decimal arithmetic would perform bad for heavy number crunching like in graphic libraries for example. (No rule without exception however: mainframes have decimal hardware processors, but you will most likely not use mainframes as client PC's).
IMPLEMENTATION AND CREDITS
Decimal uses IBM's decNumber library to implement a numeric variable according to the General Decimal Arithmetic Specification in ANSI C. This specification defines a decimal arithmetic which meets the requirements of commercial, financial, and human-oriented applications. It also matches the decimal arithmetic in the IEEE 754 Standard for Floating Point Arithmetic.
Decimal encapsulates decQuad and uses decimal128, a 128-bit decimal floating-point representation, to provide 34 decimal digits of precision in a compressed format. A Decimal variable occupies 16 bytes.
Many thanks to IBM and IBM Fellow Mike Cowlishaw from the UK Laboratories, for making decNumber package widely available under the terms of ICU v1.8.1.