Nash - Scientific Computing with PCs (523165), страница 29
Текст из файла (страница 29)
Usually, we content ourselveswith some specific tests near commonly troublesome arguments. Cody and Waite (1980) give some otherexamples and produced the ELEFUNT set of FORTRAN tests.•For trigonometric functions, evaluation is usually accomplished by rational approximation (a ratio oftwo power series) using an argument reduced to a small range. The range reduction is performedusing trigonometric identities such as(9.8.1)Clearly, if(9.8.2)sin (x + 2*n*π) = sin (x)wheren=1, 2, . . .,n is very large, the representation of the number(x + 2*n*π)is the same for many different values of x because of digit loss on rounding. That is, if we have twonumbers x and y that give different values for sin(x) and sin(y), there is not necessarily any differencebetween the machine-stored numbers(9.8.3)(x + 2*π*n) and (y + 2*π*n)nor, obviously, between their trigonometric functions. Another test is to add or subtract one bit at a timeto the representation of π and examine the sine, cosine or tangent functions for correct values.•The logarithm function is often poorly approximated near 1.0.
It is quite easy to evaluate an2approximation of log (1 + x) using the McLaurin series for x < 1 (or some more economical one)(9.8.4)log(1 + x) = x - (1/2)x 2 + (1/3)x 3 - (1/4)x 4 + . . .This can be compared against the approximation produced by LOG( ) or LN( ) in the programminglanguage.•yThe power function x is frequently abused by programmers to form simple integer powers ofnumbers such as squares and cubes. These are usually more safely and rapidly calculated bymultiplication. The power function should really be reserved for powers that are not integer orinteger/2 (i.e., using square root). Language processors may treat all negative arguments y as invalid,even when they are mathematically admissible.
Users should check these cases as needed. It has been9: DEBUGGING83noted that some versions of the popular spreadsheet Lotus 1-2-3 have an error in the power function(Nash, 1989b). The errors can be brought to light by computing very small powers of numbersbetween 1 and 2.Comparisons: Comparisons are made to decide which control path to follow in a program (IF statements).It has become part of the folklore of computing that comparisons for equality between floating-point(REAL) numbers should not be made, because of the rounding errors made in a sequence of computations.However, a statement such as(9.8.5)if ( (abs(x) + 10.0) = 10.0 ) do ...should work as a test forexample in PASCAL with:x being zero relative to 10.0. Clearly one can check if the test works, forx:=1.0; i:=1;repeatx:=x/10.0; writeln(i,’ ’,x); i:=i+1;until ((abs(x) + 10.0) = 10.0) ;Compiler writers attempt to optimize execution by removing common elements on both sides of a test.Here such an alteration in the instructions is a disaster.
Such a failure occurred in a program we werehelping to prepare with the Microsoft C compiler (Version 6.0 for MS-DOS) that effectively removed the"10.0" on either side of a test similar to that in (9.8.5) above, although such optimization was supposedly"turned off". Our code failed because iterations never terminated.Tests for "smallness" as above are important in termination criteria in iterative algorithms (Nash J C,1987d; Nash J C and Walker-Smith, 1989b). Note that we avoid the term "convergence tests". Convergenceis a property of an algorithm, that is, of the mathematical process under study.
Termination, on the otherhand, could involve checks on false inputs, hardware or software failure, user or other interruption ofexecution, file or storage capacity exceeded, and many other factors beside the mathematical steps thatmake up the iteration.A rarer complication is the use of a "fuzzy" comparison.
Floating-point numbers within some FUZZ aretreated as equal in such comparisons. The Tektronix 4051 BASIC had such a feature (Nash, 1978b). Ouropinion is that such "features" are offensive.Data structures: Sometimes restrictions exist on the size of declared arrays or strings, or on the numberof dimensions allowed. It is relatively easy to test these possibilities, as we have done in Section 7.1. Oftenvariable dimensions are not allowed, for example, in BASIC10 INPUT "NUMBER OF ROWS IN MATRIX=";M20 INPUT "NUMBER OF COLUMS = ";N30 DIM A(M,N)Loop control: A common cause of confusion for students of programming arises in the study of loopingmechanisms. We need to be sure whether loop control calculations are carried out at the beginning or theend of the loop.
It is easy to devise short programs that verify loop operation by printing out the loopcontrol variable each time the loop is executed. We also note that there may be restrictions on the typesof variables that can be used in loop control, so that apparently trivial changes in declarations in one partof a program may have untoward consequences elsewhere. An example concerns Turbo Pascal, wheretype longint (long integer) is not permitted in for loops.Previous Home Next84Copyright © 1984, 1994 J C & M M NashNash Information Services Inc., 1975 Bel Air Drive, Ottawa, ON K2C 0X1 CanadaSCIENTIFIC COMPUTING WITH PCsCopy for:Dr.
Dobb’s JournalChapter 10Utilities — a desirable set10.110.210.310.410.510.610.7EditorsFile view, move, copy, archive and backupFile display, conversion and printFile comparisonDirectories and catalogsFixup programsSortingWe have mentioned utility programs often in this book. Now we suggest the contents of a useful set.While some utility functions come with operating system software, they may be incomplete or poorlysuited to our needs. The acquisition and tailoring of utilities to our needs take time and effort. When weneed a utility program to use the computer, we will spend effort or money to acquire it.
When a utilityimproves our ability to use the PC, we must decide if the new program is worth obtaining and installing.10.1 EditorsEditors are probably the most used programs on PCs if we include word processing software under the"editor" heading. Editors are important in computing applications: programs need changing, data mustbe altered and output requires formatting for presentation (as in this book).Every editor has roughly similar functions. The differences lie in the ways we control them and in specialfeatures.
The main functions of an editor are to allow data to be added, changed or deleted. Other featuresinclude copying or moving data, assembling or breaking up files, or performing certain translations ordisplay functions so that we can note the presence of control characters not normally printable. Becauseof both similarities and differences, those who must use more than two or three editors soon findthemselves giving the wrong commands.
It is helpful if we can use just one editor.This goal may be frustrated by the ways in which data is stored. Programs may use tokenized keywordsor include formatting information for greater readability. With increasing memory capacities, the incentivefor tokenizing source code has declined, but recent versions of the Microsoft BASIC interpreters for MSDOS PCs still default to a tokenized form of storage. We can force the SAVE command to store ourprogram in text (ASCII) form by using the syntaxSAVE "PROG",ATo reduce editing problems, we recommend that program source codes always be saved in text modewherever possible.Desirable features of a text editor are simplicity, power and flexibility.
Unfortunately most products onthe market today fail to meet one or more of these desiderata. Too many special features clutter manualsand render an editor awkward to learn. Which editor one uses may be a matter of taste. Some users swearby reliable but pedestrian line-oriented editors, but most editors now use full screen capabilities, wheretext displayed on the screen is modified before our eyes.Note that word-processing editors often present the user with a picture of the (probable) form of theprinted documents. Useful for producing attractive text, such "what you see is what you get" (WYSIWYG)programs can be a great nuisance if we wish to adjust the exact content of a file, since they may10: UTILITIES - A DESIRABLE SET85automatically insert codes for spacing, tabs, underlining, etc. Most word processors with retrieve and savesimple text, but default storage modes generally include formatting codes.Many screen.oriented editors present the user with a lot of information at once.
Besides the text, there maybe tab positions, ruler, current page size, cursor position, file in use, mode of operation and other details.If, like ourselves and Jerry Pournelle (1983) you only want to see your own text on the screen, this canbe most annoying.Many programming environments (e.g., Turbo PASCAL, True BASIC, Microsoft Quick BASIC and QuickC) provide their own editor. Don’t expect that it will have the familiar commands of your favorite texteditor. Some programming editors, e.g., BRIEF, allow the user to edit, compile and run programs easilyin various programming languages from a single, powerful editing environment.