In one of the applications, I observed the some unexpected behavior/results from Appian (possible bug) for basic +/- calculations on decimals.
92000 - (7000.27 + 73000.27 + 2000.33 + 9999.13) => -1.455192e-011 [expected result is "0"]
<Please evaluate above calculations in expression rule>
This happens only certain set of decimal additions, i.e. if only one decimal value is changed in above calculations then we get correct results e.g.:
92000 - (7000.27 + 73000.27 + 2000.33 + 9999.12) => 0.01 (Decimal)
Can Product Support team look into the things and fix this issue? This is very common and basic calculations; and causing very big problem in Application workflow.
Discussion posts and replies are publicly visible
Hi @Vaibhav,
This looks likely to be a rounding error due to Floating Point Arithmetic. I believe Appian uses the Java Primitive double for Number(decimal) under the hood, which is a 64-bit IEEE 754 floating point. IEEE 754 is the technical standard for floating-point computation, this is the most widely used standard for floating point arithmetic and number formats.
The issue is that some numbers have an infinite number of digits, and can not be represented with 100% precision with only 64-bits. But it's not just Pi that can be affected by this, something as simple as 1/10, or 0.1 can be affected.
For instance, the below code would print false to the console in Java:
double x1 = 0.3; double x2 = 0.1 + 0.1 + 0.1; StdOut.println(x1 == x2);
double x1 = 0.3;
double x2 = 0.1 + 0.1 + 0.1;
StdOut.println(x1 == x2);
However, the below code would print true:
double z1 = 0.5; double z2 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1; StdOut.println(z1 == z2);
double z1 = 0.5;
double z2 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
StdOut.println(z1 == z2);
This is just a limitation of the IEEE 754 standard, and there's few ways to get around it. To do so purely in Appian will require some rouding work around, or similiar, as suggest by @manikandanp13
Financial Java applications often use the BigDecimal Class, which provides more precision through larger storage of numbers and more robust mathematical functions. You could consider building a plugin that would allow access to the BigDecimal Class and it's functionality. I did a quick search on shared components and found https://forum.appian.com/suite/tempo/records/item/lMBCLGOdlMUpdGVqW3dQaIKmclBmvvNEj8vu_cjb7T-5YiPr4Fu8ly5Yj1s09uenE4RYzA8zKyx7eiUhe2kLnO1l8v4PFM9U_yr4yMcNagKDuviUQ/view/summary
Some additional reading that goes more into the Rounding issue of Floating Point Arithmetic:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
https://introcs.cs.princeton.edu/java/91float/
Best Regards