From 7eed848a178cbecae7131434eed1eaab81709a85 Mon Sep 17 00:00:00 2001 From: Mohit Agarwal Date: Tue, 18 Oct 2016 14:00:30 +0530 Subject: [PATCH] math: speed up Gamma(+Inf) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add special case for Gamma(+∞) which speeds it up: benchmark old ns/op new ns/op delta BenchmarkGamma-4 14.5 7.44 -48.69% The documentation for math.Gamma already specifies it as a special case: Gamma(+Inf) = +Inf The original C code that has been used as the reference implementation (as mentioned in the comments in gamma.go) also treats Gamma(+∞) as a special case: if( x == INFINITY ) return(x); Change-Id: Idac36e19192b440475aec0796faa2d2c7f8abe0b Reviewed-on: https://go-review.googlesource.com/31370 Reviewed-by: Robert Griesemer --- src/math/gamma.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/math/gamma.go b/src/math/gamma.go index 514260be05..cc9e869496 100644 --- a/src/math/gamma.go +++ b/src/math/gamma.go @@ -133,6 +133,8 @@ func Gamma(x float64) float64 { switch { case isNegInt(x) || IsInf(x, -1) || IsNaN(x): return NaN() + case IsInf(x, 1): + return Inf(1) case x == 0: if Signbit(x) { return Inf(-1)