I have been using BigNumber.js library for high precision arithmetic calculation. To maintain higher precision I am using toFixed
method of the Bignumber object like this:
(new BigNumber(6000 )).minus(9006000).div(9006000).times(4503000 ).plus(4503000 ).toFixed()
The above code gives result as 2999.99999999999998275
. I tried validating this calculation result in C# using decimal
data type because decimal has higher precision than double but the result was different at granular level.
decimal rg1 = 6000m;
decimal lastSavedRG1 = 9006000m;
decimal lastsavedrefinedgoalMonthly1 = 4503000m;
decimal cal1 = (lastsavedrefinedgoalMonthly1 + (lastsavedrefinedgoalMonthly1 * ((rg1 - lastSavedRG1) / lastSavedRG1)));
This calculation gives values as 3000.0000000000000000000001
. Any idea why such differences ?