[<--] [Cover] [Table of Contents] [Concept Index] [Program Index] [-->]
Google
 
Web dsl.org


Mathematics

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.

Calculating Arithmetic

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).

Making a Quick Arithmetic Calculation

WWW: http://dsl.org/comp/tinyutils/


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).

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 bc.

Making Many Arithmetic Calculations

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 calculator.

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.

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 bc.

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.

The following table describes the symbols you can use to specify mathematical operations.
SYMBOL OPERATION
expression + expression Add: output the sum of the two expressions.
expression - expression Subtract: output the difference of the two expressions.
expression * expression Multiply: output the product of the two expressions.
expression / expression Divide: output the quotient of the two expressions.
expression % expression Remainder: output the remainder resulting by dividing the two expressions.
expression ^ expression Power: raise the first expression to the power of the second expression.
(expressions) Group an expression or expressions together, altering the standard precedence of performing operations.
sqrt(expression) Output the square root of expression.

Outputting a Random Number

WWW: http://dsl.org/comp/tinyutils/


To output a random number, use random. Give as an argument an integer denoting the range of numbers to be output; random then outputs a random number from 0 to the number you give, minus one.

Listing a Sequence of Numbers

Use 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.

To output the sequence from any one number to another, give those numbers as arguments.

To specify an increment other than one, give it as the second argument, between the starting and ending number.

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.

To pass a sequence of numbers as arguments to a command, pipe the output of seq using a space character as a separator.

Finding Prime Factors

The factor tool calculates and outputs the prime factors of numbers passed as arguments.

NOTE: If no number is given, factor reads numbers from standard input; numbers should be separated by space, tab, or newline characters.

Converting Numbers

The following recipes are for converting numbers in various ways.

Converting an Amount between Units of Measurement

Debian: `units'
WWW: http://www.gnu.org/software/units/units.html


Use the 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.

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.

The 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.

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.

Converting an Arabic Numeral to English

Debian: `bsdgames'


Use 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.

Other Math Tools

The following table lists some of the other mathematics tools available for Linux. It is by no means a complete list.
TOOL DESCRIPTION
calc calc is a scientific calculator tool for Emacs.
Debian: `calc'
dc Like bc, the dc tool is an arbitrary-precision calculator language, but it is a reverse-polish calculator, where numbers are pushed on a stack. When you give an arithmetic operation symbol, dc pops numbers off the stack for their operands, and then it pushes the evaluation on the stack.
Debian: `dc'
dome Richard J. Bono's dome is a geodesic math tool for calculating the properties of a geodesic dome symmetry triangle -- it can calculate chord factors, vertex coordinates, and topological abundance of various dome types, including "Buckyball" formations and elliptical geodesics.
Debian: `dome'
WWW: http://www.cris.com/~rjbono/html/domes.html
gnucash 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.
Debian: `gnucash'
WWW: http://www.gnucash.org/
gnumeric Gnumeric is the GNOME spreadsheet application. It is powerful, and somewhat reminiscent of Excel.
Debian: `gnumeric'
WWW: http://www.gnu.org/software/gnumeric/gnumeric.html
gnuplot The gnuplot tool can be used for data visualization, making 2-D and 3-D graphs, and plotting functions.
Debian: `gnuplot'
WWW: ftp://ftp.gnu.org/pub/gnu/gnuplot/
oleo 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 output.
Debian: `oleo'
WWW: http://www.gnu.org/software/oleo/oleo.html
sc sc is a small spreadsheet tool that runs in the console; it provides formulas and other basic features you would expect from a minimal spreadsheet.
xcalc xcalc is a visual scientific calculator for the X Window System -- it draws a calculator on the screen, and you can use the mouse or keyboard to use it. It is capable of emulating the TI-30 and HP-10C calculators.
xspread xspread is the X client front-end to sc.
Debian: `xspread'


[<--] [Cover] [Table of Contents] [Concept Index] [Program Index] [-->]