math_engine.go 587 B

123456789101112131415161718192021222324
  1. package services
  2. import (
  3. "github.com/dengsgo/math-engine/engine"
  4. "math"
  5. )
  6. func init() {
  7. engine.RegFunction("log", 2, func(expr ...engine.ExprAST) float64 {
  8. base := math.Log(engine.ExprASTResult(expr[0]))
  9. number := math.Log(engine.ExprASTResult(expr[1]))
  10. return number / base
  11. })
  12. engine.RegFunction("ln", 1, func(expr ...engine.ExprAST) float64 {
  13. return math.Log(engine.ExprASTResult(expr[0]))
  14. })
  15. engine.RegFunction("pow", 2, func(expr ...engine.ExprAST) float64 {
  16. x := engine.ExprASTResult(expr[0])
  17. y := engine.ExprASTResult(expr[1])
  18. return math.Pow(x, y)
  19. })
  20. }