{"id":16039802,"url":"https://github.com/beartocode/num_parser","last_synced_at":"2025-03-18T04:30:47.750Z","repository":{"id":41400124,"uuid":"503815170","full_name":"BearToCode/num_parser","owner":"BearToCode","description":"A Rust math interpreter and evaluator","archived":false,"fork":false,"pushed_at":"2022-08-12T12:29:48.000Z","size":2809,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-06-14T15:53:23.687Z","etag":null,"topics":["expression-evaluator","math","math-parser","math-parser-library","mathematics","parser","rust","rust-crate","rust-lang","rust-library"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/BearToCode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-15T15:07:18.000Z","updated_at":"2024-06-13T15:03:25.000Z","dependencies_parsed_at":"2022-07-11T03:47:24.103Z","dependency_job_id":null,"html_url":"https://github.com/BearToCode/num_parser","commit_stats":null,"previous_names":["beartocode/numerus"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BearToCode%2Fnum_parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BearToCode%2Fnum_parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BearToCode%2Fnum_parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BearToCode%2Fnum_parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BearToCode","download_url":"https://codeload.github.com/BearToCode/num_parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243902293,"owners_count":20366259,"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":["expression-evaluator","math","math-parser","math-parser-library","mathematics","parser","rust","rust-crate","rust-lang","rust-library"],"created_at":"2024-10-08T23:08:04.788Z","updated_at":"2025-03-18T04:30:47.437Z","avatar_url":"https://github.com/BearToCode.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# num_parser: a math interpreter and evaluator\n\n[![crate](https://img.shields.io/crates/v/num_parser)](https://crates.io/crates/num_parser)\n[![license](https://img.shields.io/github/license/BearToCode/num_parser)](https://github.com/BearToCode/num_parser/blob/master/LICENSE)\n[![docs](https://img.shields.io/docsrs/num_parser)](https://docs.rs/num_parser/1.0.2/num_parser/)\n\n**num_parser** allows you to easily **parse** strings into math expressions\nand **evaluate** them.\n\n## Features\n* Binary and unary operators\n* Supports **multiple value types**:\n    * Bool,\n    * Int,\n    * Float,\n    * [Complex](num::complex::Complex64),\n    * Vector\n* Built-in functions \n* Built-in constants\n* **User-defined functions**: `f(x,y) = xsin(y)+ysin(x)`\n* **User-defined var**: `a = pi/2` or `b = a+2`\n* Define you own functions with **macros**.\n* Understands **ambiguous syntax**, like: `g(x) = pisinx`\n* **Recursion**: `f(x) = branch(x\u003c=2, 1, f(x-1)+f(x-2))`\n* Serde support\n* No panicking\n\nMuch more will be implemented in future releases!\n\n## Use Guide\n\nEvaluating **simple static expressions**:\n```rust\nuse num_parser::*;\n\nassert_eq!(eval(\"2+2\").unwrap(), Value::from(4));\nassert_eq!(eval(\"sin(pi)\").unwrap(), Value::from(0));\nassert_eq!(eval(\"re(10+3i)\").unwrap(), Value::from(10));\n```\n\nUsing **contexts**:\n\n```rust\nuse num_parser::*;\n\nlet mut context = Context::default();\n// Declaring a function\nlet res = eval_with_mutable_context(\n    \"f(x) = branch(x\u003c=2, 1, f(x-1) + f(x-2))\",\n    \u0026mut context\n).unwrap();\n\n// Result is None\nassert_eq!(res, None);\n// Calling the function. We could just use eval_with_static_context at this point\nlet res = eval_with_mutable_context(\"f(10)\", \u0026mut context).unwrap();\n\nassert_eq!(res, Some(Value::from(55)));\n```\n\n## Values\n**Values** are contained inside the [Value enum](Value), which provides useful functions\nto access the contained data:\n\n```rust\nuse num_parser::Value;\n\nlet value = Value::Float(1.0);\n\nassert_eq!(value.as_bool().unwrap(), true);\nassert_eq!(value.as_int().unwrap(), 1);\nassert_eq!(value.as_float().unwrap(), 1.0);\nassert_eq!(value.as_complex().unwrap(), num::complex::Complex::new(1.0, 0.0));\nassert_eq!(value.as_vector(), vec![Value::Float(1.0)]);\n\n// Assign type implicitly:\nlet implicit = Value::from(1.0);\n\nassert_eq!(value, implicit);\n```\n\nNote that, even thought the initial value was a float, it has been **cast** into ints and bools. This\nwas possible since the value had no decimal part and it was a one. If these conditions were not\nmet, the cast would have failed.\n\n## Operators\n**Binary** operators:\n\n| Operator | Description | Precedence |\n|----------|-------------|------------|\n| ^  | Exponentiation                                       | 90 |\n| /  | Division                                             | 70 |\n| *  | Multiplication                                       | 70 |\n| %  | Modulo                                               | 70 |\n| +  | Sum                                                  | 60 |\n| -  | Subtraction                                          | 60 |\n| \u003c  | Less than                                            | 50 |\n| \u003e  | Greater than                                         | 50 |\n| \u003c= | Less or equal to                                     | 50 |\n| \u003e= | Greater or equal to                                  | 50 |\n| == | Equal to                                             | 40 |\n| != | Not equal to                                         | 40 |\n| \u0026\u0026 | Logical AND                                          | 30 |\n| \u0026#124;\u0026#124; | Logical OR                                 | 20 |\n| ,  | Aggregation. Creates vectors                         | 10 |\n| =  | Assignment. Used for functions and vars declarations | 0  |\n\n**Unary** operators:\n\n| Operator | Description | Precedence |\n|----------|-------------|------------|\n| ! | Logical NOT | 80 |\n| - | Negation    | 60 |\n\n## Functions\n\n| Function | Parameters Amount          | Description                                                   |\n|----------|----------------------------|---------------------------------------------------------------|\n| `min`    | \u003e=1                        | Returns the minimum value.                                    |\n| `max`    | \u003e=1                        | Returns the maximum value.                                    |\n| `floor`  | 1                          | Returns the greatest lower integer.                           |\n| `ceil`   | 1                          | Returns the lowest greater integer.                           |\n| `round`  | 1                          | Returns the rounded integer.                                  |\n| `ln`     | 1                          | Returns the natural log of the number.                        |\n| `log`    | 2 (base, arg)              | Returns the logarithm of the number with the specified base.  |\n| `exp`    | 1                          | Returns e^(arg).                                              |\n| `rand`   | 2 (min, max)               | Returns a random float between the two number specified.      |\n| `abs`    | 1                          | Returns the absolute value of a number.                       |\n| `sqrt`   | 1                          | Returns the square root of a number.                          |\n| `branch` | 3 (condition, true, false) | Returns the second argument if the condition is true, the third if it is false. |\n| `sin`    | 1                          | Returns the sine of the angle.                                |\n| `cos`    | 1                          | Returns the cosine of the angle.                              |\n| `tan`    | 1                          | Returns the tangent of the angle.                             |\n| `asin`   | 1                          | Returns the arcsine of the angle.                             |\n| `acos`   | 1                          | Returns the arccosine of the angle.                           |\n| `atan`   | 1                          | Returns the arctangent of the angle.                          |\n| `sinh`   | 1                          | Returns the hyperbolic sine of the angle.                     |\n| `cosh`   | 1                          | Returns the hyperbolic cosine of the angle.                   |\n| `tanh`   | 1                          | Returns the hyperbolic tangent of the angle.                  |\n| `asinh`  | 1                          | Returns the hyperbolic arcsine of the angle.                  |\n| `acosh`  | 1                          | Returns the hyperbolic arccosine of the angle.                |\n| `atanh`  | 1                          | Returns the hyperbolic arctangent of the angle.               |\n| `re`     | 1                          | Returns the natural part of the number.                       |\n| `im`     | 1                          | Returns the imaginary part of the number.                     |\n| `polar`  | 1                          | Returns the polar form (r, theta) of the complex number.      |\n| `arg`    | 1                          | Returns the principal arg of the number.                      |\n| `norm`   | 1                          | Returns the length of the vector (re, im).                    |\n\n## Context\n\n[Contexts](Context) allows you keep track of **user-defined functions** and **variables**, as well\nas settings. They can be created as follows:\n\n```rust\nuse num_parser::*;\n\n// Generate the default context\nlet mut default = Context::default();\n\n// Generate a custom context\nlet mut custom = Context::new(\n    settings::Rounding::NoRounding,\n    settings::AngleUnit::Degree,\n    settings::DepthLimit::NoLimit\n);\n```\n\n### Serde\n\nYou can use the optional feature `serde_support` to let all the public structs\nderive  [`Serialize`](https://docs.rs/serde/1.0.71/serde/trait.Serializer.html) and\n[`Deserialize`](https://docs.rs/serde/1.0.71/serde/trait.Serializer.html).\n\n```rust\n[dependencies]\nnum = { version = \"\u003cversion\u003e\", features = [ \"serde_support\" ] }\n```\n\n## License and contribution\nnum_parser is licensed under a **MIT License**.\n\nFeel free to open issues and pull requests for any problems or ideas you come up with.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeartocode%2Fnum_parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeartocode%2Fnum_parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeartocode%2Fnum_parser/lists"}