Skip to content

Commit

Permalink
py/modmath: Add option to work around -inf bug in a port's tgamma.
Browse files Browse the repository at this point in the history
This is needed for a workaround on esp32 port (in child commit),
which produces incorrect results otherwise.

This work was funded through GitHub Sponsors.

Signed-off-by: Angus Gratton <[email protected]>
  • Loading branch information
projectgus authored and dpgeorge committed Aug 7, 2024
1 parent afba3e0 commit b0c8937
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions py/modmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,17 @@ MATH_FUN_1(erf, erf)
// erfc(x): return the complementary error function of x
MATH_FUN_1(erfc, erfc)
// gamma(x): return the gamma function of x
#if MICROPY_PY_MATH_GAMMA_FIX_NEGINF
static mp_float_t MICROPY_FLOAT_C_FUN(tgamma_func)(mp_float_t x) {
if (isinf(x) && x < 0) {
math_error();
}
return MICROPY_FLOAT_C_FUN(tgamma)(x);
}
MATH_FUN_1(gamma, tgamma_func)
#else
MATH_FUN_1(gamma, tgamma)
#endif
// lgamma(x): return the natural logarithm of the gamma function of x
MATH_FUN_1(lgamma, lgamma)
#endif
Expand Down
5 changes: 5 additions & 0 deletions py/mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,11 @@ typedef double mp_float_t;
#define MICROPY_PY_MATH_POW_FIX_NAN (0)
#endif

// Whether to provide fix for gamma(-inf) to raise ValueError
#ifndef MICROPY_PY_MATH_GAMMA_FIX_NEGINF
#define MICROPY_PY_MATH_GAMMA_FIX_NEGINF (0)
#endif

// Whether to provide "cmath" module
#ifndef MICROPY_PY_CMATH
#define MICROPY_PY_CMATH (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
Expand Down

0 comments on commit b0c8937

Please sign in to comment.