Giter Club home page Giter Club logo

node-calc-cli's Introduction

CLI Calculator in JavaScript for Node.js

First, install node.js by doing something like:

Then, run the calculator CLI by executing:

./calculator

You should see a prompt that looks like:

$ ./calculator 
? 

Start typing expressions!

Basic maths are supported:

? 3 * 3
=> 9
? 3 + 4 * 3
=> 15
? (3 + 4) * 3
=> 21

It's JavaScript math though (for now), so you can't always trust the results...

? .1 + .2
=>  0.30000000000000004

The CLI supports exponentiation with **:

? 3 * 4**5
=>  3072

It supports variable assignments, one per line. Variable names must be lowercase, and consist only of letters.

? x = 3
=> 3
? x + 3
=> 6
? x = 3
=> 3
? y = 4
=> 4
? x + y
=> 7
? asdf = 3 + 4 ** 7 * 4 + (3 + 4)
=> 65546

You can also call functions. Calls are passed through to functions defined on JavaScript's Math object:

? sin(2)
=>  0.9092974268256817  // (radians)
? pow(3 * 4, 1 + 1)
=> 144

Nested function calls are supported as well:

? pow( sin(2), 3 ** (0 + 1) )
=>  0.7518269446689928

Known Issues

  • The error messages thrown by the parser are probably next to worthless most of the time, but if you don't mistype anything, it'll work great!
  • The actual evaluation is currently delegating to the JS engine for evaluation, so it's basically double precision floating point math, which is mostly fine. Someday I plan to integrate (probably very slow) arbitrary-precision arithmetic. I have the beginnings of a BigInt class in my projecteuler solutions, but it doesn't handle subtraction/division/negative numbers yet. Switching to aribtrary precision numbers would also eliminate the usefulness of the JS Math function passthrough.
  • I think it would be fun to extend to allow the user to define arbitrary functions.

node-calc-cli's People

Contributors

roblg avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.