All Projects → dengsgo → Math Engine

dengsgo / Math Engine

Licence: apache-2.0
Mathematical expression parsing and calculation engine library. 数学表达式解析计算引擎库

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Math Engine

Sharpmath
A small .NET math library.
Stars: ✭ 36 (-70.73%)
Mutual labels:  library, math, expression
Expr
Expression language for Go
Stars: ✭ 2,123 (+1626.02%)
Mutual labels:  expression, engine, ast
Stdlib
✨ Standard library for JavaScript and Node.js. ✨
Stars: ✭ 2,749 (+2134.96%)
Mutual labels:  library, lib, math
M3u8
Parser and generator of M3U8-playlists for Apple HLS. Library for Go language. 🎦
Stars: ✭ 800 (+550.41%)
Mutual labels:  library, lib
Lockstepengine
A lockstep solution include lots of deterministic library (Math,Collision,Navmesh,BehaviorTree,Serialization ...)
Stars: ✭ 376 (+205.69%)
Mutual labels:  engine, math
Lambdahack
Haskell game engine library for roguelike dungeon crawlers; please offer feedback, e.g., after trying out the sample game with the web frontend at
Stars: ✭ 439 (+256.91%)
Mutual labels:  engine, library
IwEngine
This is an engine that I initially started building after taking a game coding class in high school. I didn't like Unity so tried to make something more code focused that was personally easier to use.
Stars: ✭ 97 (-21.14%)
Mutual labels:  math, engine
Metric Parser
📜 AST-based advanced mathematical parser written by Typescript.
Stars: ✭ 26 (-78.86%)
Mutual labels:  ast, expression
Coord Rs
[deprecated] A simple, ergonomic vector mathematics crate for Rust
Stars: ✭ 18 (-85.37%)
Mutual labels:  library, math
Easygo
基于Kotlin、OkHttp的声明式网络框架,像写HTML界面一样写网络调用代码
Stars: ✭ 40 (-67.48%)
Mutual labels:  library, lib
Formula Parser
Parsing and evaluating mathematical formulas given as strings.
Stars: ✭ 62 (-49.59%)
Mutual labels:  math, expression
Bounce
Bounce is a 3D physics engine for games.
Stars: ✭ 300 (+143.9%)
Mutual labels:  library, math
Libmqtt
MQTT v3.1.1/5.0 library in Go
Stars: ✭ 290 (+135.77%)
Mutual labels:  library, lib
Android Hot Libraries
收集总结 Android 项目中值得推荐的优秀开源项目
Stars: ✭ 755 (+513.82%)
Mutual labels:  library, lib
Cute headers
Collection of cross-platform one-file C/C++ libraries with no dependencies, primarily used for games
Stars: ✭ 3,274 (+2561.79%)
Mutual labels:  library, math
Vulkan2drenderer
Easy to use 2D rendering engine using Vulkan API as backend.
Stars: ✭ 60 (-51.22%)
Mutual labels:  engine, library
Abdk Libraries Solidity
Open-Source Libraries for Solidity by ABDK Consulting
Stars: ✭ 84 (-31.71%)
Mutual labels:  library, math
Yaep
Yet Another Earley Parser
Stars: ✭ 110 (-10.57%)
Mutual labels:  ast, library
eval-estree-expression
Safely evaluate JavaScript (estree) expressions, sync and async.
Stars: ✭ 22 (-82.11%)
Mutual labels:  ast, expression
desktop
Extendable calculator for the 21st Century ⚡
Stars: ✭ 85 (-30.89%)
Mutual labels:  math, engine

Math-Engine

Build Status Go Report Card godoc.org

使用 Go 实现的数学表达式解析计算引擎库,它小巧,无任何依赖,具有扩展性(比如可以注册自己的函数到引擎中),比较完整的完成了数学表达式解析执行,包括词法分析、语法分析、构建AST、运行。

go get -u github.com/dengsgo/math-engine

