[gnumeric] GAMMA: avoid unnecessary overflow.



commit eb9c1c2c1878ba80c47052d2bdfd788b7f39dccc
Author: Morten Welinder <terra gnome org>
Date:   Fri Nov 1 20:20:53 2013 -0400

    GAMMA: avoid unnecessary overflow.
    
    Gamma(171.5) is fine, but computation would overflow.

 ChangeLog      |    1 +
 src/mathfunc.c |    4 ++++
 2 files changed, 5 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 6df135d..9aae262 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,7 @@
 2013-11-01  Morten Welinder  <terra gnome org>
 
        * src/mathfunc.c (dpois_raw): Handler x=0 as in newer R.
+       (gnm_gamma): Protect against unnecessary overflow at top of range.
 
 2013-10-31  Morten Welinder  <terra gnome org>
 
diff --git a/src/mathfunc.c b/src/mathfunc.c
index a9e9b36..1d91ce1 100644
--- a/src/mathfunc.c
+++ b/src/mathfunc.c
@@ -8874,6 +8874,10 @@ gnm_gamma (gnm_float x)
                        return fact ((int)x - 1);
 
                if (x > 10) {
+                       /* Overflow protection. */
+                       if (x > 170)
+                               return (x - 1) * gnm_gamma (x - 1);
+
                        return gnm_pow (x / M_Egnum, x) /
                                gnm_sqrt (x / M_2PIgnum)
                                * gnm_exp (lgammacor (x));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]