{"id":19338212,"url":"https://github.com/jserv/mathex","last_synced_at":"2026-03-11T04:02:47.222Z","repository":{"id":146557276,"uuid":"87529282","full_name":"jserv/MathEX","owner":"jserv","description":"An embedded mathematical expression evaluator in C99","archived":false,"fork":false,"pushed_at":"2017-04-07T09:30:09.000Z","size":13,"stargazers_count":14,"open_issues_count":0,"forks_count":7,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-22T22:57:06.345Z","etag":null,"topics":[],"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/jserv.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,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-07T09:28:33.000Z","updated_at":"2024-11-18T22:19:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"91426a6e-7b87-464d-8ddb-1b1db1a75dde","html_url":"https://github.com/jserv/MathEX","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jserv%2FMathEX","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jserv%2FMathEX/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jserv%2FMathEX/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jserv%2FMathEX/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jserv","download_url":"https://codeload.github.com/jserv/MathEX/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250352270,"owners_count":21416461,"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":[],"created_at":"2024-11-10T03:16:42.346Z","updated_at":"2026-03-11T04:02:42.199Z","avatar_url":"https://github.com/jserv.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introduction\n\nMathEX is a mathematical expression evaluator written in C. It takes string as\ninput and returns floating-point number as a result.\n\n## Features\n\n* Supports arithmetic, bitwise and logical operators\n* Supports variables\n* Can be extended with custom functions\n* Simple evaluation takes ~50 nanoseconds on an average PC\n* Low memory usage makes it suitable for embedded systems\n* Pure C99 with no external dependencies\n* Good test coverage\n* Easy to understand\n\n## Example\n\n```c\n#include \"expression.h\"\n\n/* Custom function that returns the sum of its two arguments */\nstatic float add(struct expr_func *f, vec_expr_t args, void *c) {\n    float a = expr_eval(\u0026vec_nth(\u0026args, 0));\n    float b = expr_eval(\u0026vec_nth(\u0026args, 1));\n    return a + b;\n}\n\nstatic struct expr_func user_funcs[] = {\n    {\"add\", add, 0}, {NULL, NULL, 0},\n};\n\nint main() {\n    const char *s = \"x = 40, add(2, x)\";\n    struct expr_var_list vars = {0};\n    struct expr *e = expr_create(s, strlen(s), \u0026vars, user_funcs);\n    if (!e) { printf(\"Syntax error\"); return 1; }\n\n    printf(\"result: %f\\n\", expr_eval(e));\n\n    expr_destroy(e, \u0026vars);\n    return 0;\n}\n```\n\nOutput: `result: 42.000000`\n\n## API\n\n`struct expr *expr_create(const char *s, size_t len, struct expr_var_list\n*vars, struct expr_func *funcs)` - returns compiled expression from the given\nstring. If expression uses variables - they are bound to `vars`, so you can\nmodify values before evaluation or check the results after the evaluation.\n\n`float expr_eval(struct expr *e)` - evaluates compiled expression.\n\n`void expr_destroy(struct expr *e, struct expr_var_list *vars)` - cleans up\nmemory. Parameters can be NULL (e.g. if you want to clean up expression, but\nreuse variables for another expression).\n\n`struct expr_var *expr_var(struct expr_var *vars, const char *s, size_t len)` -\nreturns/creates variable of the given name in the given list. This can be used\nto get variable references to get/set them manually.\n\n## Supported operators\n\n* Arithmetics: `+`, `-`, `*`, `/`, `%` (remainder), `**` (power)\n* Bitwise: `\u003c\u003c`, `\u003e\u003e`, `\u0026`, `|`, `^` (xor or unary bitwise negation)\n* Logical: `\u003c`, `\u003e`, `==`, `!=`, `\u003c=`, `\u003e=`, `\u0026\u0026`, `||`, `!` (unary not)\n* Other: `=` (assignment, e.g. `x=y=5`), `,` (separates expressions or function parameters)\n\nOnly the following functions from libc are used to reduce the footprint and\nmake it easier to use:\n\n* calloc, realloc and free - memory management\n* isnan, isinf, fmodf, powf - math operations\n* strlen, strncmp, strncpy, strtof - tokenizing and parsing\n\n## Running tests\n\nTo run all the tests and benchmarks, do `make check`.\n\n## License\n\nCode is distributed under MIT X License that can be found in the `LICENSE` file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjserv%2Fmathex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjserv%2Fmathex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjserv%2Fmathex/lists"}