能够处理的表达式样例:

  • 1+127-21+(3-4)*6/2.5
  • (88+(1+8)*6)/2+99
  • 123_345_456 * 1.5 - 2 ^ 4
  • -4 * 6 + 2e2 - 1.6e-3
  • sin(pi/2)+cos(45-45*1)+tan(pi/4)
  • 99+abs(-1)-ceil(88.8)+floor(88.8)
  • max(min(2^3, 3^2), 10*1.5-7)
  • double(6) + 3 , double是一个自定义的函数

Demo

asciicast

Method Support

symbol explanation e.g.
+ 加,plus 1+2 = 3
- 减,sub 8-3.5 = 4.5
* 乘,multiply 2*3 = 6
/ 除,division 5/2 = 2.5
% 取余,remainder 5%2 = 1
^ 整数次方,integer power 2^3 = 8, 3^2 = 9
e 科学计数法,E-notation 1.2e3 = 1.2e+3 = 1200,1.2e-2 = 0.012
() 括号,brackets (2+3)*4 = 20
_ 数字分隔符,number separator 123_456_789 = 123456789
pi π pi = 3.141592653589793
sin(x) 正弦函数,sine sin(pi/2) = 1
cos(x) 余弦函数,cosine cos(0) = 1
tan(x) 正切函数,tangent tan(pi/4) = 1
cot(x) 余切函数,cotangent cot(pi/4) = 1
sec(x) 正割函数,secant sec(0) = 1
csc(x) 余割函数,cosecant csc(pi/2) = 1
abs(x) 绝对值,absolute value abs(-6) = 6
ceil(x) 向上取整 ceil(4.2) = 5
floor(x) 向下取整 floor(4.8) = 4
round(x) 四舍五入取整 round(4.4) = 4, round(4.5) = 5
sqrt(x) 平方根,square root sqrt(4) = 2
cbrt(x) 立方根,cube root cbrt(27) = 3
max(x, y) x, y 中的较大值 max(2, 3) = 3
min(x, y) x, y 中的较小值 min(2, 3) = 2
noerr(x) 计算 x 出错时返回 0 noerr(1 / 1) = 1, noerr( 1/ 0 ) = 0
double(x) 返回 x 的双倍值,这是一个自定义的函数示例,你可以注册任意的自定义函数到引擎中 double(6) = 12

Usage

你可以直接引用该库嵌入到自己的程序中:

go get -u github.com/dengsgo/math-engine

在代码中引入:

import "github.com/dengsgo/math-engine/engine"

e.g. 1 常规用法: 直接调用解析执行函数 :

import "github.com/dengsgo/math-engine/engine"

func main() {
  s := "1 + 2 * 6 / 4 + (456 - 8 * 9.2) - (2 + 4 ^ 5)"
  // call top level function
  r, err := engine.ParseAndExec(s)
  if err != nil {
    fmt.Println(err)
  }
  fmt.Printf("%s = %v", s, r)
}

e.g. 2 高级用法: 依次调用函数,手动执行 :

import "github.com/dengsgo/math-engine/engine"

func main() {
	s := "1 + 2 * 6 / 4 + (456 - 8 * 9.2) - (2 + 4 ^ 5)"
	exec(s)
}

// call engine
// one by one
func exec(exp string) {
	// input text -> []token
	toks, err := engine.Parse(exp)
	if err != nil {
		fmt.Println("ERROR: " + err.Error())
		return
	}
	// []token -> AST Tree
	ast := engine.NewAST(toks, exp)
	if ast.Err != nil {
		fmt.Println("ERROR: " + ast.Err.Error())
		return
	}
	// AST builder
	ar := ast.ParseExpression()
	if ast.Err != nil {
		fmt.Println("ERROR: " + ast.Err.Error())
		return
	}
	fmt.Printf("ExprAST: %+v\n", ar)
	// AST traversal -> result
	r := engine.ExprASTResult(ar)
	fmt.Println("progressing ...\t", r)
	fmt.Printf("%s = %v\n", exp, r)
}

