1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
|
@c $Id: mathematics.texinfo,v 1.18 2002/01/09 16:59:51 m Exp m $
@node Mathematics, , Reminders, Productivity
@comment node-name, next, previous, up
@chapter Mathematics
@cindex mathematics
@noindent
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.
@menu
* Arithmetic:: Calculating arithmetic.
* Random Numbers:: Output a random number.
* Number Sequence:: Listing a sequence of numbers.
* Prime Factors:: Getting prime factors.
* Converting Numbers:: Converting between units of scale.
* Math Tools:: Other math tools.
@end menu
@node Arithmetic, Random Numbers, Mathematics, Mathematics
@comment node-name, next, previous, up
@section Calculating Arithmetic
@cindex calculating arithmetic
@cindex arithmetic, calculating
@noindent
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
(@pxref{Math Tools, , Other Math Tools}).
@menu
* Quick Math:: Quick math on the input line.
* Bc:: A command-line calculator.
@end menu
@node Quick Math, Bc, Arithmetic, Arithmetic
@comment node-name, next, previous, up
@subsection Making a Quick Arithmetic Calculation
@cindex making a quick arithmetic calculation
@cindex arithmetic calculation, making a quick
@pindex calc
@flushleft
@sf{WWW}: @url{http://dsl.org/comp/tinyutils/}
@end flushleft
@*
@noindent
To do a quick calculation that requires only addition, subtraction,
multiplication, or division, use @code{calc}. It takes as an argument a
simple mathematical expression, and it outputs the answer.
Use @samp{*} for a multiplication sign and @samp{/} for division; to
output the remainder, use @samp{%}. You can use parenthesis to group
expressions---but when you do, be sure to quote them (@pxref{Quoting, ,
Passing Special Characters to Commands}).
@itemize @bullet
@item
To output the result of 50 times 10, type:
@example
@cartouche
$ @kbd{calc 50*10 @key{RET}}
500
$
@end cartouche
@end example
@item
To output the result of 100 times the sum of 4 plus 420, type:
@example
@cartouche
$ @kbd{calc '100*(4+420)' @key{RET}}
42400
$
@end cartouche
@end example
@item
To output the remainder of 10 divided by 3, type:
@example
@cartouche
$ @kbd{calc 10%3 @key{RET}}
1
$
@end cartouche
@end example
@end itemize
@sp .25
@noindent
@strong{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 @code{bc}.
@node Bc, , Quick Math, Arithmetic
@comment node-name, next, previous, up
@subsection Making Many Arithmetic Calculations
@cindex making many arithmetic calculations
@cindex arithmetic calculations, making many
@pindex bc
@flushleft
@sf{Debian}: @file{bc}
@sf{WWW}: @url{ftp://src.doc.ic.ac.uk:/pub/gnu/bc-1.05a.tar.gz}
@end flushleft
@*
@noindent
When you have a lot of calculations to make, or when you must compute
numbers with decimals, use @code{bc}, a calculation language that
supports arbitrary precision numbers. Type @kbd{bc} to perform
arithmetic operations interactively, just like you would with a
calculator.
Type each statement to evaluate on a line by itself, typing @key{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 @code{bc} as an
arithmetic expression. To exit, type @kbd{quit} on a line by itself.
@itemize @bullet
@item
To multiply 42 and 17, type:
@example
@cartouche
$ @kbd{bc @key{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'.
@kbd{42 * 17 @key{RET}}
714
@kbd{quit @key{RET}}
$
@end cartouche
@end example
@end itemize
In this example, @code{bc} output its version number and warranty
information when it started; then, the statement @kbd{42 * 17} was typed
by the user, @code{bc} output the result (@samp{714}), and then the
user typed @kbd{quit} to exit @code{bc}.
By default, digits to the right of the decimal point are truncated from
the output---so dividing 10 by 3 would output @samp{3} as a result, and
outputting the remainder from this operation by typing @kbd{10%3} would
output a @samp{1}. However, @code{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 @code{scale} variable; its
default value is 0.
@itemize @bullet
@item
To use @code{bc} to compute the result of 10 divided by 3, using 20
digits after the decimal point, type:
@*
@*
@*
@example
@cartouche
$ @kbd{bc @key{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'.
@kbd{scale=20 @key{RET}
10 / 3 @key{RET}}
3.33333333333333333333
@kbd{quit @key{RET}}
$
@end cartouche
@end example
@end itemize
The following table describes the symbols you can use to specify
mathematical operations.
@multitable @columnfractions .30 .70
@item @sc{Symbol}
@tab @sc{Operation}
@item @code{@var{expression} + @var{expression}}
@tab Add: output the sum of the two expressions.
@item @code{@var{expression} - @var{expression}}
@tab Subtract: output the difference of the two expressions.
@item @code{@var{expression} * @var{expression}}
@tab Multiply: output the product of the two expressions.
@item @code{@var{expression} / @var{expression}}
@tab Divide: output the quotient of the two expressions.
@item @code{@var{expression} % @var{expression}}
@tab Remainder: output the remainder resulting by dividing the two
expressions.
@item @code{@var{expression} ^ @var{expression}}
@tab Power: raise the first expression to the power of the second
expression.
@item @code{(@var{expressions})}
@tab Group an expression or expressions together, altering the standard
precedence of performing operations.
@item @code{sqrt(@var{expression})}
@tab Output the square root of @var{expression}.
@end multitable
@node Random Numbers, Number Sequence, Arithmetic, Mathematics
@comment node-name, next, previous, up
@section Outputting a Random Number
@cindex outputting a random number
@cindex random number, outputting a
@pindex random
@flushleft
@sf{WWW}: @url{http://dsl.org/comp/tinyutils/}
@end flushleft
@*
@noindent
To output a random number, use @code{random}. Give as an argument an
integer denoting the range of numbers to be output; @code{random} then
outputs a random number from 0 to the number you give, minus one.
@itemize @bullet
@item
To output a random number from 0 to 9, type:
@example
$ @kbd{random 10 @key{RET}}
@end example
@end itemize
@node Number Sequence, Prime Factors, Random Numbers, Mathematics
@comment node-name, next, previous, up
@section Listing a Sequence of Numbers
@cindex listing a sequence of numbers
@cindex numbers, listing a sequence of
@pindex seq
@pindex cat
@noindent
Use @code{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.
@itemize @bullet
@item
To output the sequence of numbers from one to seven, type:
@example
$ @kbd{seq 7 @key{RET}}
@end example
@item
To output the sequence of numbers from one to negative seven, type:
@example
$ @kbd{seq -7 @key{RET}}
@end example
@end itemize
To output the sequence from any one number to another, give those
numbers as arguments.
@itemize @bullet
@item
To output the sequence of numbers from nine to zero, type:
@example
$ @kbd{seq 9 0 @key{RET}}
@end example
@item
To output the sequence of numbers from negative one to negative twenty, type:
@example
$ @kbd{seq -1 -20 @key{RET}}
@end example
@end itemize
To specify an increment other than one, give it as the @emph{second}
argument, between the starting and ending number.
@itemize @bullet
@item
To output the sequence of numbers from -1 to 14, incrementing by 3,
type:
@example
$ @kbd{seq -1 3 14 @key{RET}}
@end example
@end itemize
Use the @samp{-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 @samp{-s} option; the default is a newline character, which
outputs each number in the sequence on its own line.
@itemize @bullet
@item
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:
@example
$ @kbd{seq -w 9 23 999 @key{RET}}
@end example
@item
To output the sequence of numbers from 1 to 23, with a space character
between each, type:
@example
$ @kbd{seq -s ' ' 1 23 @key{RET}}
@end example
@end itemize
To pass a sequence of numbers as arguments to a command, pipe the output
of @code{seq} using a space character as a separator.
@itemize @bullet
@item
To concatenate all the files in the current directory, whose names are
numbers from 25 through 75, into a new file called @file{selected-mail},
type:
@example
$ @kbd{cat `seq -s " " 25 75` > selected-mail @key{RET}}
@end example
@end itemize
@node Prime Factors, Converting Numbers, Number Sequence, Mathematics
@comment node-name, next, previous, up
@section Finding Prime Factors
@cindex finding prime factors
@cindex prime factors, finding
@pindex factor
@noindent
The @code{factor} tool calculates and outputs the prime factors of
numbers passed as arguments.
@itemize @bullet
@item
To output the prime factors of 2000, type:
@example
@cartouche
$ @kbd{factor 2000 @key{RET}}
2000: 2 2 2 2 5 5 5
$
@end cartouche
@end example
@end itemize
@sp .25
@noindent
@strong{NOTE:} If no number is given, @code{factor} reads numbers from
standard input; numbers should be separated by space, tab, or newline
characters.
@node Converting Numbers, Math Tools, Prime Factors, Mathematics
@comment node-name, next, previous, up
@section Converting Numbers
@cindex converting numbers
@cindex numbers, converting
@noindent
The following recipes are for converting numbers in various ways.
@menu
* Converting Units:: Converting units of measurement.
* Converting Arabic:: Converting Arabic numerals to text.
@end menu
@node Converting Units, Converting Arabic, Converting Numbers, Converting Numbers
@comment node-name, next, previous, up
@subsection Converting an Amount between Units of Measurement
@cindex converting an amount between units of measurement
@cindex measurement, converting an amount between units of
@cindex units of measurement, converting an amount between
@pindex units
@flushleft
@sf{Debian}: @file{units}
@sf{WWW}: @url{http://www.gnu.org/software/units/units.html}
@end flushleft
@*
@noindent
Use the @code{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.
@itemize @bullet
@item
To output the number of ounces in 50 grams, type:
@example
@cartouche
$ @kbd{units '50 grams' 'ounces' @key{RET}}
* 1.7636981
/ 0.56699046
$
@end cartouche
@end example
@end itemize
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 @code{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 @samp{-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.
@itemize @bullet
@item
To determine the location of the @code{units} database, type:
@example
@cartouche
$ @kbd{units -V @key{RET}}
units version 1.55 with readline, units database in
/usr/share/misc/units.dat
$
@end cartouche
@end example
@end itemize
In this example, the @code{units} database is located in the file
@file{/usr/share/misc/units.dat}, which is the file to peruse to list
all of the units data.
@node Converting Arabic, , Converting Units, Converting Numbers
@comment node-name, next, previous, up
@subsection Converting an Arabic Numeral to English
@cindex converting an Arabic numeral to English
@cindex numeral, converting an Arabic to English
@pindex number
@flushleft
@sf{Debian}: @file{bsdgames}
@end flushleft
@*
@noindent
Use @code{number} to convert Arabic numerals to English text. Give a
numeral as an argument; with no argument, @code{number} reads a numeral
from the standard input.
@itemize @bullet
@item
To output the English text equivalent of 100,000, type:
@example
$ @kbd{number 100000 @key{RET}}
@end example
@end itemize
@node Math Tools, , Converting Numbers, Mathematics
@comment node-name, next, previous, up
@section Other Math Tools
@cindex other math tools
@cindex math tools, other
@cindex GnuCash
@pindex dome
@pindex dc
@pindex calc
@pindex gnucash
@pindex xcalc
@noindent
The following table lists some of the other mathematics tools available
for Linux. It is by no means a complete list.
@multitable @columnfractions .30 .70
@item @sc{Tool}
@tab @sc{Description}
@item @code{calc}
@tab @code{calc} is a scientific calculator tool for Emacs.
@noindent
{@sf{Debian}}: @file{calc}
@item @code{dc}
@tab Like @code{bc}, the @code{dc} tool is an arbitrary-precision
calculator language, but it is a @dfn{reverse-polish} calculator, where
numbers are pushed on a stack. When you give an arithmetic operation
symbol, @code{dc} pops numbers off the stack for their operands, and
then it pushes the evaluation on the stack.
@noindent
{@sf{Debian}}: @file{dc}
@item @code{dome}
@tab Richard J. Bono's @code{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.
@noindent
{@sf{Debian}}: @file{dome}
@noindent
{@sf{WWW}}: @url{http://www.cris.com/~rjbono/html/domes.html}
@item @code{gnucash}
@tab 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.
@noindent
{@sf{Debian}}: @file{gnucash}
@noindent
{@sf{WWW}}: @url{http://www.gnucash.org/}
@item @code{gnumeric}
@tab Gnumeric is the GNOME spreadsheet application. It is powerful, and
somewhat reminiscent of Excel.
@noindent
{@sf{Debian}}: @file{gnumeric}
@noindent
{@sf{WWW}}: @url{http://www.gnu.org/software/gnumeric/gnumeric.html}
@item @code{gnuplot}
@tab The @code{gnuplot} tool can be used for data visualization, making
2-D and 3-D graphs, and plotting functions.
@noindent
{@sf{Debian}}: @file{gnuplot}
@noindent
{@sf{WWW}}: @url{ftp://ftp.gnu.org/pub/gnu/gnuplot/}
@item @code{oleo}
@tab 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.
@noindent
{@sf{Debian}}: @file{oleo}
@noindent
{@sf{WWW}}: @url{http://www.gnu.org/software/oleo/oleo.html}
@item @code{sc}
@tab @code{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.
@item @code{xcalc}
@tab @code{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.
@item @code{xspread}
@tab @code{xspread} is the X client front-end to @code{sc}.
@noindent
{@sf{Debian}}: @file{xspread}
@end multitable
|