Re Mathematica 10.2 running under Windows 8.
This compiled code adds the integers between 1 and n! inclusive. Yes, it's easier to use the formula n! (n! + 1) / 2 but I'm timing a loop.
g = Compile[{{n, _Integer}}, Module[{s = 0}, Do[s += k, {k, 1, n!}]; s], RuntimeOptions -> {"CatchMachineIntegerOverflow" -> True}, CompilationTarget -> "C"];
It works for n = 12:
AbsoluteTiming@g@12
Out[7]= {3.69531, 114721266640780800}
But fails for n = 13, returning the message "CompiledFunction::cfne: Numerical error encountered; proceeding with uncompiled evaluation. >>"
However, this compiled function works for n = 13:
h = Compile[{{n, _Integer}}, n! (n! + 1) / 2,
RuntimeOptions -> {"CatchMachineIntegerOverflow" -> True},
CompilationTarget -> "C"];
AbsoluteTiming@h@13
Out[2]= {0.0000384889, 1.93879*10^19}
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.