So, what is the numerical error that bites the first code, forcing it to punt to “regular” Mathematica but not the other? Is it simply the very long loop that causes this problem? Thanks.
Notice that the result for n=13 is a real number rather than an integer. With the nondefault option setting, the compiled code apparently allows a transfer from machine integers to machine doubles. Whichmakes good sense, given the name of the option.
If you are trying to time a basic flop count this might not pose any problem. This is especially true if the underlying processor speeds are roughly equal for machine ints vs. doubles. If you really need to be certain of arithmetic speed on machine ints, then you will need to change the code to stay under the maximum size for those.
Thanks, Frank. You’re right – I’m more interested in loops. The summing example just starts discussion. Your use of Sum, however, is a good observation.
Thanks, Dan. I was afraid of that but that’s the way it goes.
The code below works but can it be simpler? The module declares s = 0. and not s = 0 because the latter never returned an answer. I used the gamma function to get a floating point result – (1.0 n)! didn’t work.