# Buggy code
def _moment_from_stats(n, mu, mu2, g1, g2, moment_func, args):
    if n == 0:
        return 1.0
    elif n == 1:
        if mu is None:
            val = moment_func(1, *args)
        else:
            val = mu
    elif n == 2:
        if mu2 is None or mu is None:
            val = moment_func(2, *args)
        else:
            val = mu2 + mu * mu
    elif n == 3:
        if g1 is None or mu2 is None or mu is None:
            val = moment_func(3, *args)
        else:
            mu3 = g1 * (mu2**1.5)  # 3rd central moment
            val = mu3 + 3 * mu * mu2 + mu**3  # 3rd non-central moment
    elif n == 4:
        if g1 is None or g2 is None or mu2 is None or mu is None:
            val = moment_func(4, *args)
        else:
            mu4 = (g2 + 3.0) * (mu2**2.0)  # 4th central moment
            mu3 = g1 * (mu2**1.5)  # 3rd central moment
            val = mu4 + 4 * mu * mu3 + 6 * mu * mu * mu2 + mu**4
    else:
        val = moment_func(n, *args)

    return val


# Traceback (OverflowError)
======================================================================
ERROR: test_continuous_basic.test_cont_basic(<scipy.stats.distributions.alpha_gen object at 0x3726dd0>, 0.5, (3.5704770516650459,), (0, 1), [<bound method alpha_gen.pdf of <scipy.stats.distributions.alpha_gen object at 0x3726dd0>>, <bound method alpha_gen.logpdf of <scipy.stats.distributions.alpha_gen object at 0x3726dd0>>, <bound method alpha_gen.cdf of <scipy.stats.distributions.alpha_gen object at 0x3726dd0>>, <bound method alpha_gen.logcdf of <scipy.stats.distributions.alpha_gen object at 0x3726dd0>>, <bound method alpha_gen.logsf of <scipy.stats.distributions.alpha_gen object at 0x3726dd0>>])
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/virtualenv/python2.6/lib/python2.6/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/home/travis/build/scipy/scipy/build/testenv/lib/python2.6/site-packages/scipy/stats/tests/test_continuous_basic.py", line 454, in check_named_args
npt.assert_equal(distfn.moment(3, *a, **k),
File "/home/travis/build/scipy/scipy/build/testenv/lib/python2.6/site-packages/scipy/stats/distributions.py", line 1702, in moment
val = _moment_from_stats(n, mu, mu2, g1, g2, self._munp, args)
File "/home/travis/build/scipy/scipy/build/testenv/lib/python2.6/site-packages/scipy/stats/distributions.py", line 387, in _moment_from_stats
mu3 = g1*(mu2**1.5) # 3rd central moment
OverflowError: (34, 'Numerical result out of range')
======================================================================

ERROR: test_continuous_basic.test_cont_basic(<scipy.stats.distributions.foldcauchy_gen object at 0x373e890>, 0.5, (4.7164673455831894,), (0, 1), [<bound method foldcauchy_gen.pdf of <scipy.stats.distributions.foldcauchy_gen object at 0x373e890>>, <bound method foldcauchy_gen.logpdf of <scipy.stats.distributions.foldcauchy_gen object at 0x373e890>>, <bound method foldcauchy_gen.cdf of <scipy.stats.distributions.foldcauchy_gen object at 0x373e890>>, <bound method foldcauchy_gen.logcdf of <scipy.stats.distributions.foldcauchy_gen object at 0x373e890>>, <bound method foldcauchy_gen.logsf of <scipy.stats.distributions.foldcauchy_gen object at 0x373e890>>])
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/virtualenv/python2.6/lib/python2.6/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/home/travis/build/scipy/scipy/build/testenv/lib/python2.6/site-packages/scipy/stats/tests/test_continuous_basic.py", line 454, in check_named_args
npt.assert_equal(distfn.moment(3, *a, **k),
File "/home/travis/build/scipy/scipy/build/testenv/lib/python2.6/site-packages/scipy/stats/distributions.py", line 1702, in moment
val = _moment_from_stats(n, mu, mu2, g1, g2, self._munp, args)
File "/home/travis/build/scipy/scipy/build/testenv/lib/python2.6/site-packages/scipy/stats/distributions.py", line 387, in _moment_from_stats
mu3 = g1*(mu2**1.5) # 3rd central moment
OverflowError: (34, 'Numerical result out of range')

# Fixed code
def _moment_from_stats(n, mu, mu2, g1, g2, moment_func, args):
    if n == 0:
        return 1.0
    elif n == 1:
        if mu is None:
            val = moment_func(1, *args)
        else:
            val = mu
    elif n == 2:
        if mu2 is None or mu is None:
            val = moment_func(2, *args)
        else:
            val = mu2 + mu * mu
    elif n == 3:
        if g1 is None or mu2 is None or mu is None:
            val = moment_func(3, *args)
        else:
            mu3 = g1 * np.power(mu2, 1.5)  # 3rd central moment
            val = mu3 + 3 * mu * mu2 + mu * mu * mu  # 3rd non-central moment
    elif n == 4:
        if g1 is None or g2 is None or mu2 is None or mu is None:
            val = moment_func(4, *args)
        else:
            mu4 = (g2 + 3.0) * (mu2**2.0)  # 4th central moment
            mu3 = g1 * np.power(mu2, 1.5)  # 3rd central moment
            val = mu4 + 4 * mu * mu3 + 6 * mu * mu * mu2 + mu * mu * mu * mu
    else:
        val = moment_func(n, *args)

    return val