Giter Club home page Giter Club logo

calculy's Introduction

pipeline status

Calculy

Lightweight, zero dependency calculator engine.

Only ≈2kB min + gzipped! ✨

Installation

npm install calculy

Usage

import { calculate } from "calculy";

calculate("1+2(3!)^2"); // 73
calculate("ans*2", { constants: { ans: 3 } }); // 6
calculate("ans^x", { constants: { ans: 3, x: 2 } }); // 9
calculate("cos(90)"); // -0.44807361612
calculate("cos(90)", { deg: true }); // 0

You can also directly include ./dist/calculy.min.js from the npm package. This will make all calculy functions available under the global variable Calculy.

Syntax

Calculy supports most basic algebra that you would need for regular calculations.

Addition & Substraction

2+2 = 4

2-2 = 0

Multiplication & Division

2*2 = 4

1/2 = 0.5

Exponents

2^2 = 4

Grouping

2^(2+1) = 8

4*5-2^2-3 = 1

Constants

e ≈ 2.718281828459045

π or pi ≈ 3.141592653589793

φ or phi ≈ 1.618033988749895

τ or tau ≈ 6.283185307179586

Custom constants

ans*2 = 3 (if the provided constant ans is 3) x^2 = 16 (if the provided constant x is 4)

Shorthand multiplication

2pi ≈ 6.283185307179586

(4-2)(3-4) = -2

3(4-2)^2 = 12

Percentages

50% = 0.5

-200% = -2

Big number notation

2.5e+6 = 2500000

2.5e-3 = 0.0025

Absolute

|-3| = 3

|(2*2-6)| = 2

Factorials

5! = 5

0.5! ≈ 0.8862269254527586

Roots

√(9) or √(9, 2) or sqrt(9) = 3

√(27, 3) or cbrt(27) = 3

√(81, 4) = 3

Logarithms

log(100) or log(100, 10) = 2

log(8, 2) = 3

ln(e) or log(e, e) = 3

Trigonometry

sin(pi/2) = 1

cos(2pi) = 1

tan(pi/4) ≈ 1

asin(0.5) ≈ 0.5235987755982989

acos(0.5) ≈ 1.0471975511965979

atan(0.5) ≈ 0.4636476090008061

Other functions

rand() = random number between 0 and 1

rand(2) = random number between 0 and 2

rand(2, 5) = random number between 2 and 5

round(2.6) = 3

floor(2.6) = 2

ceil(2.2) = 3

ceil(2.2) = 3

API

calculate(code: string, [options: Object]) => number

Will execute the math and give the answer. Can throw a SyntaxError.

Options

  • constants: Custom constants (Object.<number>), will override default constants with same name. (default: {})
  • deg: Whether to use degrees instead instead of radians for trigonometry functions. (default: false)

tokenize(code: string) => string[]

Will return a list of the tokens that calculy parses. Can come in handy for input validation/formatting.

parse(tokens: string[], [options: Object]) => Expression

Will return the raw AST, useful for custom evaluation. Can throw a SyntaxError.

evaluate(expression: Expression, [options: Object]) => number

Will evaluate a calculation expression;

calculy's People

Stargazers

 avatar

Watchers

James Cloos avatar Niels Gerritsen avatar

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.