9

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?

0

8 Answers 8

8

bc & dc

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

bc 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 calculations:

2
2
5+5
10

dc examples

$ dc

Syntax to use for your calculations:

2 p
2 p
5 5 +p
10 p

Note that dc uses postfix, so you'll need to specify operands first, then use an operator such as p (print) or + (add)

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

References

2
  • Since this isn't an Ubuntu-specific question, maybe link to calc upstream instead of to the Ubuntu package?
    – mattdm
    Commented Dec 17, 2017 at 15:15
  • @mattdm - thanks added that link, I left the other link simply b/c I was linking to a man page on calc, not necessarily the ubuntu package.
    – slm
    Commented Dec 18, 2017 at 1:13
6

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
2
  • 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. Commented Dec 14, 2013 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
    Commented Dec 14, 2013 at 3:20
5

In zsh:

$ autoload zcalc  # best in ~/.zshrc
$ zcalc
1> 8*(6-4)
16
2> $1*2
32
4

insect has both web- and terminal-based versions:

insect example usage

2

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

1

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)
0

A other way is to use a interpreter like Python 3:

$ python3
> from math import *
> 213/53*exp(0.8)/asin(3)
9.645423462356044

Have the advantage, that you can script everything, there are allready many functions (you could also import cmath if you need complex numbers), you do not need to set any precision for the most cases and python is already installed on the most Linux installations.

Python 3.x is better for a generic calculator than Python 2.x, because Python 3.x do a float point divisions if the result is is not a int.

$ python3
> 3/2
1.5
$ python2
> 3/2
1

The major downside, for me at least, is, that floats are not printed in the scientific notation if there are not very big or small, without a format string:

> exp(35)
1586013452313430.8
> "{:e}".format(exp(35))
'1.586013e+15'
-1

Simply you can use python and enjoy:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .