Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function miscalculation [duplicate]

Tags:

javascript

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.

like image 422
angel Avatar asked Sep 24 '14 07:09

angel


People also ask

What is the meaning of miscalculations?

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.

What are the most common miscalculations in writing?

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.

Are You making miscalculations as a leader?

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.”

What is the formula to calculate a duplicate using the sum function?

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")


1 Answers

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/

like image 138
Derek 朕會功夫 Avatar answered Oct 21 '22 17:10

Derek 朕會功夫