Code Monkey home page Code Monkey logo

calculator's Introduction

calculator

Calculator computing arithmetic expressions.

Features

  • Calculator uses Reverse Polish Notation for evaluation of math expressions.
  • Defines some builtin functions:
    • trigonometric (sin, cos, tan),
    • inverse trigonometric (asin, acos, atan, atan2),
    • hyperbolic (sinh, cosh, tanh),
    • log and exp,
    • abs, sgn and sqrt,
    • conversions: degrees to radians (d2r), radians to degrees (r2d),
    • min and max,
    • neg (unary minus).
  • Predefined constants: PI and E.
  • Calculator allows user to define own custom functions and constants.

Expression notation

As mentioned before, calculator evaluates expressions using RPN and shunting-yard algorithm. No additional syntax check is performed, which makes expression syntax quite liberal. Here are some examples of valid expressions:

> 2+2*2
6.0

> 2^2^3
256.0

> sin(PI)
1.2246467991473532E-16

> cos 0*2
2.0

> atan2(2+2, 3+3)
0.5880026035475675

> log exp -1
-1

> 7-(2*PI + cos 12.3456)
-0.25891429587906956

As can be seen, brackets are not needed for unary functions. All operators except power (^) are evaluated left to right. Power operator as well as functions are evaluated right to left. This allows function composition without explicit using brackets.

Moreover, functions have higher priority than operators. Following table shows operator precedence and associativity.

Function or operatorPriorityAssociativityComment
( ) ,
-1RightNo actions are executed; these operators are only used as “terminals” in the expression.
Additive: +, -1Left
Multiplicative: *, /, %2Left
Power: ^3RightEvaluated right to left for expressions like 2^3^4 (== 2^(3^4))
Function10Right
Constant100LeftConstants are a special kind of functions that take no parameters. “Ultra high” priority causes them to be always evaluated first, and left associativity assures they are left-to-right evaluated.

Special commands

Special commands start with colon (:). Here is the list of all defined commands:

  • :c, :del and :ce - clear and clear all,
  • :func, :const and :s - user defined functions and constants,
  • :p and :pd - print list of functions and constants.

Clearing results

  • :c - clear. Result of every expression evaluation is stored in variable ans. Command :c erases last result and sets ans to 0.
  • :del {name} - deletes constant or function with name {name}. Default builtin functions and constants (e.e. sin or PI) cannot be removed.
  • :ce - clear all. Clears last result (see :c) and deletes all user defined functions and constants.

Defining functions and constants

Custom functions and constants are nothing more than expressions. Arguments for function are denoted in its body as {0}, {1}..., {N}. Constants are functions without arguments, which makes them constant expressions.

Function or constant name cannot be equal to any of the default function or constant. If custom (non-default) function or constant with the same name already exists, new function or constant overwrites it.

  • :func {name} {expression} - define a function with name {name}. Examples of function definitions:
    • :func square {0}*{0} - defines an unary function named square that multiplies the argument by itself,
    • :func dist max({0},{1}) - min({0},{1}) - defines binary function calculating absolute distance between two numbers.
  • :const {name} {expression} - define a constant with name {name}. Examples of constant definitions:
    • :const ONE 1 - constant with value 1,
    • :const PI_2 PI/2 - constant with value PI / 2.
  • :s {name} - save last result (i.e. ans) to a new constant named {name}.

Printing functions and constants

  • :p - print all custom functions and constants.
  • :pd - print all default, predefined functions and constants (e.g. sin, abs, PI).

calculator's People

Contributors

zarebski-m 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.