diff --git a/py/modmath.c b/py/modmath.c index 46423d2b5c..296d16be6f 100644 --- a/py/modmath.c +++ b/py/modmath.c @@ -68,8 +68,6 @@ MATH_FUN_2(pow, pow) MATH_FUN_1(exp, exp) /// \function expm1(x) MATH_FUN_1(expm1, expm1) -/// \function log(x) -MATH_FUN_1(log, log) /// \function log2(x) MATH_FUN_1(log2, log2) /// \function log10(x) @@ -136,6 +134,19 @@ MATH_FUN_1(lgamma, lgamma) #endif //TODO: factorial, fsum +// Function that takes a variable number of arguments + +// log(x[, base]) +STATIC mp_obj_t mp_math_log(mp_uint_t n_args, const mp_obj_t *args) { + mp_float_t l = MICROPY_FLOAT_C_FUN(log)(mp_obj_get_float(args[0])); + if (n_args == 1) { + return mp_obj_new_float(l); + } else { + return mp_obj_new_float(l / MICROPY_FLOAT_C_FUN(log)(mp_obj_get_float(args[1]))); + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_math_log_obj, 1, 2, mp_math_log); + // Functions that return a tuple /// \function frexp(x) diff --git a/tests/float/math_fun.py b/tests/float/math_fun.py index 16aec76f92..94411a36bd 100644 --- a/tests/float/math_fun.py +++ b/tests/float/math_fun.py @@ -59,6 +59,7 @@ binary_functions = [('copysign', copysign, [(23., 42.), (-23., 42.), (23., -42.) ('atan2', atan2, ((1., 0.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)), ('fmod', fmod, ((1., 1.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)), ('ldexp', ldexp, ((1., 0), (0., 1), (2., 2), (3., -2), (-3., -4),)), + ('log', log, ((2., 2.), (3., 2.), (4., 5.))), ] for function_name, function, test_vals in binary_functions: