Skip to content
Related Articles

Related Articles

Improve Article

Why does changing the sum order returns a different result ?

  • Last Updated : 31 Dec, 2020

In mathematics, addition is associative in nature. The associative law states that the result of summation remains the same irrespective of the order of addends. This is true for all programming languages when the addends are integers. However, there is a slight difference in behavior when the addends are floating-point numbers.

Most of the CPUs in use today use the IEEE 754 binary floating-point standard, which introduces inaccuracies when decimal numbers are stored as binary values. The change in sum result is observed because when we change the order the intermediate value stored in the memory also changes and therefore the end result also changes. Let us consider the below example that shows how different outputs are obtained whenever the order is changed.

Example:

Javascript




var a = 10.54;
var b = 9.99;
var c = 1.01;
  
console.log("a+b+c", a+b+c);
console.log("b+c+a", b+c+a);
console.log("b+a+c", b+a+c);

Output:



a+b+c 21.540000000000003
b+c+a 21.54
b+a+c 21.540000000000003

Solution: A solution to this problem is using the BigDecimal class. This class in JavaScript represents floating-point numbers of arbitrary precision. The BigDecimal makes it a point to round up the floating-point numbers in mathematical operations where changing the order may change the result. This package needs to be imported into the program using the require function. 

The package can be installed using the below npm command.

npm install js-big-decimal

Approach 1: The package contains an inbuilt add() method which takes two string arguments, adds them, and returns the rounded result. This can be used to correctly add multiple numbers.

Example:

Javascript




var bigDecimal = require('js-big-decimal');
  
var a = "10.54";
var b = "9.99";
var c = "1.01";
  
console.log("a = ", a);
console.log("b = ", b);
console.log("c = ", c);
  
var ab = "" + bigDecimal.add(a, b);
var abc = bigDecimal.add(ab, c);
console.log("a+b+c = ", abc);
  
var bc = "" + bigDecimal.add(b, c);
var bca = bigDecimal.add(bc, a);
console.log("b+c+a = ", bca);
  
var ba = "" + bigDecimal.add(b, a);
var bac = bigDecimal.add(ba, c);
console.log("b+a+c = ", bac);

Output:

a =  10.54
b =  9.99
c =  1.01
a+b+c =  21.54
b+c+a =  21.54
b+a+c =  21.54

Approach 2: Another approach is using the instance property of the BigDecimal class. The new operator is used to create instances of the BigDecimal numbers. The add() method invokes an instance and takes another instance as a parameter. The second add() method is invoked by the result returned by the first add() method. Finally, the getValue() method is used to get the string value of the BigDecimal number. As observed the addition result for all three arrangements of numbers is the same.

Example:

Javascript




var bigDecimal = require('js-big-decimal');
  
var a = new bigDecimal("10.54");
var b = new bigDecimal("9.99");
var c = new bigDecimal("1.01");
  
var abc = (a.add(b).add(c)).getValue();
var bca = (b.add(c).add(a)).getValue();
var bac = (b.add(a).add(c)).getValue();
  
console.log("a+b+c = ", abc);
console.log("b+c+a = ", bca);
console.log("b+a+c = ", bac);

Output:

a+b+c =  21.54
b+c+a =  21.54
b+a+c =  21.54



My Personal Notes arrow_drop_up
Recommended Articles
Page :