A quick overview of numbers
Z - Set of all negative and positive integers (…-2,-1,0,1,2…)
Q - Rational numbers – set of all numbers which can be expressed as a/b where a, b∈ Z and b ≠ 0.
P - Irrational numbers, like π (3.14159...), e (2.71828...) , and √2 (1.4142...) which cannot be expressed as a rational number
R - Real numbers, union of irrational and rational numbers: P ∪ Q
The set of integers (Z) can be expressed in base 2. Each place is a power of 2.
|power of 2||2^7||2^6||2^5||2^4||2^3||2^2||2^1||2^0|
Decimal and binary representation of some integers
Integers (Z) can easily be expressed as binary digits. However, not all fractions (rational numbers) can be represented in binary. The reason is converting these to binary results in an repeating number. ⇒See this article for information on decimals and binary, and here on ⇒StackOverFlow. This is also a study of numerical analysis.
How do you solve this problem?
Use a 3rd party math library. For my example I used the ⇒BigDecimal.js library. To make the BigDecimal.js library to work with BPM and our class loader, use the uncompressed JS files and put the MathContext.js contents pasted before the BigDecimal.js file in a single file. Then add this file as a managed asset to the application. From here you can then create statements like this.
var a = new BigDecimal(tw.local.n1.toString());
var b = new BigDecimal(tw.local.n2.toString());
a = a.add(b);
tw.local.n3 = a.toString();
Then you can get the values you want. For multiplying decimal values, these can easily add extra digits. For this you can add statement like
a = a.setScale(tw.local.customScale,
Where tw.local.customScale is an integer with the default value of 2. To enable the rounding, click the round in the BigDecimal example or the floor checkbox on the decimal version. For the plain JS Decimal version, you can use the technique of multiply by 100 and then floor, then divide by 100. Here I have an environment variable holding the value 100, so it could be changed to 1, 10, 1000 and so on.
var d1 = tw.local.n1;
var d2 = tw.local.n2;
var d3 = d1+d2;
tw.local.n3 = Math.floor(d3*tw.env.precisionDecimal) / tw.env.precisionDecimal;
The Example TWX file is available in the BPM L2 Sample Apps section.
Change History: 1/19/2018 - Updated links to GIT repository
THE DOWNLOAD(S) ARE PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT OF PATENTS, COPYRIGHTS OR OTHER PROPRIETARY RIGHTS OF OTHERS. NEITHER THE AUTHOR NOR IBM WARRANT THAT THE FUNCTIONS, ROUTINES, AND DATA CONTAINED IN, OR GENERATED AS A RESULT OF THE DOWNLOAD(S), WILL MEET YOUR REQUIREMENTS OR BE ERROR-FREE. The entire risk related to the quality and performance of the download(s) and sample(s) is with you. In the event that there is any defect, you assume the entire cost of all necessary services, repair or correction. IN NO EVENT WILL THE AUTHOR OR IBM BE LIABLE TO YOU OR TO ANY THIRD PARTY FOR ANY DIRECT OR INDIRECT DAMAGES (INCLUDING, WITHOUT LIMITATION, LOST PROFITS, LOST SAVINGS OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES) ARISING OUT OF THE USE OR INABILITY TO USE THE DOWNLOAD(S), EVEN IF THE AUTHOR OR IBM HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Some jurisdictions do not allow the exclusion of implied warranties or the limitation or exclusion of liability for incidental or consequential damages, so some of the above may not apply to you."