-->

Previous | Table of Contents | Next

Page 259

Hour 17

Learning Math and Financial
Tools

This lesson is an introduction to some of the mathematical and financial tools available for Linux. This hour shows you calculators, spreadsheets, and graphic modeling programs, and points you to sources where you can find even more programs. Whether you're only interested in setting up simple single-screen spreadsheets, or would rather plot detailed maps using 200 megabytes of cartographic data from the U.S. Geologic Survey, you'll find Linux tools to help you get started.

The hour begins with a discussion of some calculators and calculating languages, and introduces you to just some of the Linux spreadsheet programs, followed by a discussion of modeling programs, such as gnuplot. This hour can't cover all the of the more than 1,500 scientific applications for Linux, but if you have an interest in other fields such as artificial intelligence, astronomy, biology, chemistry, database systems, electronics, linear algebra, physics, or raytracing, you can find tools for Linux to help you.

Page 260

Calculators

This section introduces you to several Linux calculators. You'll find some of these handy when you pay bills, cook, or even travel. Some of these calculators work from the command line, and others run under the X Window System.

Doing Desk Calculations with the dc Command

The dc (desk calculator) command is a command-line calculator that uses reverse Polish notation, or RPN, to perform calculations, and has more than 30 different operators and internal commands.

The dc command, found under the /usr/bin directory, is easy to use:


# dc

44

55

+

p

99

q

The example shows that to add two numbers, you first enter the numbers, then enter the operator, then use the p command to print the value placed on the stack by the addition operator. The q command quits the dc program. This method of performing calculations is not as inconvenient as you may think. For example, suppose that you're going through the checks you've written during the month, and you want to check your written calculations. Using the dc command, you can enter


# dc

2500.00

49.95

-

p

2450.05

32.18

-

p

2417.87

q

You started by entering a $2500.00 balance, then entered $49.95 as the first check, followed by the subtraction operator, or minus sign. The p command prints the result, and the next current balance of $2450.05 is maintained on the stack. You can also use the dc command to read files of calculations instead of typing commands at your terminal, and it has 256 different registers, or temporary storage areas for your calculations. See the dc command's manual page for more information and other features.

Page 261

Calculating with the X11 xcalc Client

The xcalc client is one of the more familiar graphic calculators (see Figure 17.1), and comes with the XFree86 X Window System. The xcalc command, found under the /usr/X11R6/bin directory, has only two command-line options, -stipple, and -rpn. The -stipple option merely colors the background of xcalc's face, whereas the -rpn option tells the xcalc command to use rpn for doing calculations, and changes its appearance.

You must be running X11 in order to use the xcalc command. You can use your mouse or the keyboard to enter numbers and perform calculations. To use it in its normal mode with the -stipple option, type


# xcalc -stipple &

Figure 17.1.
The xcalc calculator.

To use the xcalc command as an rpn calculator, type


# xcalc -rpn -stipple &

You can customize nearly any aspect of xcalc by editing its defaults file, Xcalc, which is found under the /usr/X11R6/lib/X11/app-defaults directory. See the xcalc manual page for more information.

Three Calculators in One: dtcalc

The dtcalc calculator, part of Red Hat Software, Inc.'s Common Desktop Environment, or CDE (see Hour 8, "Exploring Other X11 Window Managers"), offers a variety of features

Page 262

and can emulate a logical, scientific, or financial calculator. Although dtcalc does not have as much register storage as the dc command, you can use your mouse or keyboard to enter data, just as you can with xcalc. Figure 17.2 shows the dtcalc calculator.

Figure 17.2.
The dtcalc calculator,
part of the Common
Desktop Environment
for the X Window
System, can emulate
three different calcula-
tors.

You can perform calculations in binary (base 2), octal (base 8), decimal, and hexadecimal (base 16). You can also define your own functions and constants. Like most other CDE applications, dtcalc has extensive and context-sensitive help. For more information about CDE, see Hour 6, "Using the Shell."

Performing Unit Conversions with the units Command

If you've ever had trouble remembering the formulas to convert miles to meters, or cups to gallons, you'll really like Adrian Mariano's units command, which you'll find under the /usr/bin directory. Want to know how many furlongs per mile? How about how many acres in a square mile?

One way to use the units command is from the command line, for example:


# units floz gallon

        * 0.0078125

        / 128

This simple example shows how to find out how many fluid ounces are in a gallon. You see that there are 128, and that a fluid ounce is less than one-hundredth of a gallon. Although using the command line is handy for quick conversions, you can also run a series of queries as follows:


# units

Page 263


501 units, 41 prefixes



You have: mile*mile

You want: acre

        * 640

        / 0.0015625

You have: mile2

You want: acre

        * 640

        / 0.0015625

You have: mile^2

You want: acre

        * 640

        / 0.0015625

You have: mile

You want: furlong

        * 8

        / 0.125

You have: 100 fathoms

You want: feet

        * 600

        / 0.0016666667

...

In the interactive mode, you can ask units for any number of conversions. The units command works by reading its library of conversions from the file units.lib under the /usr/lib directory. As you can see, you can use different notations to indicate amounts to be converted. Another interesting feature is that units can also perform currency conversions, for example:


# units dollar yen

        * 107.52688

        / 0.0093

Note that this may not be entirely true, as currency values change daily. You can edit the units.lib file and insert not only current currency values, but also prices for gold, silver, platinum, or pork bellies. See the units manual page for more information.

Programming Calculators with the bc Language Interpreter

The bc command is an interpreter for a calculator language. You can use this command, by Philip Nelson, to write calculator programs while bc is running, or have bc run the program after it starts. The bc language has nearly 40 operators, functions, and programming logic keywords. Although this section doesn't go into how to program in bc, it shows you how a simple checkbook balancing program (from bc's manual page) can work like the earlier example for the dc command. If you'd like to try this program, see the bc manual page, then type the program into a file using your favorite text editor. You can run it with


# bc nameofyourfile

The bc command will start by reading in its program, and present the following:

Previous | Table of Contents | Next