编译运行,应该可以看到如下输出:

ExprAST: {Op:- Lhs:{Op:+ Lhs:{Op:+ Lhs:{Val:1} Rhs:{Op:/ Lhs:{Op:* Lhs:{Val:2} Rhs:{Val:6}} Rhs:{Val:4}}} Rhs:{Op:- Lhs:{Val:456} Rhs:{Op:* Lhs:{Val:8} Rhs:{Val:9.2}}}} Rhs:{Op:+ Lhs:{Val:2} Rhs:{Op:^ Lhs:{Val:4} Rhs:{Val:5}}}}
progressing ...  -639.6
1+2*6/4+(456-8*9.2)-(2+4^5) = -639.6

TrigonometricMode

三角函数的参数类型默认为弧度RadianMode,e.g. sin(pi/2) = 1.

你可以通过设置 TrigonometricMode 调整参数类型,可选 弧度RadianMode、角度AngleMode,e.g. :

import "github.com/dengsgo/math-engine/engine"

func main() {
  s := "1 + sin(90)"
  engine.TrigonometricMode = engine.AngleMode
  engine.ParseAndExec(s) // will return 2, nil
  s = "1 + sin(pi/2)"
  engine.TrigonometricMode = engine.RadianMode
  engine.ParseAndExec(s) // will return 2, nil
}

Register Function

math-engine 提供了自定义函数注册到引擎的能力。你可以把常用的函数注册到引擎中,然后就能像内置函数一样在输入的数学表达式中使用。

e.g

  // RegFunction is Top level function
  // the same function name only needs to be registered once.
  // double is register function name.
  // 1 is a number of parameter signatures.
  // func(expr ...engine.ExprAST) float64 is your function.
  engine.RegFunction("double", 1, func(expr ...engine.ExprAST) float64 {
    // you can use the index value directly according to the number of parameters
    // without worrying about crossing the boundary.
    // use ExprASTResult to get the result of the ExprAST structure.
    return engine.ExprASTResult(expr[0]) * 2
  })

然后你就可以在输入的表达式中使用这个函数 double:

//exp := "double(6) + 2"
r, err := engine.ParseAndExec("double(6) + 2")
if err != nil {
  panic(err)
}
fmt.Printf("double(6) + 2 = %f\n", r) // will print : double(6) + 2 = 14.000000

注意事项:

  • 注册的函数名只能是英文字母和数字,且必须英文字母开头(区分大小写);
  • 每一个函数名只能且只需注册一次;
  • 注册的函数逻辑中如果有 panic ,需要程序自己捕获处理;

Compile

go version 1.12

# Compile Demo
go test
go build
./math-engine

也可以直接下载已编译好的二进制文件,直接运行:

Github Releases

实现细节

请阅读我的博客文章:用 Go 实现一个完整的数学表达式计算引擎

TODO

已实现

  • [x] 加 +
  • [x] 减 -
  • [x] 乘 *
  • [x] 除 /
  • [x] 取余 %
  • [x] 整数次方 ^
  • [x] 科学计数法 e.g. 1.2e71.2e-7
  • [x] 括号 ()
  • [x] 混合运算 e.g. 1+2*6/4+(456-8*9.2)-(2+4^5)*2e3+1.2e-2
  • [x] 友好的长数字 e.g. 123_456_789
  • [x] 三角函数 e.g. sin, cos, tan, cot, sec, csc
  • [x] 常量 pi
  • [x] 辅助函数 e.g. abs, ceil, floor, sqrt, cbrt, max, min, noerr
  • [x] 提供自定义函数注册功能,注册后可以在表达式中使用
  • [x] 精确的数据计算
  • [x] 友好的错误消息 e.g.
input /> 123+89-0.0.9
ERROR: strconv.ParseFloat: parsing "0.0.9": invalid syntax
want '(' or '0-9' but get '0.0.9'
------------
123+89-0.0.9
       ^
------------
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].