LinuxCommandLibrary

calc

Perform arithmetic calculations

TLDR

Start calc in interactive mode

$ calc
copy

Perform a calculation in non-interactive mode
$ calc '[85 * (36 / 4)]'
copy

Don't format the output (for use with [p]ipes)
$ calc -p '[4/3 * pi() * 5^3]'
copy

Perform a calculation and then switch to [i]nteractive mode
$ calc -i '[sqrt(2)]'
copy

Start calc in a specific permission [m]ode (0 to 7, defaults to 7)
$ calc -m [mode]
copy

View an introduction to calc
$ calc help intro
copy

View an overview of calc
$ calc help overview
copy

Open the calc manual
$ calc help
copy

SYNOPSIS

calc [options] [expression ...]
Common invocation: calc [-e expression] [-f file] [-i] [-V] [-h] [expression ...]

PARAMETERS

-e or --expression
    Evaluates the given expression directly from the command line. Multiple -e options can be used.

-f or --file
    Reads and executes expressions from the specified file. Useful for scripts or loading predefined functions/variables.

-i or --interactive
    Forces calc into interactive mode, even if input is being redirected from a pipe or file.

-h or --help
    Displays a brief help message with command-line options and exits.

-V or --version
    Prints the version information of the calc utility and exits.

DESCRIPTION

The calc command provides a highly versatile and powerful arbitrary-precision calculator for the command line. It is typically found as part of the apcalc package on Linux systems. This utility offers a rich set of features, including support for complex numbers, vectors, matrices, trigonometric functions, logarithms, and the ability to define user-specific variables and functions. calc supports various number bases such as binary, octal, decimal, and hexadecimal.

It can be used in an interactive mode for exploratory calculations or non-interactively to process expressions provided as command-line arguments or read from files, making it well-suited for scripting and automated tasks. Its arbitrary precision capabilities ensure that calculations, even those involving extremely large or very small numbers, are performed without the loss of precision often associated with fixed-precision floating-point systems.

CAVEATS

The name calc can refer to different calculator programs on various Unix-like systems (e.g., apcalc, GNU calc). This analysis primarily describes the widely adopted apcalc version, which is common on Linux. While extremely powerful, its extensive features and unique syntax might present a steeper learning curve for new users. It is designed for numerical computation and does not offer advanced symbolic algebra capabilities found in Computer Algebra Systems.

<B>ARBITRARY PRECISION ARITHMETIC</B>

calc supports arbitrary precision arithmetic, meaning calculations are performed with as many digits as necessary, limited only by available memory. This prevents the loss of precision commonly associated with fixed-precision floating-point systems, making it suitable for scientific and financial calculations requiring high accuracy.

<B>COMPLEX NUMBERS, VECTORS, AND MATRICES</B>

Beyond basic arithmetic, calc natively supports complex numbers, vectors, and matrices. This robust capability allows users to perform advanced linear algebra operations and complex analysis directly from the command line, offering powerful functionality for engineers, mathematicians, and scientists.

<B>NUMBER BASES AND UNITS</B>

Users can easily switch between and perform calculations in various number bases, including binary (e.g., 0b101), octal (e.g., 0o777), decimal, and hexadecimal (e.g., 0xFF). Additionally, calc has support for physical units, allowing conversions and calculations with units directly (e.g., 10m + 5ft).

HISTORY

The most prominent calc utility on Linux systems, apcalc, was originally developed by Dave Gillespie. It has been a long-standing and essential tool in the Unix/Linux environment, providing advanced mathematical capabilities at the command line for decades. Its development has consistently focused on delivering arbitrary precision and a rich set of mathematical functions, solidifying its position as a go-to utility for complex numerical computations in both scripting and interactive usage.

SEE ALSO

bc(1), dc(1), expr(1), awk(1), python(1)

Copied to clipboard