{"id":40336476,"url":"https://github.com/yidane/formula","last_synced_at":"2026-01-20T08:35:25.043Z","repository":{"id":43043686,"uuid":"183405832","full_name":"yidane/formula","owner":"yidane","description":"Provide analysis and calculation of mathematical expressions","archived":false,"fork":false,"pushed_at":"2022-03-22T06:37:02.000Z","size":3975,"stargazers_count":55,"open_issues_count":2,"forks_count":19,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-08-13T20:36:52.047Z","etag":null,"topics":["express","formula","golang","math"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yidane.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-25T09:50:17.000Z","updated_at":"2025-03-25T13:24:24.000Z","dependencies_parsed_at":"2022-08-26T11:10:59.360Z","dependency_job_id":null,"html_url":"https://github.com/yidane/formula","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/yidane/formula","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidane%2Fformula","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidane%2Fformula/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidane%2Fformula/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidane%2Fformula/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yidane","download_url":"https://codeload.github.com/yidane/formula/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidane%2Fformula/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28599099,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T02:08:49.799Z","status":"ssl_error","status_checked_at":"2026-01-20T02:08:44.148Z","response_time":117,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","formula","golang","math"],"created_at":"2026-01-20T08:35:24.432Z","updated_at":"2026-01-20T08:35:25.038Z","avatar_url":"https://github.com/yidane.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# formula\n\n支持对数学运算的编译执行，主要支持以下数学公式：\n\n* 支持四则运算\n* 支持三角函数\n* 支持位运算\n* 支持三元操作\n* 支持自定义参数\n* 支持自定义函数\n* 支持公式缓存\n\n内置函数如下表\n\n| 函数名称 | 参数个数 | 调用方式            | 结果                     |\n| -------- | -------- | ------------------- | ------------------------ |\n| abs      | 1        | abs(-1)             | 1                        |\n| acos     | 1        | acos(sqrt(3)/2)     | 0.5235987755982991 (π/6) |\n| asin     | 1        | asin(1/2)           | 0.5235987755982991 (π/6) |\n| asin     | 1        | asin(1)             | π/2                      |\n| atan     | 1        | atan(1)             | 0.7853981633974483 (π/4) |\n| ceil     | 1        | ceil(3.4)           | 4                        |\n| concat   | n        | concat(1,23,hello)  | 123hello                 |\n| cos      | 1        | cos(π/3)            | 0.5000000000000001       |\n| /        | 2        | 3/4                 | 0.75                     |\n| exp      | 1        | exp(3.3)            | 27.112638920657883       |\n| floor    | 1        | floor(2.2)          | 2                        |\n| \\\u003e       | 2        | 3 \u003e 2               | true                     |\n| iif      | 3        | iif(3 \u003e 2,π,10)     | 3.141592653589793        |\n| \\\u003c       | 2        | 3 \u003c 2               | false                    |\n| in       | n        | in(3,3,4,5)         | true                     |\n| ln       | 1        | ln(2.718281828)     | 0.9999999998311266       |\n| log2     | 1        | log2(16)            | 4                        |\n| log10    | 1        | log10(100000)       | 5                        |\n| log      | 2        | log(100,10)         | 2                        |\n| max      | n        | max(-1,2,3.1) = 3.1 | 3.1                      |\n| min      | n        | min(-1,2,3.1) = -1  | -1                       |\n| mod      | 2        | mod(5,2)            | 1                        |\n| *        | 2        | 3*3.4               | 10.2                     |\n| +        | 2        | 5+10                | 15                       |\n| pow      | 2        | pow(10,2)           | 100                      |\n| round    | 2        | round(100.11)       | 100                      |\n| sign     | 1        | sign(100)           | false                    |\n| sin      | 1        | sin(π/6)            | 0.49999999999999994      |\n| -        | 2        | 3-6                 | -3                       |\n| tan      | 1        | tan(π/4)            | 1                        |\n| truncate | 1        | truncate(12.3)      | 12                       |\n| \\\u003e\u003e       | 2        | 2\u003e\u003e1                | 1                        |\n| \\\u003c\u003c       | 2        | 1\u003c\u003c1                | 2                        |\n\n## 调用方法\n\n* 普通数学公式调用\n\n``` go\n\nexpression:=formula.NewExpression(\"1+2\")\nresult,err:=expression.Evaluate()\nif err!=nil{\n    //handle err\n}\n\nv,err:= result.Int64()\nif err!=nil{\n    //handle err\n}\n\n//v should equal 3\n\n```\n\n* 自定义参数调用\n```go\nexpression := NewExpression(\"[i]+[j]\")\nerr := expression.AddParameter(\"i\", 1)\nif err != nil {\n    t.Fatal(err)\n}\nerr = expression.AddParameter(\"j\", 2)\nif err != nil {\n    t.Fatal(err)\n}\nresult, err := expression.Evaluate()\n//handle result\n```\n\n自定义公式开发\n\n* 实现接口 opt.Function\n```go\ntype CustomFunction struct {\n}\n\nfunc (*CustomFunction) Name() string {\n\treturn \"CustomFunction\"\n}\n\nfunc (f *CustomFunction) Evaluate(context *opt.FormulaContext, args ...*opt.LogicalExpression) (*opt.Argument, error) {\n\terr := opt.MatchTwoArgument(f.Name(), args...)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tleft, err := (*args[0]).Evaluate(context)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tleftValue, err := left.Int64()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tright, err := (*args[1]).Evaluate(context)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\trightValue, err := right.Int64()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn opt.NewArgumentWithType(leftValue+rightValue+1, reflect.Int64), nil\n}\n```\n\n* 注册自定义函数\n```go\nfunc init() {\n\tvar f opt.Function = new(CustomFunction)\n\terr := formula.Register(\u0026f)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\n* 调用自定义函数\n\n```go\nexpression := formula.NewExpression(\"CustomFunction(1,2)\")\nresult, err := expression.Evaluate()\nif err != nil {\n    log.Fatal(err)\n}\n\nv, err := result.Int64()\nif err != nil {\n    log.Fatal(err)\n}\n\nif v != 4 { //CustomFunction: i+j+1\n    log.Fatal(\"error\")\n}\n\nlog.Println(\"custom function succeed\")\n```\n\n## 性能测试\n\n```bash\nBenchmarkOnePlusOne-8   \t   50000\t     26676 ns/op\nBenchmarkOne-8   \t           100000\t     19401 ns/op\nBenchmarkComplexOne-8   \t   10000\t    180650 ns/op\nBenchmarkSin-8   \t           20000\t     78591 ns/op\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidane%2Fformula","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyidane%2Fformula","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidane%2Fformula/lists"}