17.11.2012 Views

Numerical recipes

Numerical recipes

Numerical recipes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1.2 Some C Conventions for Scientific Computing 25<br />

is immediately thrown away. A corollary of these rules is that all the real-number<br />

standard C library functions are of type double and compute to double precision.<br />

The justification for these conversion rules is, “well, there’s nothing wrong with<br />

a little extra precision,” and “this way the libraries need only one version of each<br />

function.” One does not need much experience in scientific computing to recognize<br />

that the implicit conversion rules are, in fact, sheer madness! In effect, they make it<br />

impossible to write efficient numerical programs. One of the cultural barriers that<br />

separates computer scientists from “regular” scientists and engineers is a differing<br />

point of view on whether a 30% or 50% loss of speed is worth worrying about. In<br />

many real-time or state-of-the-art scientific applications, such a loss is catastrophic.<br />

The practical scientist is trying to solve tomorrow’s problem with yesterday’s<br />

computer; the computer scientist, we think, often has it the other way around.<br />

The ANSI C standard happily does not allow implicit conversion for arithmetic<br />

operations, but it does require it for function arguments, unless the function is fully<br />

prototyped by an ANSI declaration as described earlier in this section. That is<br />

another reason for our being rigorous about using the ANSI prototype mechanism,<br />

and a good reason for you to use an ANSI-compatible compiler.<br />

Some older C compilers do provide an optional compilation mode in which<br />

the implicit conversion of float to double is suppressed. Use this if you can.<br />

In this book, when we write float, we mean float; when we write double,<br />

we mean double, i.e., there is a good algorithmic reason for having higher<br />

precision. Our routines all can tolerate the traditional implicit conversion rules,<br />

but they are more efficient without them. Of course, if your application actually<br />

requires double precision, you can change our declarations from float to double<br />

without difficulty. (The brute force approach is to add a preprocessor statement<br />

#define float double !)<br />

A Few Wrinkles<br />

We like to keep code compact, avoiding unnecessary spaces unless they add<br />

immediate clarity. We usually don’t put space around the assignment operator “=”.<br />

Through a quirk of history, however, some C compilers recognize the (nonexistent)<br />

operator “=-” as being equivalent to the subtractive assignment operator “-=”, and<br />

“=*” as being the same as the multiplicative assignment operator “*=”. That is why<br />

you will see us write y= -10.0; or y=(-10.0);, and y= *a; or y=(*a);.<br />

We have the same viewpoint regarding unnecessary parentheses. You can’t write<br />

(or read) C effectively unless you memorize its operator precedence and associativity<br />

rules. Please study the accompanying table while you brush your teeth every night.<br />

We never use the register storage class specifier. Good optimizing compilers<br />

are quite sophisticated in making their own decisions about what to keep in registers,<br />

and the best choices are sometimes rather counter-intuitive.<br />

Different compilers use different methods of distinguishing between defining<br />

and referencing declarations of the same external name in several files. We follow<br />

the most common scheme, which is also the ANSI standard. The storage class<br />

extern is explicitly included on all referencing top-level declarations. The storage<br />

class is omitted from the single defining declaration for each external variable. We<br />

have commented these declarations, so that if your compiler uses a different scheme<br />

you can change the code. The various schemes are discussed in §4.8 of [1].<br />

Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)<br />

Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by <strong>Numerical</strong> Recipes Software.<br />

Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable<br />

files (including this one) to any server computer, is strictly prohibited. To order <strong>Numerical</strong> Recipes books or CDROMs, visit website<br />

http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!