Patashu / break_eternity.js

A Javascript numerical library to represent numbers as large as 10^^1e308 and as small as 10^-10^^1e308. Sequel to break_infinity.js, designed for incremental games.
MIT License
120 stars 43 forks source link

Wrong factorial approximation #162

Closed hypcos closed 3 months ago

hypcos commented 5 months ago
  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

James103 commented 5 months ago

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).

MathCookie17 commented 4 months ago

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

MathCookie17 commented 3 months ago

Fixed.