Giter Club home page Giter Club logo

calc's Introduction

Calc

Calc is safe, simple, pure Ruby mathematical expressions evaluator (calculator) library.

Although based on Ruby 'eval', it takes special care to sanitize the expression.

Calc supports all basic mathematical operations +, -, *, / and the power function (through ** operator). Calc also supports parenthesis and nesting. Calc does NOT support advanced mathematical functions like trigonometry, logarithm, etc.

Calc plays well with Rails ActiveModel::Validations. If the supplied expression is invalid (nil, blank, or syntax error), it returns the expression itself verbatim, thus allowing to handle errors via Rails validations.

Usage

require 'calc'

Calc.evaluate( "2 + 2" )  # => 4

Calc.evaluate( "2 * (1 + 9)" )  # => 20

Calc.evaluate( "2 ** (1.0 / 2)" )  # => 1.4142135623730951  (the square of two)

Calc.evaluate( "( ( 2 - 3 ) / 13 * ( 50 + 325.843 ) ) ** 3" )  # => -53090815.704202116

Installation

gem install calc

Using Bundler?

Add the following to your Gemfile:

gem 'calc'

Then run as usual:

bundle

Calc is safe

Calc defines a white list of characters allowed in the expression to prevent code injection attacks. All letters are outlawed as well as majority of other characters. Calc comes with tests.

License

Copyright (C) 2010 Piotr 'Qertoip' Włodarek. Distributed under the MIT License.

calc's People

Stargazers

holin he avatar Uladzislau Bondar avatar ecomarine avatar Marek Kaluzny avatar Denis Korobitsin avatar Tobias Casper avatar Adam Dilek avatar K Patel avatar Piotr Włodarek avatar

Watchers

Piotr Włodarek avatar  avatar

Forkers

ansarbek genzade

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.