4

Is there a terminal calculator for the Mac that's similar to concalc from Ubuntu?

concalc allows you to calculate an expression by prefixing it with the tool name (more on the concalc man page)

Expected usage:

concalc 2*(3+4/9)^3
1
  • If you have a license of MATLAB, you could do much more complex calculations through terminal (although it has a GUI app) 😛
    – AVelj
    Feb 24, 2023 at 23:32

4 Answers 4

11

Your Mac comes preinstalled with bc;

bc -le "2*(3+4/9)^3"
bc <<< "2*(3+4/9)^3" -l

both produce

81.73113854595336076784

(thanks @Gilby for the more natural syntax)

The -l option is needed to change the precision; without it, it rounds 4/9 down to 0 and the result will be 54 instead.

It also has an interactive mode (bc -l) where you can enter expressions in the prompt.

1
  • 1
    Thank you, works as expected. Added an alias alias '?'='bc -le' to .zshrc so I can execute it e.g. ? 2+2 that outputs 4
    – tomrozb
    Feb 26, 2023 at 18:04
4

In addition to Glorfindel’s answer (should be accepted), there is Calc (free) which is a C style arbitrary precision calculator. I’m just posting this as an excellent alternative to consider.

It’s available via MacPorts and Homebrew. I’ve not used this on a Mac, but in FreeBSD. What I liked about it was that you could either use it as a single CLI command or in interactive mode; just type calc and begin entering calculations

Calc allows you to use variables and functions in your calculations:

% calc "v=2; 5^v"
  25

% calc "v=3; x=v^2; sqrt(x)"
  3

% calc "define myfunc(a,b) = a^b; myfunc(8,2)"
myfunc(a,b) defined
    64

Additionally, you can create a file of calculations and have them read into calc with a calc -f filename operator. This is helpful when defining your own complex functions.

1
  • That's a good option, not for my use case, but maybe would help somebody else. Thanks for answering!
    – tomrozb
    Feb 26, 2023 at 18:07
4

This answer discusses a few possibilities that are bundled with macOS.

Shell built-in features

In this section, $ represents a bash prompt and % represents a zsh prompt.

For simple operations, if you don't mind typing a little more punctuation, you can use the shell's built-in arithmetic. The shell evaluates arithmetic expressions inside $((…)) or $[…] (they're exactly equivalent).

# bash or zsh
$ echo $[3*4] $((1/2))
12 0

Bash can only do integer operations. Zsh can also do floating point operations.

# zsh only
% echo $[1./2]
0.5

To set up easier access to arithmetic in zsh without having to type punctuation, see https://unix.stackexchange.com/questions/700892/evaluate-terms-in-zsh-without-a-command/700946#700946. (I don't recommend it though: it's a lot of work for little benefit compared to typing a couple of punctuation characters or running zcalc.)

In zsh, you can get additional mathematical functions by loading the zsh/mathfunc module.

% zmodload zsh/mathfunc
% echo $[sin(1)]
0.8414709848078965

And by running zmathfunc, you get access to a few other functions.

% autoload -zU zmathfunc
% zmathfunc
% echo $((sum(1,2,3)))
6

Zsh also comes with a calculator mode, which gives access to the same operators and functions, plus a few more features like the constant PI, output format customization, and a stack. See the manual for details. In particular, run zcalc -f to perform floating-point operations all the time (by default, / calculates the integer quotient if both operands are integers).

% autoload -zU zcalc
% zcalc
> 3/2
1
> 3/2.
1.5
> sin(PI/2)
1.
> q
% zcalc -f
> 3/2
1.5

To make all of this available by default in all interactive shells, make sure you're using zsh and put this in your ~/.zshrc:

zmodload zsh/mathfunc
autoload -zU zmathfunc
zmathfunc
autoload -zU zcalc

bc and dc

bc and dc are two classic Unix calculator programs. bc uses classical infix notation whereas dc uses reverse Polish notation (RPN).

Note that both tools work with a defined number of digits (called the scale) after the decimal point, rather than floating point. The default scale is 0.

% bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
5/3
1
scale=4
5/3
1.6666
quit

(In the session above, the bold text is what I typed, and the rest is displayed by the bc, or by the shell on the first line.)

The command to exit bc is quit. The command to exit dc is q (quit also works). You can also type Ctrl+D at the beginning of a line.

Python

Python comes bundled with the system and can offer a very powerful calculator-like experience. Basic arithmetic operators are available out of the box. Common mathematical functions are in the math module, and some basic statistical functions in the statistics module. There's also cmath for complex numbers, fractions for rational numbers, and decimal for decimal arithmetic (rather than binary floating-point).

% python3
Python 3.8.9 (default, Oct 26 2021, 07:25:53)
[Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from math import *
>>> 3//2
1
>>> 3/2
1.5
>>> sin(pi)
1.2246467991473532e-16
>>> exit()

The command to exit Python is exit(). You can also type Ctrl+D at the beginning of a line.

3

You can compile concalc for macOS yourself.

  • Get Homebrew and use it to install cmake
  • Get the latest concalc source from Sourceforge (version 0.9.3 dated 2010-04-05)
  • tar xzf concalc-0.9.3.tar.gz to unpack it
  • cd concalc-0.9.3
  • cmake . to build the Makefile (ignore the warnings)
  • make, again ignore the warnings.

This will build concalc into the current directory.

$ ./concalc -v

Version: 0.9.3 2010-04-04
Calculator algorithms: extcalc v0.9.3 2010-03-28

Author:
Rainer Strobel
http://extcalc-linux.sourceforge.net
2010
$ ./concalc 2*(3+4/9)^3
81.7311385459534
1
  • Thank you, that's also a very good option :)
    – tomrozb
    Feb 26, 2023 at 18:05

You must log in to answer this question.

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