Live data from Hacker News

Sum of 1 to 1000000000 in different programming languages

stackoverflow.com

21–30 of 83 posts

Re: Sum of 1 to 1000000000 in different programming languages

#23
post #16

Earlier 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?

Presumably because it allows you to do anything involving double precision or 32-bit integer arithmetic, and performance was not originally a major consideration. It's pretty rare to need more than 53 bits of precision (and was even rarer for JS's original intent), so it makes sense that the numeric type is kept simple. Edit: and to clarify, the advantage is that this makes basic implementation extremely simple. Only if you want to optimize your engine's performance do you have to worry about shuffling types around.

These days the solution for having more precision is to use an external library. I think that's generally fine, although I think performance is a concern. Financial applications aside, working with arbitrary precision is a good hint that you might be doing something processor intensive. It's certainly a case where I'd like the library to be compiled to target asm.js, and maybe optionally NaCL, once those have widespread adoption. Ideally, ECMAScript would also have a native implementation, but that won't eliminate the need for a library for shimming for years to come.

Re: Sum of 1 to 1000000000 in different programming languages

#24
post #10

Haskell foldl' (+) 0 [1..1000000000] You could use sum, but that will eat up a lot of RAM because of the laziness. EDIT: For the fun of it, I decided to do the same in a slightly more esoteric language, so here's a Prolog version (given that your stack is big enough) rangesum(0,0). rangesum(N,X) :- M is N - 1, rangesum(M,Y), X is Y + N. ?- rangesum(1000000000, X), write(X).

sum is fine in GHC. It is specialised for Integer. GHCi uses naive sum though.

Re: Sum of 1 to 1000000000 in different programming languages

#25
Someone replied the following in there:

"The key in this case is using C99's long long data type. It provides the biggest primitive storage C can manage (128-bits on 32-bit machines and 256-bits on a 64-bit machine) and it runs really, really fast."

Isn't long long "typically" 64-bit? (I know the C standard doesn't actually specify any actual size).

What platform does this long long type really give you the full 128 or 256 bits on?

And do 64-bit CPU's indeed support 256 bit integer types? If so, what can I do to play with it!! C does not provide it for me on Linux!

Thanks :)

Re: Sum of 1 to 1000000000 in different programming languages

#26
post #22

Knowing how to use a language is critical to get expected results. This gives the proper result in PHP by forcing the integer cast. $sum = (int) $sum + $i;

This casts only $sum to int. If $i is a float, the result is a float.

With 32 bits, $sum will be a float bigger than PHP_INT_MAX at some point, and the cast will truncate it, which is unlikely to give the right answer.

With 64 bits, which I assume is the platform you tested on, $sum is never bigger than PHP_INT_MAX, so it's never converted to a float, and the (int) cast does nothing (and the computation is done right).

Re: Sum of 1 to 1000000000 in different programming languages

#27

Someone replied the following in there: "The key in this case is using C99's long long data type. It provides the biggest primitive storage C can manage (128-bits on 32-bit machines and 256-bits on a 64-bit machine) and it runs really, really fast." Isn't long long "typically" 64-bit? (I know the C standard doesn't actually specify any actual size). What platform does this long long type really give you the full 128…

If you want a guaranteed 64-bit type, put in your code:

#include

then, use uint64_t for unsigned and int64_t for signed. If you want 128 bits, in gcc you can use __uint128_t (it has two extra underscores at the beginning because that size is nonstandard), but I don't think there is support for 256 bit integers.

Try a big integer library: http://stackoverflow.com/questions/124332/c-handling-very-la...

Re: Sum of 1 to 1000000000 in different programming languages

#28
post #22

Knowing how to use a language is critical to get expected results. This gives the proper result in PHP by forcing the integer cast. $sum = (int) $sum + $i;

It's been a long time since I've worked with PHP, I assume

    $sum += (int) $i;
will still convert to float once the size of $sum gets to the requisite size?

Re: Sum of 1 to 1000000000 in different programming languages

#29
Clojure:

    (reduce + (range 1000000001))
Written that way, though, it takes a long time (108 sec, compared to 2.6 sec in C). So that's not fast, as elegant as it may be.

This is what I have for C. It's probably not great C code.

    int main() {
      long res = 0;

      int i;
      for (i = 0; i 
Faster than (naive?) C is this Clojure loop (no range object).

    user=> (time (loop [i 0 res 0] 
              (if (> i 1000000000) 
              res
              (recur (inc i) (+ res i)))))
    "Elapsed time: 1612.094 msecs"
    500000000500000000

Re: Sum of 1 to 1000000000 in different programming languages

#30

Someone replied the following in there: "The key in this case is using C99's long long data type. It provides the biggest primitive storage C can manage (128-bits on 32-bit machines and 256-bits on a 64-bit machine) and it runs really, really fast." Isn't long long "typically" 64-bit? (I know the C standard doesn't actually specify any actual size). What platform does this long long type really give you the full 128…

long long is merely required to be at least 64-bit and in practice is 64-bit on every platform. AFAIK no general-purpose processors support 256-bit ints. AVX2 has 256-bit vector operations, but that is not at all the same thing.
Post reply on HN