dengsgo / math-engine Goto Github PK
View Code? Open in Web Editor NEWMathematical expression parsing and calculation engine library. 数学表达式解析计算引擎库
License: Apache License 2.0
Mathematical expression parsing and calculation engine library. 数学表达式解析计算引擎库
License: Apache License 2.0
input /> 1--
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow
runtime stack:
runtime.throw(0x10d97b0, 0xe)
/usr/local/go/src/runtime/panic.go:617 +0x72
runtime.newstack()
/usr/local/go/src/runtime/stack.go:1041 +0x6f0
runtime.morestack()
/usr/local/go/src/runtime/asm_amd64.s:429 +0x8f
goroutine 1 [running]:
github.com/dengsgo/math-engine/engine.(*AST).parsePrimary(0xc043fffdb0, 0x0, 0x0)
/Users/admin/go/pkg/mod/github.com/dengsgo/[email protected]/engine/ast.go:164 +0x85c fp=0xc0240003e8 sp=0xc0240003e0 pc=0x109ef4c
github.com/dengsgo/math-engine/engine.(*AST).parsePrimary(0xc043fffdb0, 0x0, 0x0)
err := engine.RegFunction("if", 1, func(ast ...engine.ExprAST) float64 {
a := engine.ExprASTResult(ast[0])
}
比如这样获取时,a 变量可以是字符串吗?
比如可以判断 double(1,2)<1 double是自定义函数
应用场景主要是,策划可以配置一些条件,
比如:get战斗力(uid)>=10w get战斗力(uid)是自定义函数
作者博客也说了,词法用来做脚本解析正好的。
计算器的比较合适实现方式,是逆波兰式(后缀表达式)
不知道是否已经提供了这种功能,我的公式,里面会含有一些不可解析的字符,比如
100*100米/扎=10000
这是我希望的结果,这里的 米/扎 显然是无法解析的。
这只是个例子,可能还会有 100kg/桶 等等诸如此类的。
有没有这样的功能,就是可以把这种无法运算的内容自动清除掉?
1、自定义函数只能注册,不能注销
2、自定义函数可不可以加一个绑定自定义参数(不是把参数写在公式里),因为自定义函数有些可能需要传一些对象,按目前的设计,要么把对像做成全局变量,要么就是用unsafe pointer,
开平方根应该有两个答案吧,正数和负数
例如√2
I found that this project already supports Go modules. But sadly, the [tags](https://github.com https://github.com/dengsgo/math-engine/tags) doesn't follow Semantic Versioning, which means that all tags of this project will be ignored by Go modules and replaced by pseudo-versions, go get acts weirdly when tags are not in that form. It would be great to have the tagged release be named in the format vX.X.X format so that go mod can read it.
github.com/dengsgo/math-engine v0.0.0-20200627074419-8918d8f8ea02
Else the mod file shows something like github.com/dengsgo/math-engine v0.0.0-20200627074419-8918d8f8ea02
which is not very readable and difficult to upgrade. It’s hard to verify which version is in use. This is not conducive to version control.
So, I propose this project to follow Semantic Versioning in future versions. For example, v1.0.1
, v2.0.0
, v3.1.0-alpha
, v3.1.0-beta.2
etc, so that other project can use tag in go.mod.
可以把常用的函数注册到引擎中,然后就能像内置函数一样在输入的数学表达式中使用。
e.g
engine.RegFunction("double", 1, func(expr ...engine.ExprAST) float64 {
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
"(1+2)3" 输出结果3,感觉应该报错吧?
(1+2)*3,中间漏掉了符号。
V3.0 update: the top-level function ExprASTResult
will have a panic error when a runtime error occurs
.
Most of the time you should use ParseAndExec
, which automatically handles the errors that occur with ExprASTResult
. Use recover to catch the panic if you really need to call ExprASTResult
:
defer func() {
if e := recover(); e != nil {
// todo
}
}()
ExprASTResult(expr)
否则,一般情况下,本通过表达式可以解决的事情,结果又需要引入代码来解决,违背初衷
0.7*360=251.99999999999997
4/0,应该报 division by zero
现在,没报错,输出 +Inf
由用户自己实现接口类
这样有更好的灵活性和扩展性
package main
import (
"fmt"
"github.com/dengsgo/math-engine/engine"
)
func main() {
s := "0.7*"
// call top level function
r, err := engine.ParseAndExec(s)
if err != nil {
fmt.Println(err)
}
fmt.Println(r)
// 0.7
}
类似 0.7- 、0.7+
这种表达式也是无效吧。
计算这个会得到错误的结果:
(((3 / 3 - 3 - 3) / (3 * 3) / 3 / 3) - ((3 / 3) * (3 / 3) - ((3 * 3) * (3 * 3))) / ((3 / 3) * (3 / 3) - 3 * 3 / (3 / 3) * ((3 / 3) / 3 / 3 + 3 - 3 * (3 / 3))))
正确结果应该是:-1.8014398509481984e+17
math-engine结果: 7.272877265274083e+16
输入有异常符号
Tok: strings.ReplaceAll(p.Source[start:p.offset], "_", ""),
代码直接替换了 "_",应该报错,或者返回0
请问下有没有计划实现比较运算符, 如: ==, !=, > , <,>=, <=
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.