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] [file]

PARAMETERS

-b base
    Set the output base (2-36). Defaults to 10.

-c
    Start with a cleared calculator state (no saved variables or functions).

-d digits
    Set the number of decimal digits to use for calculations.

-e expression
    Evaluate the given expression and exit.

-f file
    Execute commands from the specified file.

-h
    Display help message.

-i
    Ignore case for variable names.

-l
    List built-in functions.

-p
    Print precision and base settings.

-q
    Quiet mode: suppresses prompts and banner.

-r base
    Set the input base (2-36). Defaults to 10.

-s
    Silent mode: suppresses all output (except errors).

-v
    Display version information.

DESCRIPTION

The calc command is an interactive arbitrary precision calculator. It allows users to perform complex mathematical calculations with a higher degree of accuracy than standard calculators or scripting languages. It supports a wide range of mathematical functions, including trigonometric functions, logarithms, exponential functions, and user-defined functions.

calc's primary advantage lies in its ability to handle numbers with a very large number of digits, overcoming the limitations of floating-point arithmetic. Users can set the desired precision (number of significant digits) to meet specific requirements. It allows you to perform complex arithmetic without the risk of integer overflow issues.

The calculator operates in interactive mode, where users enter expressions, and the results are displayed immediately. Additionally, calc can be used in batch mode, reading commands from a file. It is suitable for scenarios where precision is paramount, such as scientific research, financial modeling, or cryptographic operations.

CAVEATS

Error messages are displayed using stderr, not stdout.
Complex numbers and matrix operations are not supported by default.

FUNCTIONS

calc supports many built-in functions. See manual page for details.
Examples: sin(x), cos(x), log(x), exp(x), sqrt(x), etc.
Users can also define their own functions.

VARIABLES

Variables can be used to store intermediate results for later use.
Example: x = 10; y = x * 2; print y;

INTERACTIVE MODE

In interactive mode, calc displays a prompt where you can enter expressions. The results are printed after each evaluation. To exit interactive mode, type 'quit' or 'exit'.

HISTORY

calc has evolved over time as a powerful arbitrary precision calculator, with updates including bug fixes, feature enhancements (like trigonometric functions, logarithms, and user-defined functions) and optimization in arithmetic calculation. The project aims to deliver a practical CLI tool to perform arithmetics in a controlled manner.

SEE ALSO

bc(1), dc(1), awk(1)

Copied to clipboard