{"id":13878188,"url":"https://github.com/schneidmaster/eqn","last_synced_at":"2025-04-23T02:17:57.604Z","repository":{"id":32396916,"uuid":"35973501","full_name":"schneidmaster/eqn","owner":"schneidmaster","description":"A gem to evaluate mathematical equations.","archived":false,"fork":false,"pushed_at":"2021-02-01T21:58:05.000Z","size":97,"stargazers_count":36,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-23T02:17:50.268Z","etag":null,"topics":["calculator","equation","ruby","ruby-gem"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/schneidmaster.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-20T20:50:18.000Z","updated_at":"2024-10-21T15:25:29.000Z","dependencies_parsed_at":"2022-09-09T04:11:46.215Z","dependency_job_id":null,"html_url":"https://github.com/schneidmaster/eqn","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schneidmaster%2Feqn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schneidmaster%2Feqn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schneidmaster%2Feqn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/schneidmaster%2Feqn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/schneidmaster","download_url":"https://codeload.github.com/schneidmaster/eqn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250354518,"owners_count":21416752,"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":["calculator","equation","ruby","ruby-gem"],"created_at":"2024-08-06T08:01:42.245Z","updated_at":"2025-04-23T02:17:57.581Z","avatar_url":"https://github.com/schneidmaster.png","language":"Ruby","readme":"[![Build Status](https://circleci.com/gh/schneidmaster/eqn.svg?style=shield)](https://circleci.com/gh/schneidmaster/eqn) \n[![Test Coverage](https://codeclimate.com/github/schneidmaster/eqn/badges/coverage.svg)](https://codeclimate.com/github/schneidmaster/eqn/coverage)\n[![Code Climate](https://codeclimate.com/github/schneidmaster/eqn/badges/gpa.svg)](https://codeclimate.com/github/schneidmaster/eqn)\n[![Gem Version](https://badge.fury.io/rb/eqn.svg)](http://badge.fury.io/rb/eqn)\n[![security](https://hakiri.io/github/schneidmaster/eqn/master.svg)](https://hakiri.io/github/schneidmaster/eqn/master)\n\n# Eqn\n\nEqn uses the Treetop parser generator to safely evaluate mathematical expressions in Ruby.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'eqn'\n```\n\nAnd then execute:\n\n```bash\n$ bundle\n```\n\nOr install it yourself as:\n\n```bash\n$ gem install eqn\n```\n\n## Usage\n\nTo evaluate an equation string, run the following:\n\n```ruby\nEqn::Calculator.calc('1 + 1')\n# =\u003e 2\n```\n\nYou can also check if an equation is valid:\n\n```ruby\nEqn::Calculator.valid?('1 + 1')\n# =\u003e true\nEqn::Calculator.valid?('1 + / 1')\n# =\u003e false\n```\n\nIf you want to peek at how Eqn is parsing an equation, run the following to get the syntax tree:\n\n```ruby\nEqn::Parser.parse('1 + 1')\n# =\u003e \u003csyntax tree is printed\u003e\n```\n\nEqn follows the standard mathematical order of operations: parentheses, exponentiation, multiplication/division, addition/subtraction. It ignores  whitespace, so `1 + 1` === `1+1`. (However, it does not ignore whitespace between two numbers, so `1 1` is invalid.)\n\n### Variables\n\nEqn supports dynamically inserting values into an equation. Variables are passed to the calculator method via a hash; variable names may contain any lowercase or uppercase letters. Some examples:\n\n```ruby\nEqn::Calculator.calc('a + 1', a: 1)\n# =\u003e 2\n\nEqn::Calculator.calc('5 * value', value: 2.5)\n# =\u003e 12.5\n\nEqn::Calculator.calc('if(a \u003e 10, b, c)', a: 15, b: 1, c: 0) # see below for function documentation\n# =\u003e 1\n```\n\nIf you need distinct equations with variable sets, you can instantiate separate instances:\n\n```ruby\ncalc = Eqn::Calculator.new('1 + abc', abc: 2.0)\ncalc.calc\n# =\u003e 3.0\ncalc_two = Eqn::Calculator.new('1 + abc', abc: 5.0)\ncalc_two.calc\n# =\u003e 6.0\n```\n\nOn calculator instances, variables can be set via key-value syntax, hash syntax, or as a method:\n\n```ruby\ncalc = Eqn::Calculator.new('1 + abc')\ncalc.set(:abc, 3.0)\ncalc.abc\n# =\u003e 3.0\ncalc.set(abc: 4.0)\ncalc.abc\n# =\u003e 4.0\ncalc.abc = 5.0\ncalc.abc\n# =\u003e 5.0\n```\n\n### Functions\n\nEqn presently supports four functions:\n\n**if**\n\nSyntax: `if(comparison, value_if_true, value_if_false)`\n\nFor example, `if(5 \u003e 3, 1, 2)` would evaluate to `1`.\n\n**round**\n\nSyntax: `round(number, [decimals])`\n\nRounds the number up if the decimal is greater than 0.5 and down otherwise (i.e. \"normal\" rounding). Optionally, pass a number of decimal places to round to as a second parameter (the default is 0).\n\n**roundup**\n\nSyntax: `roundup(number, [decimals])`\n\nRounds the number up (i.e. ceiling function). Optionally, pass a number of decimal places to round to as a second parameter (the default is 0).\n\n**rounddown**\n\nSyntax: `rounddown(number, [decimals])`\n\nRounds the number down (i.e. floor function). Optionally, pass a number of decimal places to round to as a second parameter (the default is 0).\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.\n\nThe gem uses rspec for testing and appraisal to test against multiple versions of treetop (the only runtime dependency). From the command line, run `bundle exec appraisal install` to install gems and `bundle exec appraisal rspec` to run the complete test suite. (You can still run `bundle exec rspec` to only test against the latest version of treetop while developing.)\n\n## Authorship\n\nWritten by Zach Schneider for [Aha!, the world's #1 product roadmap software](http://www.aha.io/)\n\n## Contributing\n\n1. Fork it ([https://github.com/schneidmaster/eqn/fork](https://github.com/schneidmaster/eqn/fork))\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschneidmaster%2Feqn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fschneidmaster%2Feqn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fschneidmaster%2Feqn/lists"}