CALC

Section: (1L)
Updated: 24 February 1988
Index Return to Main Contents

 

NAME

calc - a command line calculator

 

SYNOPSIS

calc [ -d -h -o -b -@ -pn -e - ] [ expression ]

 

DESCRIPTION

calc evaluates mathematical expressions.

An expression is any decimal number in integer or real format. It may also be a binary, octal or hexidecimal integer prefixed by, respectively, b, o, or h, or an ASCII character, prefixed by @.

An expression is also any expression preceded by one of the following:

          sin       log       - (unary minus)
          cos       ln        + (unary plus)
          tan       sqrt      ~ (one's complement)
          atan      exp

or any two expressions separated by one of the following operators:

Exponentiation: : Multiplication: * x X (none: implied multiplication) Division: / Modulo: % Addition: + Subtraction: - Left shift: < Right shift: > Bitwise AND: & Bitwise XOR: ^ BitWise OR: |

An expression is any expression preceded by an opening delimeter:

          {    [    (

and followed by a closing delimeter:

          }    ]    )

The special symbols "pi" and "PI" are also valid expressions. So is "prev" which returns the value of the previous equation (multiple-expression mode only).

If the expression is omitted from the command line, then the program will be in multiple-expression mode and repeatedly read expressions from the standard input until it encounters an empty line or an end of file.

 

OPTIONS

-d
(The default mode.) The answer is printed as a decimal number, or in scientific format if it is very large.
-o
The answer is printed in octal, rounded down to the nearest integer.
-h
The answer is printed in hexadecimal, rounded down to the nearest integer.
-b
The answer is printed in binary, rounded down to the nearest integer.
-@
The answer is printed as an ASCII character (modulo 128).
-pn
Only meaningful in decimal and binary mode. Specifies the precision (the number of digits appearing after the decimal point) for a decimal number, or the number of minimum number of digits appearing in a binary number. (In order to line up the columns when multiple calculations are being performed.) There must not be a space between the ``p'' and n.
-e
Will cause calc to exit when a bad expression is entered. This is only meaningful in multiple-expression mode.
-
Indicates the end of the argument list. This is used when the beginning of the expression might accidently be interpreted as an option. (See EXAMPLES below.)

 

USAGE NOTES

Arguments to trig functions are specified in degrees.

All binary operators group left-to-right, unary operators and functions group right-to-left. Priority of functions and operators is almost identical to that of C (except modulo is slightly lower here):

Highest:       Unary Op's        Functions
               Exponentiation
               Multiplication    Division
               Modulo
               Addition          Subtraction
               Left shift        Right shift
               And
               Xor
Lowest:        Or

The C shell use many of calc's symbols for its own evil purposes. These include all three pairs of delimeters, and the asterisk. Whenever you need a delimeter, you are advised to enclose the entire expression in double quotes to keep the shell from messing with it. Alternatively, you can omit the expression from the command line, and have calc prompt you for it, in which case the shell will never see what is typed.

Computations are performed using double precision floating point numbers with the following exceptions: The modulo operation (%) rounds its arguments to the nearest integer. Bitwise and bit shift operations expect (or will truncate to) positive integers. Hex and octal modes expect (or will truncate to) the nearest integer. When an integer is expected, it must be less than the largest long integer allowed on the machine (typically, 2:31). You will get undefined results if you go sticking large numbers where they don't belong.

 

EXAMPLES

calc  2 + 5 x 6
      32


calc '(2 + 5) x 6'
Parens quoted to hide them from the shell.
42 calc -p2 "atan(tan(45))" 45.00 calc -h 20 h14 calc -o @A o101
calc -b 2:8 + 3
Exponentiation takes precidence over addition.
b100000011 calc b101 - hc -7
calc - -h4ff
``-'' used so ``-h'' won't be interpreted as an option
-1279

 

DIAGNOSTICS

calc prints its error messages on the standard output. Normally, when an error occurs calc terminates with an exit status of 1. The exception is when in multiple-expression mode if the e option has not been specified. In this case, calc will simply report the error and move on to the next input.

The error messages are:

bad argument to -p option
Option must be followed by an integer argument with no intervening spaces.
missing expression
An operator of function expected an expression and didn't find one.
division by zero
Right-hand expression to the division operator evaluated to zero.
bad argument to modulo
Right-hand expression to the modulo operator was zero, or was greater than or equal to 2:31.
illegal expression
An expression couldn't be parsed.
unmatched open paren
A parenthesized expression was opened, but never closed.
bad binary digit
An expression preceded by a ``b'' contained a character other than ``1'' and ''0''.

 

BUGS

Ascii format (-@, and @) doesn't do anything to pretty up control characters. For example, "calc -@ 12" may have a disconcerting result.

 

AUTHOR

Wayne Mesard, MESARD@BBN.COM


 

Index

NAME
SYNOPSIS
DESCRIPTION
OPTIONS
USAGE NOTES
EXAMPLES
DIAGNOSTICS
BUGS
AUTHOR

This document was created by man2html, using the manual pages.
Time: 21:51:43 GMT, February 02, 2023