4.0.0 • Published 1 month ago

@algorithm.ts/calculator v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

A tiny calculator for number arithmetics such as +-*/().

Install

  • npm

    npm install --save @algorithm.ts/calculator
  • yarn

    yarn add @algorithm.ts/calculator

Usage

// Perform integer arithmetics.
import calculator from 'algorithm.ts/calculator'
// or
import { calculator } from 'algorithm.ts/calculator'

// Perform decimal arithmetics.
import { decimalCalculator } from 'algorithm.ts/calculator'

// Perform bigint arithmetics.
import { bigintCalculator } from 'algorithm.ts/calculator'

Examples

  • integer arithmetics

    import calculator from '@algorithm.ts/calculator'
    
    calculator.calculate('3/2')                  // => 1
    calculator.calculate('-2+1')                 // => -1
    calculator.calculate('-2*3 + 2*5*3/6')       // => -1
    calculator.calculate('(1+(4+5+2)-3)+(6+8)')  // => 23
  • decimal arithmetics

    import { decimalCalculator as calculator } from '@algorithm.ts/calculator'
    
    calculator.calculate('3/2')                  // => 1.5
    calculator.calculate('-2+1')                 // => -1
    calculator.calculate('-2*3 + 2*5*3/6')       // => -1
    calculator.calculate('(1+(4+5+2)-3)+(6+8)')  // => 23
  • bigint arithmetics

    import { bigintCalculator as calculator } from '@algorithm.ts/calculator'
    
    calculator.calculate('22222222222222222222222222222 * 3333333333333333333323232')
    // => 74074074074074074073849599999259259259259259259261504n
  • Illegal inputs

    import calculator from '@algorithm.ts/calculator'
    
    calculator.calculate('-2++1')      // => SyntaxError
    calculator.calculate('-2*/23')     // => SyntaxError
    calculator.calculate('1+(4+5+2))') // => SyntaxError
    calculator.calculate('1+(4+5+2')   // => SyntaxError
  • A solution of https://leetcode.com/problems/basic-calculator/

    import calculator from '@algorithm.ts/calculator'
    export function calculate(expression: string): number {
      return calculator.calculate(expression)
    }
  • A solution of https://leetcode.com/problems/basic-calculator-ii/

    import calculator from '@algorithm.ts/calculator'
    export function calculate(expression: string): number {
      return calculator.calculate(expression)
    }

Related

4.0.0

1 month ago

3.1.1

11 months ago

3.1.0

12 months ago

3.0.0-alpha.8

1 year ago

3.0.0

1 year ago

3.0.0-alpha.3

1 year ago

3.0.0-alpha.2

1 year ago

3.0.0-alpha.5

1 year ago

3.0.0-alpha.4

1 year ago

3.0.0-alpha.7

1 year ago

3.0.0-alpha.6

1 year ago

3.0.0-alpha.1

1 year ago

3.0.0-alpha.0

2 years ago