{"id":13509330,"url":"https://github.com/Rob-bie/Expr","last_synced_at":"2025-03-30T13:31:59.135Z","repository":{"id":29243111,"uuid":"32775275","full_name":"Rob-bie/expr","owner":"Rob-bie","description":"An Elixir library for parsing and evaluating mathematical expressions","archived":false,"fork":false,"pushed_at":"2018-04-04T15:39:40.000Z","size":12,"stargazers_count":12,"open_issues_count":3,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-23T07:34:58.830Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Elixir","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/Rob-bie.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":"2015-03-24T04:03:18.000Z","updated_at":"2023-05-05T04:32:47.000Z","dependencies_parsed_at":"2022-09-20T01:43:58.401Z","dependency_job_id":null,"html_url":"https://github.com/Rob-bie/expr","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/Rob-bie%2Fexpr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rob-bie%2Fexpr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rob-bie%2Fexpr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Rob-bie%2Fexpr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Rob-bie","download_url":"https://codeload.github.com/Rob-bie/expr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246323958,"owners_count":20759057,"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-08-01T02:01:06.281Z","updated_at":"2025-03-30T13:31:58.875Z","avatar_url":"https://github.com/Rob-bie.png","language":"Elixir","funding_links":[],"categories":["Text and Numbers"],"sub_categories":[],"readme":"# expr\n\nexpr is an Elixir library for parsing and evaluating mathematical expressions.\n\n- [expr](#expr)\n  - [Installation](#installation)\n  - [Examples](#examples)\n    - [Operators and constants](#operators-and-constants)\n    - [Basic examples](#basic-examples)\n    - [Variable usage](#variable-usage)\n    - [Caveats](#caveats)\n  - [TODO](#todo)\n  - [License](#license)\n\n\n\n## Installation\n\nAdd Expr to your ```mix.exs``` dependencies:\n\n```elixir \ndef deps do\n  [{:expr, \"~\u003e 0.1.0\"}]\nend\n```\n\nAfterwards, run the command ```mix deps.get``` and you're good to go.\n\n## Examples\n\n##### Operators and constants\n\n| Operator  |Precedence | Associativity |\n| :-------------: | :-------------: | :------------: |\n| log10  | 4  | RIGHT |\n| floor  | 4  | RIGHT |\n| ceil  | 4  | RIGHT |\n| asin  | 4  | RIGHT |\n| acos  | 4  | RIGHT |\n| atan  | 4  | RIGHT |\n| sqrt  | 4  | RIGHT |\n| log  | 4  | RIGHT |\n| tan  | 4  | RIGHT |\n| cos  | 4  | RIGHT |\n| sin  | 4  | RIGHT |\n| abs  | 4  | RIGHT |\n| !  | 4  | RIGHT |\n| #  | 4  | RIGHT |\n| ^  | 4  | RIGHT |\n| /  | 3  | LEFT |\n| *  | 3  | LEFT |\n| +  | 2  | LEFT |\n| - | 2  | LEFT |\n  \u003cbr\u003e\n  \n| Symbol  | Value |\n| :-------------: | :-------------: |\n| pi  | 3.14 ...  |\n| e | 2.71 ...|\n\n##### Basic examples\n\nThe result of all evaluations are returned at the front of a list. As of version ```0.1.0``` there is no error checking or expression validation. If something goes wrong, an arithmetic error will be thrown. While the parser will understand your intent for most expressions, the more explicit you are, the better. (Be liberal with parenthesis if you're getting unexpected results!)\n\n```elixir\nExpr.eval!(\"1 + 2 * 3\")\n=\u003e [7.0]\n\nExpr.eval!(\"5/2\")\n=\u003e [2.5]\n\nExpr.eval!(\"5! + abs(-5)\")\n=\u003e [125.0]\n\nExpr.eval!(\"--4\") #Negation\n=\u003e [4.0]\n\nExpr.eval!(\"5(sqrt(abs(-16)))\") #Implicit multiplication\n=\u003e [20.0]\n\nExpr.eval!(\"(5^2)(floor(2.75))\") #Implicit multiplication #2\n=\u003e [50.0]\n\nExpr.eval!(\"sin(60)\") #RADIANS!\n=\u003e [-0.3048106211022167]\n```\n\n##### Variable usage\n\nValid variable names cannot contain operator names, constant names or start with numbers. Starting with capital letters, containing numbers and unused symbols are fine. However, I recommend using short, lowercase variable names. A map of variable names and their values are passed to ```Expr.eval!``` along with an expression when evaluating expressions that contain variables.\n\n```elixir\nExpr.eval!(\"x + y / x\", %{\"x\" =\u003e 2, \"y\" =\u003e 4})\n=\u003e [3.0]\n\nExpr.eval!(\"-x@ + 1\", %{\"x@\" =\u003e 2})\n=\u003e [-1.0]\n\nExpr.eval!(\"-(sqrt(abs(some_var)))\", %{\"some_var\" =\u003e -2.5})\n=\u003e [-1.5811388300841898]\n\nExpr.eval!(\"ABC+2^CBA\", %{\"ABC\" =\u003e 2, \"CBA\" =\u003e 3})\n=\u003e [10.0]\n\nvars = %{\"a\" =\u003e 2.5, \"b\" =\u003e 3, \"c\" =\u003e 0.25, \"d\" =\u003e 10, \"z\" =\u003e 6.5}\nExpr.eval!(\"a^(b+c)-d(z)\", vars)\n=\u003e [-45.35260266120413]\n```\n\n##### Caveats\n\n- Technically you can have a variable name that is the same as a constant name, however the constant's value will override the value of the variable.\n\n ```Expr.eval!(\"pi + 1\", %{\"pi\" =\u003e 1}) =\u003e 4.141592...```\n \n\n- Sometimes a malformed expression will actually evaluate and return a list of length ```\u003e1```, whenever this happens know something has gone wrong.\n\n\n- Complex numbers are not supported, it's not uncommon that you'll have a properly formed expression and somewhere during the calculation an arithmetic error will be thrown. For example, trying to take the square root of a negative number.\n\n\n- Negation is represented by ```#``` under the hood, ```#``` can be used in expressions. \n\n\n- Implicit multiplication is currently only *partially* supported. While ```pi(r^2)``` is entirely valid, ```pir^2``` is unfortunately not. This may be coming in a future version.\n\n\n## TODO\n\n- Function support for functions with arity ```\u003e2```\n- More operator/constant support\n- Expression validator\n- Improved implicit multiplication\n- Clean up messy parser code\n- Comments.\n\n## License\n\n```\nThis work is free. You can redistribute it and/or modify it under the\nterms of the MIT License. See the LICENSE file for more details.\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRob-bie%2FExpr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRob-bie%2FExpr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRob-bie%2FExpr/lists"}