6

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.

andrex
  • 983
  • 5
  • 14
angel
  • 163
  • 2
  • 10

1 Answers1

4

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/

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • It may be more meaningful if you compare the number, complete with thousand separators, aligned with the number that OP posted so it's clear which one is bigger. – Niet the Dark Absol Sep 24 '14 at 07:25
  • Thank you, it works. 1) But if the formula looked like this: ((r + c) * (r + c + 1)) / 2 + r How should I change the formula? 2) What is the maximum value calculated? 3) In the second issue new BigNumber (0)). C.join ("") because .c.join ("")? 4 – angel Sep 24 '14 at 07:33
  • 4)If I have this function function Dec (str) { var s = Math.floor ((Math.sqrt (1 + 8 * str) - 1) / 2); this.par str = - s * (s + 1) / 2; this.cod = s - this.par; } How can I change it using the library BigNumber.js Thank you. – angel Sep 24 '14 at 07:41
  • @angel - There are corresponding methods for every arithmetic operators. So `+` would be `.plus()`, `/` would be `.div()`, etc. There is no maximum value according to BigNumber's documentation; apparently the number can go as big as you want. I did `.c.join("")` just to log the readable value into the console. You can also do `.toString()` to get the string containing the number. – Derek 朕會功夫 Sep 24 '14 at 07:42
  • Additionally, there is a complete list of methods you can use [right here](http://mikemcl.github.io/bignumber.js/#prototype-methods) if you need help converting your formula. – Derek 朕會功夫 Sep 24 '14 at 07:44
  • @Derek 朕會功夫 If you do not mind you could convert these two methods, I get confused a bit. xD Cod function (r, c) {return ((r + c) * (r + c + 1)) / 2 + r; } Dec function (str) { var s = Math.floor ((Math.sqrt (1 + 8 * str) - 1) / 2); this.par str = - s * (s + 1) / 2; this.cod = s - this.par; } – angel Sep 24 '14 at 07:46
  • @angel - `this.par str =` isn't valid JavaScript... And why are there `this`'s? – Derek 朕會功夫 Sep 24 '14 at 07:48
  • Sorry function Dec(str){ var s = Math.floor((Math.sqrt (1 + 8 * str) - 1) / 2); this.par = str - s * (s + 1) / 2; this.cod = s - this.par; } could you give me a hand? – angel Sep 24 '14 at 07:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61826/discussion-between-derek--and-angel). – Derek 朕會功夫 Sep 24 '14 at 07:52
  • Error: new BigNumber() base out of range: 0 what? – angel Sep 24 '14 at 08:08