浏览代码

新增自定义函数

hongze 7 月之前
父节点
当前提交
a7a2759d52
共有 1 个文件被更改,包括 18 次插入0 次删除
  1. 18 0
      services/math_engine.go

+ 18 - 0
services/math_engine.go

@@ -0,0 +1,18 @@
+package services
+
+import (
+	"github.com/dengsgo/math-engine/engine"
+	"math"
+)
+
+func init() {
+	engine.RegFunction("log", 2, func(expr ...engine.ExprAST) float64 {
+		base := math.Log(engine.ExprASTResult(expr[0]))
+		number := math.Log(engine.ExprASTResult(expr[1]))
+		return number / base
+	})
+
+	engine.RegFunction("ln", 1, func(expr ...engine.ExprAST) float64 {
+		return math.Log(engine.ExprASTResult(expr[0]))
+	})
+}