|[<--] [Cover] [Table of Contents] [Concept Index] [Program Index] [-->]|
Tools and techniques for dealing with numbers are the subject of this chapter: listing them in sequence or randomly, calculating arithmetic, and converting between units. Larger applications, such as spreadsheets and plotting tools, are also mentioned.
As you might expect, there are many tools for making arithmetic calculations in Linux. The following recipes describe how to use two of them for two common scenarios; a list of other calculator tools, including a visual calculator, appears at the end of this chapter (see Other Math Tools).
To do a quick calculation that requires only addition, subtraction, multiplication, or division, use
calc. It takes as an argument a
simple mathematical expression, and it outputs the answer.
Use `*' for a multiplication sign and `/' for division; to output the remainder, use `%'. You can use parenthesis to group expressions -- but when you do, be sure to quote them (see Passing Special Characters to Commands).
$ calc 50*10 [RET] 500 $
$ calc '100*(4+420)' [RET] 42400 $
$ calc 10%3 [RET] 1 $
NOTE: This tool is useful for quickly computing a simple
arithmetic equation, but it has several drawbacks: it only outputs whole
integers, its operators are limited, and complex expressions must be
quoted. For doing anything more than the simplest operations, see the
next recipe, which describes
Debian: `bc' WWW: ftp://src.doc.ic.ac.uk:/pub/gnu/bc-1.05a.tar.gz
When you have a lot of calculations to make, or when you must compute numbers with decimals, use
bc, a calculation language that
supports arbitrary precision numbers. Type bc to perform
arithmetic operations interactively, just like you would with a
Type each statement to evaluate on a line by itself, typing [RET] at
the end the statement; the evaluation of what you type is output on the
following line. Each line you type will be evaluated by
bc as an
arithmetic expression. To exit, type quit on a line by itself.
$ bc [RET] bc 1.05 Copyright 1991, 1992, 1993, 1994, 1997, 1998 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 42 * 17 [RET] 714 quit [RET] $
In this example,
bc output its version number and warranty
information when it started; then, the statement 42 * 17 was typed
by the user,
bc output the result (`714'), and then the
user typed quit to exit
By default, digits to the right of the decimal point are truncated from
the output -- so dividing 10 by 3 would output `3' as a result, and
outputting the remainder from this operation by typing 10%3 would
output a `1'. However,
bc is an arbitrary precision
calculator, and you can give the number of digits to use after the
decimal point by specifying the value of the
scale variable; its
default value is 0.
bcto compute the result of 10 divided by 3, using 20 digits after the decimal point, type:
$ bc [RET] bc 1.05 Copyright 1991, 1992, 1993, 1994, 1997, 1998 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. scale=20 [RET] 10 / 3 [RET] 3.33333333333333333333 quit [RET] $
The following table describes the symbols you can use to specify mathematical operations.
||Add: output the sum of the two expressions.|
||Subtract: output the difference of the two expressions.|
||Multiply: output the product of the two expressions.|
||Divide: output the quotient of the two expressions.|
||Remainder: output the remainder resulting by dividing the two expressions.|
||Power: raise the first expression to the power of the second expression.|
||Group an expression or expressions together, altering the standard precedence of performing operations.|
||Output the square root of expression.|
To output a random number, use
random. Give as an argument an
integer denoting the range of numbers to be output;
outputs a random number from 0 to the number you give, minus one.
$ random 10 [RET]
seq to print a sequence of numbers. This is very useful for
getting a listing of numbers to use as arguments, or otherwise passing
sequences of numbers to other commands.
To output the sequence from 1 to any number, give that number as an argument.
$ seq 7 [RET]
$ seq -7 [RET]
To output the sequence from any one number to another, give those numbers as arguments.
$ seq 9 0 [RET]
$ seq -1 -20 [RET]
To specify an increment other than one, give it as the second argument, between the starting and ending number.
$ seq -1 3 14 [RET]
Use the `-w' option to pad numbers with leading zeros so that they're all output with the same width.
Specify a separator string to be output between numbers as an argument to the `-s' option; the default is a newline character, which outputs each number in the sequence on its own line.
$ seq -w 9 23 999 [RET]
$ seq -s ' ' 1 23 [RET]
To pass a sequence of numbers as arguments to a command, pipe the output
seq using a space character as a separator.
$ cat `seq -s " " 25 75` > selected-mail [RET]
factor tool calculates and outputs the prime factors of
numbers passed as arguments.
$ factor 2000 [RET] 2000: 2 2 2 2 5 5 5 $
NOTE: If no number is given,
factor reads numbers from
standard input; numbers should be separated by space, tab, or newline
The following recipes are for converting numbers in various ways.
Debian: `units' WWW: http://www.gnu.org/software/units/units.html
units tool to convert units of measurement between
scales. Give two quoted arguments: the number and name of the units you
have, and the name of the units to convert to. It outputs two values:
the number of the second units you have, and how many of the second kind
of unit can make up the quantity of the first that you've specified.
$ units '50 grams' 'ounces' [RET] * 1.7636981 / 0.56699046 $
In this example, the output indicates that there are about 1.7636981 ounces in 50 grams, and that conversely, one ounce is about 0.56699046 times 50 grams.
units tool understands a great many different kinds of
units -- from Celsius and Fahrenheit to pounds, hectares, the speed of
light, and a "baker's dozen." All understood units are kept in a text
file database; use the `-V' option to output the location of this
database on your system, which you can then peruse or search through to
see the units your version supports.
$ units -V [RET] units version 1.55 with readline, units database in /usr/share/misc/units.dat $
In this example, the
units database is located in the file
`/usr/share/misc/units.dat', which is the file to peruse to list
all of the units data.
number to convert Arabic numerals to English text. Give a
numeral as an argument; with no argument,
number reads a numeral
from the standard input.
$ number 100000 [RET]
The following table lists some of the other mathematics tools available for Linux. It is by no means a complete list.
|| Like |
|| Richard J. Bono's |
|| GnuCash is an intuitive personal finance application. Use it for
managing finances, including bank accounts, stocks, income, and
expenses; it's "based on professional accounting principles" to ensure
accuracy in computation and reporting.
|| Gnumeric is the GNOME spreadsheet application. It is powerful, and
somewhat reminiscent of Excel.
|| The |
|| GNU Oleo is a spreadsheet application. It can run in both X and in
the console, has Emacs-like key bindings, and can generate PostScript
[<--] [Cover] [Table of Contents] [Concept Index] [Program Index] [-->]