All Projects → schneidmaster → eqn

schneidmaster / eqn

Licence: MIT license
A gem to evaluate mathematical equations.

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to eqn

keisan
A Ruby-based expression parser, evaluator, and programming language
Stars: ✭ 48 (+50%)
Mutual labels:  calculator, equation
churnalizer
Analyze your Ruby app for Churn vs Complexity
Stars: ✭ 17 (-46.87%)
Mutual labels:  ruby-gem
daru-io
daru-io is a plugin gem to the existing daru gem, which aims to add support to Importing DataFrames from / Exporting DataFrames to multiple formats.
Stars: ✭ 21 (-34.37%)
Mutual labels:  ruby-gem
N-Matrix-Programmer
A software to write an optimized code that calculates inverse and determinant of N by N matrix.
Stars: ✭ 35 (+9.38%)
Mutual labels:  calculator
cvss
CVSS (Common Vulnerability Scoring System) Calculator CVSSv3.1
Stars: ✭ 35 (+9.38%)
Mutual labels:  calculator
Mather
A powerful expression-based calculator, unit converter, and general computation engine for Android
Stars: ✭ 49 (+53.13%)
Mutual labels:  calculator
calculator
Calculator app designed for elementary OS
Stars: ✭ 73 (+128.13%)
Mutual labels:  calculator
Equation
Solve Math expressions, create equations, define operators and constants
Stars: ✭ 37 (+15.63%)
Mutual labels:  equation
lex
Lex is an implementation of lex tool in Ruby.
Stars: ✭ 49 (+53.13%)
Mutual labels:  ruby-gem
rsc
A handwritten scientific calculator for interpreting equations.
Stars: ✭ 32 (+0%)
Mutual labels:  calculator
rpncalc
RPNCalc: The Command Line Reverse Polish Notation (RPN) Calculator
Stars: ✭ 19 (-40.62%)
Mutual labels:  calculator
calculator
C++ operator precedence parser
Stars: ✭ 73 (+128.13%)
Mutual labels:  calculator
latex in word
LaTeX equation edition in a macro-enabled Word document
Stars: ✭ 29 (-9.37%)
Mutual labels:  equation
ruby terraform
A simple Ruby wrapper for invoking terraform commands.
Stars: ✭ 92 (+187.5%)
Mutual labels:  ruby-gem
hurriyet-cli
A Ruby CLI for Hurriyet
Stars: ✭ 14 (-56.25%)
Mutual labels:  ruby-gem
graphql-remote loader
Performant remote GraphQL queries from within the resolvers of a Ruby GraphQL API.
Stars: ✭ 52 (+62.5%)
Mutual labels:  ruby-gem
Atosym
algebraic expressions parsing and evaluation through a property system based algorithm
Stars: ✭ 15 (-53.12%)
Mutual labels:  calculator
glGraph
A 2d Graphing Calculator written in C++ using Modern OpenGL
Stars: ✭ 15 (-53.12%)
Mutual labels:  calculator
rpn
Command-Line RPN Calculator
Stars: ✭ 22 (-31.25%)
Mutual labels:  calculator
menu-calc
A calculator for Rofi/dmenu(2)
Stars: ✭ 45 (+40.63%)
Mutual labels:  calculator

Build Status Test Coverage Code Climate Gem Version security

Eqn

Eqn uses the Treetop parser generator to safely evaluate mathematical expressions in Ruby.

Installation

Add this line to your application's Gemfile:

gem 'eqn'

And then execute:

$ bundle

Or install it yourself as:

$ gem install eqn

Usage

To evaluate an equation string, run the following:

Eqn::Calculator.calc('1 + 1')
# => 2

You can also check if an equation is valid:

Eqn::Calculator.valid?('1 + 1')
# => true
Eqn::Calculator.valid?('1 + / 1')
# => false

If you want to peek at how Eqn is parsing an equation, run the following to get the syntax tree:

Eqn::Parser.parse('1 + 1')
# => <syntax tree is printed>

Eqn 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.)

Variables

Eqn 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:

Eqn::Calculator.calc('a + 1', a: 1)
# => 2

Eqn::Calculator.calc('5 * value', value: 2.5)
# => 12.5

Eqn::Calculator.calc('if(a > 10, b, c)', a: 15, b: 1, c: 0) # see below for function documentation
# => 1

If you need distinct equations with variable sets, you can instantiate separate instances:

calc = Eqn::Calculator.new('1 + abc', abc: 2.0)
calc.calc
# => 3.0
calc_two = Eqn::Calculator.new('1 + abc', abc: 5.0)
calc_two.calc
# => 6.0

On calculator instances, variables can be set via key-value syntax, hash syntax, or as a method:

calc = Eqn::Calculator.new('1 + abc')
calc.set(:abc, 3.0)
calc.abc
# => 3.0
calc.set(abc: 4.0)
calc.abc
# => 4.0
calc.abc = 5.0
calc.abc
# => 5.0

Functions

Eqn presently supports four functions:

if

Syntax: if(comparison, value_if_true, value_if_false)

For example, if(5 > 3, 1, 2) would evaluate to 1.

round

Syntax: round(number, [decimals])

Rounds 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).

roundup

Syntax: roundup(number, [decimals])

Rounds 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).

rounddown

Syntax: rounddown(number, [decimals])

Rounds 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).

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

The 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.)

Authorship

Written by Zach Schneider for Aha!, the world's #1 product roadmap software

Contributing

  1. Fork it (https://github.com/schneidmaster/eqn/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].