0

function add1(n) {
      let sum = 0;
      for(let i = 1; i <= n; i++) {
        sum = sum + i;
      }
      console.log(sum);
    }
    function add2(n) {
      sum = n * (n + 1) / 2;
      console.log(sum);
     }
     add1(134217730);
     add2(134217730);

When I ran this code with the value of 134217729 fun add1() working fine. after this value it print different result.

and when I ran this concept in C, most of the time both functions give same result.

#include<stdio.h>
#include<conio.h>

typedef unsigned long long int ulli;

ulli add1(ulli num) {
    ulli i,total=0;
    for(i = 1; i <= num; i++) {
        total += i;
    }
    printf("Result of add1 is %llu\n", total);
    return total;
}

ulli add2(ulli num) {
    ulli total;
    total = (ulli)(num * (num + 1) /2.0);
    printf("Result of add2 is %llu\n", total);
    return total;
}


int main() {
    ulli num;
    
    printf("Enter A Number");
    scanf("%llu", &num);
    printf("Number is %llu\n",num);
    
    add1(num);
    add2(num);

    return 0;
}

I have no Idea why this kind of strange output I have seen. is there any problem in my code. or this is internal behaviour of V8 engine. or something else.

  • 3
    I think the number being generated is bigger than the max safe integer, which means it can be different. Consider using BigInt instead? `add1(134217730n);` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt – evolutionxbox Mar 31 '21 at 19:14
  • Does this answer your question? [How to deal with floating point number precision in JavaScript?](https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) – evolutionxbox Mar 31 '21 at 19:18
  • 3
    Addtionally, the clue should have been in the different data types - you've mixed apples and oranges. In the C code you typed the values a `unsigned long int` while in JavaScript those numbers are like doubles in C. Change the data type in your C code to an unsigned double and see what result turns up out of curiosity. – Randy Casburn Mar 31 '21 at 19:18
  • @RandyCasburn Unsigned double? – Bergi Mar 31 '21 at 20:03
  • @Bergi Oh, that's a funny typo. `long double` probably won't make the compiler throw up. :-) – Randy Casburn Mar 31 '21 at 20:08

1 Answers1

0

The problem is not in the functions but in JavaScript itself. In contrary to C, JavaScript stores all numbers as 64-bit floating-point numbers according to the IEEE 754 standard.

Because of this, integers have only 53 bits to fit in (the highest bit is hidden). Therefore, if the value is larger than 9,007,199,254,740,992, the precision is lost. You can see it in the following example:

console.log(9007199254740992+1)

Because the values your functions return are higher than the maximum value which fits in the 53 bits, the numbers experience rounding errors during the calculations.

If you really need to work with such large numbers, consider using the BigInt objects, which can hold values larger than 2^53 without a loss in precision.

Petr Fiedler
  • 278
  • 2
  • 10