소스 검색

fix https://github.com/pocketpy/pocketpy/issues/212

blueloveTH 2 년 전
부모
커밋
4af7d5c873
2개의 변경된 파일12개의 추가작업 그리고 2개의 파일을 삭제
  1. 8 1
      src/modules.cpp
  2. 4 1
      tests/10_cmath.py

+ 8 - 1
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])))));
 

+ 4 - 1
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)