Библиотека сайта ruslinux.net
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29. 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.
29.1 Calculating Arithmetic Calculating arithmetic. 29.2 Outputting a Random Number Output a random number. 29.3 Listing a Sequence of Numbers Listing a sequence of numbers. 29.4 Finding Prime Factors Getting prime factors. 29.5 Converting Numbers Converting between units of scale. 29.6 Other Math Tools Other math tools.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.1 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 section Other Math Tools).
29.1.1 Making a Quick Arithmetic Calculation Quick math on the input line. 29.1.2 Making Many Arithmetic Calculations A commandline calculator.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.1.1 Making a Quick Arithmetic Calculation
@sf{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 expressionsbut when you do, be sure to quote them (see section Passing Special Characters to Commands).

To output the result of 50 times 10, type:
$ calc 50*10 RET 500 $

To output the result of 100 times the sum of 4 plus 420, type:
$ calc '100*(4+420)' RET 42400 $

To output the remainder of 10 divided by 3, type:
$ 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 bc
.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.1.2 Making Many Arithmetic Calculations
@sf{Debian}: `bc'
@sf{WWW}: ftp://src.doc.ic.ac.uk:/pub/gnu/bc1.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.

To multiply 42 and 17, 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'. 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 bc
.
By default, digits to the right of the decimal point are truncated from
the outputso 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.

To use
bc
to 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.
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. 
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.2 Outputting a Random Number
@sf{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.

To output a random number from 0 to 9, type:
$ random 10 RET
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.3 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 of numbers from one to seven, type:
$ seq 7 RET

To output the sequence of numbers from one to negative seven, type:
$ seq 7 RET
To output the sequence from any one number to another, give those numbers as arguments.

To output the sequence of numbers from nine to zero, type:
$ seq 9 0 RET

To output the sequence of numbers from negative one to negative twenty, type:
$ seq 1 20 RET
To specify an increment other than one, give it as the second argument, between the starting and ending number.

To output the sequence of numbers from 1 to 14, incrementing by 3,
type:
$ 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.

To output the sequence of numbers from 9 to 999, incrementing by 23,
with numbers padded with zeros so that they're all of equal width, type:
$ seq w 9 23 999 RET

To output the sequence of numbers from 1 to 23, with a space character
between each, type:
$ seq s ' ' 1 23 RET
To pass a sequence of numbers as arguments to a command, pipe the output
of seq
using a space character as a separator.

To concatenate all the files in the current directory, whose names are
numbers from 25 through 75, into a new file called
`selectedmail'
, type:$ cat `seq s " " 25 75` > selectedmail RET
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.4 Finding Prime Factors
The factor
tool calculates and outputs the prime factors of
numbers passed as arguments.

To output the prime factors of 2000, type:
$ 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
characters.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.5 Converting Numbers
The following recipes are for converting numbers in various ways.
29.5.1 Converting an Amount between Units of Measurement Converting units of measurement. 29.5.2 Converting an Arabic Numeral to English Converting Arabic numerals to text.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.5.1 Converting an Amount between Units of Measurement
@sf{Debian}: `units'
@sf{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.

To output the number of ounces in 50 grams, type:
$ 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.
The units
tool understands a great many different kinds of
unitsfrom 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.

To determine the location of the
units
database, type:$ 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.
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.5.2 Converting an Arabic Numeral to English
@sf{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.

To output the English text equivalent of 100,000, type:
$ number 100000 RET
[ < ]  [ > ]  [ << ]  [ Up ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
29.6 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.
{@sf{Debian}}: `calc'

dc 
Like bc , the dc tool is an arbitraryprecision
calculator language, but it is a reversepolish 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.
{@sf{Debian}}: `dc'

dome 
Richard J. Bono's dome is a geodesic math tool for
calculating the properties of a geodesic dome symmetry triangleit can
calculate chord factors, vertex coordinates, and topological abundance
of various dome types, including "Buckyball" formations and elliptical
geodesics.
{@sf{Debian}}: `dome'
{@sf{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.
{@sf{Debian}}: `gnucash'
{@sf{WWW}}: http://www.gnucash.org/

gnumeric 
Gnumeric is the GNOME spreadsheet application. It is powerful, and
somewhat reminiscent of Excel.
{@sf{Debian}}: `gnumeric'
{@sf{WWW}}: http://www.gnu.org/software/gnumeric/gnumeric.html

gnuplot 
The gnuplot tool can be used for data visualization, making
2D and 3D graphs, and plotting functions.
{@sf{Debian}}: `gnuplot'
{@sf{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 Emacslike key bindings, and can generate PostScript
output.
{@sf{Debian}}: `oleo'
{@sf{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
Systemit draws a calculator on the screen, and you can use the mouse
or keyboard to use it. It is capable of emulating the TI30 and HP10C
calculators.

xspread 
xspread is the X client frontend to sc .
{@sf{Debian}}: `xspread'

[ << ]  [ >> ]  [Top]  [Contents]  [Index]  [ ? ] 
Только зарегистрированные пользователи могут оценивать и комментировать статьи.