Skip to content

Commit 6f49520

Browse files
committed
py: Implement second arg for math.log (optional value for base).
1 parent 05c6fbc commit 6f49520

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

py/modmath.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ MATH_FUN_2(pow, pow)
6868
MATH_FUN_1(exp, exp)
6969
/// \function expm1(x)
7070
MATH_FUN_1(expm1, expm1)
71-
/// \function log(x)
72-
MATH_FUN_1(log, log)
7371
/// \function log2(x)
7472
MATH_FUN_1(log2, log2)
7573
/// \function log10(x)
@@ -136,6 +134,19 @@ MATH_FUN_1(lgamma, lgamma)
136134
#endif
137135
//TODO: factorial, fsum
138136

137+
// Function that takes a variable number of arguments
138+
139+
// log(x[, base])
140+
STATIC mp_obj_t mp_math_log(mp_uint_t n_args, const mp_obj_t *args) {
141+
mp_float_t l = MICROPY_FLOAT_C_FUN(log)(mp_obj_get_float(args[0]));
142+
if (n_args == 1) {
143+
return mp_obj_new_float(l);
144+
} else {
145+
return mp_obj_new_float(l / MICROPY_FLOAT_C_FUN(log)(mp_obj_get_float(args[1])));
146+
}
147+
}
148+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_math_log_obj, 1, 2, mp_math_log);
149+
139150
// Functions that return a tuple
140151

141152
/// \function frexp(x)

tests/float/math_fun.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
('atan2', atan2, ((1., 0.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)),
6060
('fmod', fmod, ((1., 1.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)),
6161
('ldexp', ldexp, ((1., 0), (0., 1), (2., 2), (3., -2), (-3., -4),)),
62+
('log', log, ((2., 2.), (3., 2.), (4., 5.))),
6263
]
6364

6465
for function_name, function, test_vals in binary_functions:

0 commit comments

Comments
 (0)