I have this function which is simply a sum and a product. For some values work for others not in the sense does not return the correct result.
Codia function (r, c) {
return ((r + c) * (r + c + 1));
}
alert(Codia(1908229752,0));
Result obtained by the function: 3641340788326211000
Result calculated by me: 3641340788326211256
Can you tell me where am I wrong.
Definition of miscalculation : a mistake in calculation : wrong calculation a costly miscalculation … ruled that utility shareholders, not electricity consumers, must pay for some or all of the miscalculations that resulted in excessive expansion or cost overruns.
Here are six of the most common miscalculations and how we can avoid them. Miscalculation #1: Your words. If you have ever gone to a gun range for target shooting, you have an appreciation for the phrase, “Ready, fire, aim.” Dumb and dangerous may be a good summary of that strategy! And the same can be true with our words.
We are all fallible people, and as leaders, we have all likely made miscalculations somewhere, sometime. Here are six of the most common miscalculations and how we can avoid them. Miscalculation #1: Your words. If you have ever gone to a gun range for target shooting, you have an appreciation for the phrase, “Ready, fire, aim.”
After that, the SUM function adds up the numbers, and if the sum is greater than 1, the IF function reports a "Duplicate". For our sample dataset, the formula goes as follows: =IF (SUM ((--EXACT ($A$2:$A$8,A2)))<=1,"","Duplicate")
JavaScript isn't designed to calculate with great accuracy. Once you have floats and doubles or numbers that are greater than Number.MAX_SAFE_INTEGER
(which is 9007199254740991), numbers will start to lose their accuracy. Here's a comparison of your expected answer and the max safe integer:
3,641,340,788,326,211,256 EXPECTED ANSWER
9,007,199,254,740,991 MAX_SAFE_INTEGER
To fix this, either use a library that is designed to do arithmetics with big numbers, or design a new algorithm yourself.
Here's an example using BigNumber.js which returns the correct answer of 3641340788326211256: http://jsfiddle.net/DerekL/jj47touj/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With