Last active
August 29, 2015 14:00
-
-
Save MLLeKander/b9566c2172d8e5ff1410 to your computer and use it in GitHub Desktop.
Fuck goto (sometimes)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reduce argument | |
z := 1.0 | |
for x >= 3 { | |
x = x - 1 | |
z = z * x | |
} | |
for x < 0 { | |
if x > -1e-09 { | |
return handleSmallGammaX(x,z) | |
} | |
z = z / x | |
x = x + 1 | |
} | |
for x < 2 { | |
if x < 1e-09 { | |
return handleSmallGammaX(x,z) | |
} | |
z = z / x | |
x = x + 1 | |
} | |
if x == 2 { | |
return z | |
} | |
x = x - 2 | |
p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gamP[5])*x + _gamP[6] | |
q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_gamQ[5])*x+_gamQ[6])*x + _gamQ[7] | |
return z * p / q | |
} | |
def handleSmallGammaX(x, y int): | |
if x == 0 { | |
return Inf(1) | |
} | |
return z / ((1 + Euler*x) * x) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reduce argument | |
z := 1.0 | |
for x >= 3 { | |
x = x - 1 | |
z = z * x | |
} | |
for x < 0 { | |
if x > -1e-09 { | |
goto small | |
} | |
z = z / x | |
x = x + 1 | |
} | |
for x < 2 { | |
if x < 1e-09 { | |
goto small | |
} | |
z = z / x | |
x = x + 1 | |
} | |
if x == 2 { | |
return z | |
} | |
x = x - 2 | |
p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gamP[5])*x + _gamP[6] | |
q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_gamQ[5])*x+_gamQ[6])*x + _gamQ[7] | |
return z * p / q | |
small: | |
if x == 0 { | |
return Inf(1) | |
} | |
return z / ((1 + Euler*x) * x) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
z := 1.0 | |
for x >= 3 { | |
x = x - 1 | |
z = z * x | |
} | |
x, z, smallFastPath := smallOkay(x, z) | |
if smallFastPath { | |
if x == 0 { | |
return Inf(1) | |
} | |
return z / ((1 + Euler*x) * x) | |
} | |
if x == 2 { | |
return z | |
} | |
x = x - 2 | |
p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gamP[5])*x + _gamP[6] | |
q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_gamQ[5])*x+_gamQ[6])*x + _gamQ[7] | |
return z * p / q | |
} | |
func smallOkay(x, z float64) (float64, float64, bool) { | |
for x < 0 { | |
if x > -1e-09 { | |
return x, z, true | |
} | |
z = z / x | |
x = x + 1 | |
} | |
for x < 2 { | |
if x < 1e-09 { | |
return x, z, true | |
} | |
z = z / x | |
x = x + 1 | |
} | |
return x, z, false | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
handleSmallGammaX := func(x,z int) int { | |
if x == 0 { | |
return Inf(1) | |
} | |
return z / ((1 + Euler*x) * x) | |
} | |
// Reduce argument | |
z := 1.0 | |
for x >= 3 { | |
x = x - 1 | |
z = z * x | |
} | |
for x < 0 { | |
if x > -1e-09 { | |
return handleSmallGammaX(x,z) | |
} | |
z = z / x | |
x = x + 1 | |
} | |
for x < 2 { | |
if x < 1e-09 { | |
return handleSmallGammaX(x,z) | |
} | |
z = z / x | |
x = x + 1 | |
} | |
if x == 2 { | |
return z | |
} | |
x = x - 2 | |
p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gamP[5])*x + _gamP[6] | |
q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_gamQ[5])*x+_gamQ[6])*x + _gamQ[7] | |
return z * p / q | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment