From 4af7d5c873fb2a4d934eb05d9ed74a4c7065417e Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 23 Feb 2024 15:48:24 +0800 Subject: [PATCH] fix https://github.com/pocketpy/pocketpy/issues/212 --- src/modules.cpp | 9 ++++++++- tests/10_cmath.py | 5 ++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/modules.cpp b/src/modules.cpp index acb08f17..ef157a7d 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -170,7 +170,14 @@ void add_module_math(VM* vm){ }); vm->bind_func<1>(mod, "exp", PK_LAMBDA(VAR(std::exp(CAST_F(args[0]))))); - vm->bind_func<1>(mod, "log", PK_LAMBDA(VAR(std::log(CAST_F(args[0]))))); + // vm->bind_func<1>(mod, "log", PK_LAMBDA(VAR(std::log(CAST_F(args[0]))))); + + vm->bind(mod, "log(x, base=2.718281828459045)", [](VM* vm, ArgsView args){ + f64 x = CAST_F(args[0]); + f64 base = CAST_F(args[1]); + return VAR(std::log(x) / std::log(base)); + }); + vm->bind_func<1>(mod, "log2", PK_LAMBDA(VAR(std::log2(CAST_F(args[0]))))); vm->bind_func<1>(mod, "log10", PK_LAMBDA(VAR(std::log10(CAST_F(args[0]))))); diff --git a/tests/10_cmath.py b/tests/10_cmath.py index 513002b8..fd42034d 100644 --- a/tests/10_cmath.py +++ b/tests/10_cmath.py @@ -1,4 +1,4 @@ -from cmath import isclose, sqrt, nan, inf, nanj, infj +from cmath import isclose, sqrt, nan, inf, nanj, infj, log import math assert 1+2j == complex(1, 2) == 2j+1 @@ -36,3 +36,6 @@ assert repr(nanj) == '(0.0+nanj)', nanj assert repr(-nanj) == '(0.0+nanj)', -nanj assert repr(infj) == '(0.0+infj)', infj assert repr(-infj) == '(0.0-infj)', -infj + +assert math.log(1) == 0.0 +assert isclose(log(10+5j), 2.4141568686511508+0.4636476090008061j)