formula

package module
v0.0.0-...-d9afe6e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2019 License: GPL-3.0 Imports: 10 Imported by: 0

README

formula

支持对数学运算的编译执行,主要支持以下数学公式:

  • 支持四则运算
  • 支持三角函数
  • 支持位运算
  • 支持三元操作
  • 支持逻辑运算符
  • 支持自定义参数
  • 支持自定义函数
  • 支持公式缓存

支持lua

  • 支持lua缓存

内置函数如下表

函数名称 参数个数 调用方式 结果
abs 1 abs(-1) 1
acos 1 acos(sqrt(3)/2) 0.5235987755982991 (π/6)
asin 1 asin(1/2) 0.5235987755982991 (π/6)
asin 1 asin(1) π/2
atan 1 atan(1) 0.7853981633974483 (π/4)
ceil 1 ceil(3.4) 4
concat n concat(1,23,hello) 123hello
cos 1 cos(π/3) 0.5000000000000001
/ 2 3/4 0.75
exp 1 exp(3.3) 27.112638920657883
floor 1 floor(2.2) 2
> 2 3 > 2 true
if 3 iif(3 > 2,π,10) 3.141592653589793
< 2 3 < 2 false
in n in(3,3,4,5) true
ln 1 ln(2.718281828) 0.9999999998311266
log2 1 log2(16) 4
log10 1 log10(100000) 5
log 2 log(100,10) 2
max n max(-1,2,3.1) = 3.1 3.1
min n min(-1,2,3.1) = -1 -1
mod 2 mod(5,2) 1
* 2 3*3.4 10.2
+ 2 5+10 15
pow 2 pow(10,2) 100
round 2 round(100.11) 100
sign 1 sign(100) false
sin 1 sin(π/6) 0.49999999999999994
- 2 3-6 -3
tan 1 tan(π/4) 1
truncate 1 truncate(12.3) 12
>> 2 2>>1 1
<< 2 1<<1 2

调用方法

  • 普通数学公式调用
params := make(map[string]interface{})
r,err:=HandleFormula("1+2",params)
if err!=nil{
    //handle err
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LuaFunctionRegexp = `lua[('\s]+function[\w\s]+\(([a-zA-Z0-9,\s]+)\).*?end\'\)`
View Source
var WildcardRegexp = `(@[\w\d\.]+(\.\*)+[\.\w\d\*]*)[,\s)]`
View Source
var WildcardSymbols = []string{"sum(", "count", "avg"}

Functions

func Register

func Register(f *opt.Function) error

Register custom function which implement opt.Function

Types

type CustomParamsListener

type CustomParamsListener struct {
	parser.BaseFormulaListener
	// contains filtered or unexported fields
}

func (*CustomParamsListener) ExitId

func (s *CustomParamsListener) ExitId(c *parser.IdContext)

ExitId is called when exiting the id production.

func (*CustomParamsListener) GetParamNames

func (s *CustomParamsListener) GetParamNames() []string

type Expression

type Expression struct {
	// contains filtered or unexported fields
}

Expression for build user's input

func NewExpression

func NewExpression(expression string, options ...opt.Option) *Expression

NewExpression create new Expression

func (*Expression) AddParameter

func (expression *Expression) AddParameter(name string, value interface{}) error

AddParameter add user's parameter which is required in the expression

func (*Expression) Evaluate

func (expression *Expression) Evaluate() (*opt.Argument, error)

Evaluate return result of expression

func (*Expression) GetParameterNames

func (expression *Expression) GetParameterNames() []string

func (*Expression) GetResult

func (expression *Expression) GetResult() (*opt.Argument, error)

func (*Expression) OriginalString

func (expression *Expression) OriginalString() string

OriginalString return user's input text

func (*Expression) Precompile

func (expression *Expression) Precompile() error

func (*Expression) ResetParameters

func (expression *Expression) ResetParameters()

ResetParameters clear all parameter

Directories

Path Synopsis
examples
internal
exp
fs
lua

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL