{"id":16246009,"url":"https://github.com/miroiu/string-math","last_synced_at":"2025-03-16T12:32:56.899Z","repository":{"id":65413661,"uuid":"232105885","full_name":"miroiu/string-math","owner":"miroiu","description":"Evaluates a math expression from a string. Supports variables and custom operators.","archived":false,"fork":false,"pushed_at":"2024-04-24T07:05:10.000Z","size":229,"stargazers_count":54,"open_issues_count":2,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-16T02:26:22.028Z","etag":null,"topics":["boolean-algebra","boolean-expression","calculator","csharp","dotnet","expression-evaluator","expression-parser","math","math-expression","math-operation","mathematics","string","string-calculator","string-math"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miroiu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"miroiu","custom":["https://www.buymeacoffee.com/miroiu","https://paypal.me/miroiuemanuel"]}},"created_at":"2020-01-06T13:22:37.000Z","updated_at":"2025-03-13T20:44:15.000Z","dependencies_parsed_at":"2023-01-22T09:25:11.102Z","dependency_job_id":"ecfb9b8a-058a-49d0-8897-1d9aaa8125fb","html_url":"https://github.com/miroiu/string-math","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miroiu%2Fstring-math","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miroiu%2Fstring-math/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miroiu%2Fstring-math/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miroiu%2Fstring-math/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miroiu","download_url":"https://codeload.github.com/miroiu/string-math/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243869329,"owners_count":20360997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["boolean-algebra","boolean-expression","calculator","csharp","dotnet","expression-evaluator","expression-parser","math","math-expression","math-operation","mathematics","string","string-calculator","string-math"],"created_at":"2024-10-10T14:29:10.195Z","updated_at":"2025-03-16T12:32:56.611Z","avatar_url":"https://github.com/miroiu.png","language":"C#","readme":"\u003e V3 README can be found here: \u003chttps://github.com/miroiu/string-math/tree/release-3.0.0\u003e\n\n\u003e **NEW!** Boolean math example: https://github.com/miroiu/string-math/pull/6/files\n\n# String Math [![NuGet](https://img.shields.io/nuget/v/StringMath?style=flat-square\u0026logo=nuget)](https://www.nuget.org/packages/StringMath/) [![Downloads](https://img.shields.io/nuget/dt/StringMath?label=downloads\u0026style=flat-square\u0026logo=nuget)](https://www.nuget.org/packages/StringMath) ![.NET](https://img.shields.io/static/v1?label=%20\u0026message=Framework%204.6.1%20to%20NET%208\u0026color=5C2D91\u0026style=flat-square\u0026logo=.net) ![](https://img.shields.io/static/v1?label=%20\u0026message=documentation\u0026color=yellow\u0026style=flat-square)\n\nCalculates the value of a math expression from a string returning a double.\nSupports variables, user defined operators and expression compilation.\n\n```csharp\ndouble result = \"1 * (2 - 3) ^ 2\".Eval(); // 1\n```\n\n## Variables\n\n```csharp\ndouble result = \"{a} + 2 * {b}\".Substitute(\"a\", 2).Substitute(\"b\", 3).Result; // 8\n```\n\n### Global variables\n\nThese variables are inherited and cannot be substituted.\n\n```csharp\nMathExpr.AddVariable(\"PI\", 3.1415926535897931);\ndouble result = \"1 + {PI}\".Eval(); // 4.1415926535897931\n```\n\n## Custom operators\n\n### Global operators\n\nThese operators are inherited and can be overidden.\n\n```csharp\nMathExpr.AddOperator(\"abs\", a =\u003e a \u003e 0 ? a : -a);\ndouble result = \"abs -5\".Eval(); // 5\n\n// Operator precedence (you can specify an int for precedence)\nMathExpr.AddOperator(\"max\", (a, b) =\u003e a \u003e b ? a : b, Precedence.Power);\ndouble result = new MathExpr(\"2 * 3 max 4\").Result; // 8\n```\n\n### Local operators\n\nThese are applied only to the target expression.\n\n```csharp\nMathExpr expr = \"{PI} + 1\";\nexpr.SetOperator(\"+\", (a, b) =\u003e Math.Pow(a, b));\ndouble result = expr; // 3.1415926535897931\n\ndouble result2 = \"{PI} + 1\".Eval(); // 4.1415926535897931\n```\n\n## Advanced\n\n### Extract variables\n\n```csharp\nvar expr = \"{a} + {b} + {PI}\".ToMathExpr();\nvar variables = expr.Variables; // { \"a\", \"b\", \"PI\" }\nvar localVariables = expr.LocalVariables; // { \"a\", \"b\" }\n```\n\n### Compilation\n\n```csharp\nFunc\u003cdouble, double\u003e fn = \"{a} + 2\".ToMathExpr().Compile(\"a\");\ndouble result = fn(5); // 7\n```\n\n### Conditional substitution\n\n```csharp\nMathExpr expr = \"1 / {a}\".Substitute(\"a\", 1);\n\ndouble temp = expr.Result; // 1\n\nif (someCondition)  // true\n expr.Substitute(\"a\", 2);\n\ndouble final = expr.Result; // 0.5\n```\n\n### Sharing math context\n\n```csharp\nMathExpr expr = \"{PI} + 1\";\nexpr.SetOperator(\"+\", (a, b) =\u003e Math.Pow(a, b));\n\nMathExpr expr2 = \"3 + 2\".ToMathExpr(expr.Context);\n\ndouble result = \"1 + 2 + 3\".Eval(expr.Context);\n```\n\n### Custom math context\n\n```csharp\nvar context = new MathContext(); // new MathContext(MathContext.Default); // to inherit from global\ncontext.RegisterBinary(\"+\", (a, b) =\u003e Math.Pow(a, b));\n\nMathExpr expr = new MathExpr(\"{PI} + 1\", context);\nMathExpr expr2 = \"3 + 2\".ToMathExpr(context);\ndouble result = \"1 + 2 + 3\".Eval(context);\n```\n\n## Default operators\n\n### Binary\n\n```csharp\n+ (addition)\n- (subtraction)\n* (multiplication)\n/ (division)\n% (remainder)\n^ (power)\nlog (logarithm)\nmax (maximum)\nmin (minimum)\n```\n\n### Unary\n\n```csharp\n- (negation)\n! (factorial)\nsqrt (square root)\nsin (sine)\nasin (arcsine)\ncos (cosine)\nacos (arccosine)\ntan (tangent)\natan (arctangent)\nrad (convert degrees to radians)\ndeg (convert radians to degrees)\nceil (ceiling)\nfloor (floor)\nround (rounding)\nexp (e raised to power)\nabs (absolute)\n```\n","funding_links":["https://github.com/sponsors/miroiu","https://www.buymeacoffee.com/miroiu","https://paypal.me/miroiuemanuel"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiroiu%2Fstring-math","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiroiu%2Fstring-math","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiroiu%2Fstring-math/lists"}