break_eternity.js icon indicating copy to clipboard operation
break_eternity.js copied to clipboard

Wrong factorial approximation

Open hypcos opened this issue 1 year ago • 2 comments

  var f_gamma = function f_gamma(n) {
    //unrelated things
    var l = 0.9189385332046727; //0.5*Math.log(2*Math.PI)
    l = l + (n + 0.5) * Math.log(n);
    l = l - n;
    var n2 = n * n;
    var np = n;
    l = l + 1 / (12 * np);
    np = np * n2;
    l = l + 1 / (360 * np);
    np = np * n2;
    l = l + 1 / (1260 * np);
    np = np * n2;
    l = l + 1 / (1680 * np);
    np = np * n2;
    l = l + 1 / (1188 * np);
    np = np * n2;
    l = l + 691 / (360360 * np);
    np = np * n2;
    l = l + 7 / (1092 * np);
    np = np * n2;
    l = l + 3617 / (122400 * np);
    return Math.exp(l) / scal1;
  };

This looks like the series expansion of ln(n!), but some coefficients are wrong: The l = l + 1 / (360 * np); should be l = l - 1 / (360 * np); The l = l + 1 / (1680 * np); should be l = l - 1 / (1680 * np); The l = l + 691 / (360360 * np); should be l = l - 691 / (360360 * np); The l = l + 3617 / (122400 * np); should be l = l - 3617 / (122400 * np); I guess the mistake also exists in other files than break_eternity.js

hypcos avatar Mar 23 '24 06:03 hypcos

I tested these changes and the corrected f_gamma function appears to be much more accurate (with a relative error around 10-14 instead of 10-6).

James103 avatar Mar 23 '24 06:03 James103

Your fix has been added to my pull request. Hopefully that means it'll get fixed soon.

MathCookie17 avatar Apr 08 '24 14:04 MathCookie17

Fixed.

MathCookie17 avatar May 29 '24 14:05 MathCookie17