Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm looking for a Linux-compatible TTY-based calculator. For example:

user@host:~$ calculate
> 2
2
user@host:~$ calculate
> 8*6-4
44
user@host:~$ calculate
> 8*(6-4)
16

Is there anything like this that supports basic operations, some built-in functions like atan(), and possibly custom functions via scripting?

share|improve this question
add comment

5 Answers 5

up vote 7 down vote accepted

bc & dc

bc and dc are the 2 calculators that I'll often use when needing access from a terminal.

Examples

$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

Then you can type your questions:

2
2
5+5
10

When you're done you can get out with a Ctrl+C.

Test drive

These calculators are pretty feature rich.

scaling

scale=5
193 * 1/3
64.33333

equations

principal=100
ir = 0.05
years = 5
futurevalue = principal * (1 + ir)^years

futurevalue
127.62800

your examples

8*6-4
44

8*(6-4)
16

calc

If you want something a little more interactive there's calc.

Example

$ calc
C-style arbitrary precision calculator (version 2.12.4.4)
Calc is open software. For license details type:  help copyright
[Type "exit" to exit, or "help" for help.]

; 10+10
20
; 8*6-4
    44
; 8*(6-4)
    16
; 

You can use the up/down arrows to go through past commands and it also has interactive help.

; help

Gives you this:

For more information while running calc, type  help  followed by one of the
following topics:

    topic               description
    -----               -----------
    intro               introduction to calc
    overview            overview of calc
    help                this file

    assoc               using associations
    builtin             builtin functions
    command             top level commands
    config              configuration parameters
    custom              information about the custom builtin interface
    define              how to define functions
    environment         how environment variables effect calc
    errorcodes          calc generated error codes
    expression          expression sequences
    file                using files
    history             command history
    interrupt           how interrupts are handled
    list                using lists
    mat                 using matrices
    ...

See the man page for more info.

share|improve this answer
add comment

There are many answers to your question...

The simple ones you could do in the shell.

$ echo $((8*(6-4)))
16

As a dedicated program there is bc.

$ echo "8*(6-4)" | bc
16

Custom functions via scripting? Well, shell scripts and bc both have them, in a way. Depends on how far you want to take it.

Why not Python? It's easy to learn.

$ python
>>> from math import atan
>>> 8*(6-4)+atan(0)
16.0
share|improve this answer
    
I've actually been using Python for this lately, but that's the reason I want something else. It gets annoying having to import something for every function. I was hoping more for something self-contained, like Speedcrunch (i.imgur.com/Cn6GejG.png), but command line obviously. –  Lucas Phillips Dec 14 '13 at 2:22
1  
You can solve import problem by defining PYTHONSTARTUP environment variable and preimporting stuff in the file pointed to by the variable. –  Ketan Dec 14 '13 at 3:20
add comment

In zsh:

$ autoload zcalc  # best in ~/.zshrc
$ zcalc
1> 8*(6-4)
16
2> $1*2
32
share|improve this answer
add comment

Maxima CAS :

   L(t):=exp(%i*t*2*%pi); /* unit circle parametrised with angle in turns */
   plot2d(
   [atan2(imagpart(L(x)),realpart(L(x)))],
   [x,0,1],
   [y,-2*%pi,2*%pi],
   [plot_format, gnuplot],
   [gnuplot_term,"png"],
   [gnuplot_out_file, "atan2.png"],
   [legend, "atan2"],
   [xlabel,"angle in radians"], 
   [ylabel,"angle in radians"],
   [gnuplot_preamble,"
   set key left top;
   set xtics ('pi/2' 0.25, 'pi' 0.5, '3pi/2' 0.75,'2pi' 1.0);
   set ytics ('-2pi' -6.283, '-pi' -3.1415, '-pi/2' -1.5708, '0' 0, 'pi/2'  1.5708, 'pi' 3.1415, '2pi' 6.283);
   set grid xtics ytics "]
  );

HTH

share|improve this answer
add comment

Here's a little bash hack that will let you do simple arithmetic directly on the commandline.

alias calc='set -o noglob; docalc'
function docalc { perl -e "print STDOUT $*, \"\\n\""; set +o noglob; }

Then you can do, e.g.,

calc 3 * 15 + 5

Unfortunately it doesn't play well with parentheses. (If I recall correctly you can get tcsh to accept those too, but I couldn't persuade bash to.)

PS. You could rely on bash for the arithmetic, by replacing the perl call with echo $(( $* )); but that'll give you integer division for 5/6, etc. Perl's arithmetic is more useful.

Alternative: If your only beef with python is that you need to import math, the following is your friend:

% alias calc='python -i -c "from math import *"'
% calc
>>> 5 * atan(0.25)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.