Git Product home page Git Product logo

calc's Introduction

#calc

calc is a simple command-line based calculator that supports most of the functions that one can find in a scientific calculator.

##Usage

Compile with,

gcc -std=c99 -o calc main.c -lm

Run with,

./calc [OPTIONS] <EXPRESSION>...

Obviously, the expressions are expected to be quoted within single quotes to prevent bash/shell from parsing them.

##Options

The following flags can be used,

  1. -d, consider all angles to be degrees. Results are also in degrees.
  2. -n, print results of each epressions in a new line instead of separating them by space.
  3. -h (or) --help, print help and exit.

##Expressions calc supports multiple expressions in a single call. For example,

./calc '3.4' 'atan(1/0) - 3.141519265358979323846 * sqrt 3'

The result of each expression is printed either in the same line separated by space or in separate lines if the -n flag is used.

For example, the output of the above is

3.4 -3.870474654

With -n flag,

3.4
-3.870474654

##Features

###Operators The following operators are supported,

+ --> addition
- --> subtraction
* --> multiplication
/ --> division
% --> modulo
! --> factorial
^ --> exponent

Unary '+' and '-' are also supported. '~' and '_' operators act as aliases for unary '-' and '+' respectively.

The '$' (dollar) operator can be used to refer to the result of a particular sub-expression. Sub-expression are created using the ',' (comma) operator. For example,

./calc '7 - 5, sin$1, log($1 - -$2)'

The expression has three sub-expression.

$1 = 7 - 5
$2 = sin$1 = sin(7 - 5)
$3 = log($1 - -$2) = log(7 - 5 - -sin(7 - 5))

The results of all three expressions is printed in comma separated format.

2,0.9092974268,0.4637881228

###Functions

The following function are supported,

sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh, log (base 10), ln (base e), exp (e raised), floor, ceil, round, sqrt, abs, sgn.

sgn returns 1 if positive, -1 if negative else 0.

Trignometric function may have infinite precision related issues. For example,

./calc 'sin pi' 'cos pi'

Results in,

7.338823075E-05 -0.9999999973

The actual values being 0 and -1. pi here is a symbolic constant.

###Symbolic Constants

The following symbolic constants are supported,

pi    -->  3.141519265358979323846
e     -->  2.7182818284590452354
g     -->  1.6180339887498948482
inf   -->  infinity
rand  -->  random double in the range (0, 1). (ironic, i know...)

For example,

./calc 'atan inf' 'rand, sin $1, cos($2 - $1)' '-+_+_++_--~---~+_rand'

1.570796327 0.4473471527,0.4325752577,0.9998908975 0.6601152609

##TODO

  1. Add support for complex numbers.
  2. Implicit multiplication. (sin 3 cos 4 --> sin 3 * cos 4, (9 - 4) (sin 3) --> (9 - 4) * (sin 3))
  3. Symbolic mathematics.

calc's People

Contributors

vivekannan avatar

Watchers

 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.