This issue happens on 32 bits builds of PHP and nodejs : The language switches to a floating point representation when the result of some operation exceeds INT_MAX. In 64 bits PHP builds, the computation is done right.
Sum of 1 to 1000000000 in different programming languages
11–20 of 83 posts
Re: Sum of 1 to 1000000000 in different programming languages
#12Re: Sum of 1 to 1000000000 in different programming languages
#13/*author: Gauss */ var n = 1000000000; var sum = n*(n+1)/2;
"The correct answer can be calculated using
1 + 2 + ... + n = n(n+1)/2"
Re: Sum of 1 to 1000000000 in different programming languages
#14Re: Sum of 1 to 1000000000 in different programming languages
#15Earlier quoted context omitted.
Python: sum(range(1000000000)) :)
Did you intend to demonstrate a very common off-by-one error in Python?
I believe in 3.x range has been replaced with xrange.
Re: Sum of 1 to 1000000000 in different programming languages
#16This issue happens on 32 bits builds of PHP and nodejs : The language switches to a floating point representation when the result of some operation exceeds INT_MAX. In 64 bits PHP builds, the computation is done right.
32-bit or 64-bit won't matter for Node.js. The Number type in JS is specifically defined as using the 64-bit floating point format as defined by IEEE 754, except that all NaNs are coerced to a single value. In terms of the abstraction, there is never a cast when the value overflows; it should just always be considered a double. Under the hood, there may be differences in how the number is actually being treated.
Re: Sum of 1 to 1000000000 in different programming languages
#17This issue happens on 32 bits builds of PHP and nodejs : The language switches to a floating point representation when the result of some operation exceeds INT_MAX. In 64 bits PHP builds, the computation is done right.
32-bit or 64-bit won't matter for Node.js. The Number type in JS is specifically defined as using the 64-bit floating point format as defined by IEEE 754, except that all NaNs are coerced to a single value. In terms of the abstraction, there is never a cast when the value overflows; it should just always be considered a double. Under the hood, there may be differences in how the number is actually being treated.
>>> Math.pow(2, 53) === Math.pow(2, 53) - 1
false
>>> Math.pow(2, 53) === Math.pow(2, 53)
true
>>> Math.pow(2, 53) === Math.pow(2, 53) + 1
true // derpRe: Sum of 1 to 1000000000 in different programming languages
#18 +/⍳1E9
Try it yourself, download NARS2000 (free) here:The "⍳" character is entered by typing ALT+i
Explanation:
1E9 is 1,000,000,000
⍳1E9 generates a vector containing integers from 1 to 1,000,000,000 (inclusive)
+/ is the sum of the vector
Any good APL interpreter will not actually generate a vector with a billion numbers but rather recognize the above expression and optimize the resulting operation for speed and minimal resource utilization.EDIT:
Technically the "/" is the "reduction" operator acting along the last axis. In the case of a single dimensional array it acts along the only available axis. If, instead, it was acting on a matrix it would reduce along the columns. Here's a longer annotated example with output from the interpreter:
Generate a vector from 1 to 10:
⍳10
1 2 3 4 5 6 7 8 9 10
Sum:
+/⍳10
55
Generate a vector of ten one's and zero's, repeating until the end:
10⍴1 0
1 0 1 0 1 0 1 0 1 0
Now use that vector to reduce the original 1 to 10 vector, grabbing every other
element starting with the first. The effective result is that you end up with
all the odd numbers between 1 and 10:
(10⍴1 0)/⍳10
1 3 5 7 9
Same things, now grabbing the even numbers by flipping the 1 0 sequence to 0 1:
(10⍴0 1)/⍳10
2 4 6 8 10
Sum of all odd integers between 1 and 10:
+/(10⍴1 0)/⍳10
25
Sum of all even integers between 1 and 10:
+/(10⍴0 1)/⍳10
30
One could create a single vector with the odd integers between 1 and 10 followed by
the even integers between 1 and ten by simply concatenating the generating
expressions (APL executes from right to left):
((10⍴1 0)/⍳10),(10⍴0 1)/⍳10
1 3 5 7 9 2 4 6 8 10
And then you can reshape ("⍴") the result into a matrix:
2 5⍴((10⍴1 0)/⍳10),(10⍴0 1)/⍳10
1 3 5 7 9
2 4 6 8 10
Finally, use the scan operator again, now applied to a matrix, to sum along the
columns and produce a result for each row. The effect is to output a two element
vector with the sum of the odd integers between 1 and 10 as the first element
and the sum of all the even integers between 1 and 10 as the second:
+/2 5⍴((10⍴1 0)/⍳10),(10⍴0 1)/⍳10
25 30
If you want to try this type the lines immediately following my comments above right into the interpreter. The rho "⍴" or reshape operator is entered by typing ALT+r.Hope this helps make sense of it. Of course, there are other ways to accomplish the same thing.
Re: Sum of 1 to 1000000000 in different programming languages
#19It would have been interesting to see this problem solved in many different languages. But I guess that would kill the question on Stackoverflow.
I don't think it would be that interesting - and I don't think we need to rediscover the fact that some languages use IEEE754 as the default number type over and over again
Re: Sum of 1 to 1000000000 in different programming languages
#20Earlier quoted context omitted.
32-bit or 64-bit won't matter for Node.js. The Number type in JS is specifically defined as using the 64-bit floating point format as defined by IEEE 754, except that all NaNs are coerced to a single value. In terms of the abstraction, there is never a cast when the value overflows; it should just always be considered a double. Under the hood, there may be differences in how the number is actually being treated.
I've heard this before and never understood why. Why?
As for why? I have no clue. It's specified in the specification. That's all. There are ToInteger(), ToInt32(), ToUint16(), ToUInt32 functions defined as well, but I think that's for the host to implement
EDIT: oops. there isn't a ToInt64() function defined.