Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more ➡
Download
Standard view
Full view
of .
Add note
Save to My Library
Sync to mobile
Look up keyword
Like this
1Activity
×
0 of .
Results for:
No results containing your search query
P. 1
c1-2

c1-2

Ratings: (0)|Views: 423|Likes:
Published by api-3853770

More info:

Published by: api-3853770 on Oct 19, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See More
See less

03/18/2014

pdf

text

original

1.2 Some C Conventions for Scienti\ufb01c Computing
15
S
a
m
p
l
e

p a g e

f
r
o
m

N
U
M

E
R
I
C
A
L
R
E
C
I

P E S

I
N
C
:
T
H
E
A
R
T
O
F
S
C
I
E
N
T
I
F
I
C
C
O
M
P
U
T
I
N
G
(
I

S
B
N

0
-

5 2 1 -

4 3 1 0 8 -

5
)
C

o p y

r
i

g
h
t
(

C
)

1 9 8 8 -

1 9 9 2

b
y
C
a
m
b
r
i

d g e

U
n
i
v
e
r
s
i
t
y
P
r
e

s s .

P
r

o g r

a
m
s
C

o p y

r
i

g
h
t
(

C
)

1 9 8 8 -

1 9 9 2

b
y
N
u
m
e
r
i
c
a
l
R
e
c
i

p e s

S
o

f
t
w

a
r
e
.
P
e
r
m
i

s s i

o
n
i
s
g
r

a n t

e
d
f
o
r
i
n
t
e
r

n
e
t
u
s

e
r
s
t

o
m
a

k
e

o n e

p a p e r

c

o p y

f
o
r
t

h e i

r
o
w
n

p e r

s

o n a l

u
s

e
.
F

u
r
t

h e r

r

e p r

o d u c

t
i

o
n
,
o
r

a n y

c

o p y

i
n
g

o
f
m

a
c
h
i

n e -

r

e a d a b l

e
f

i
l
e

s
(
i
n
c
l

u d i

n
g
t
h
i
s

o n e )

t
o

a n y

s
e
r
v
e
r
c
o
m

p u t

e
r
,
i
s
s
t
r
i
c
t
l
y
p
r

o h i

b
i
t

e
d
.
T

o
o
r

d e r

N
u
m
e
r
i
c
a
l
R
e
c
i

p e s

b o o k

s
o
r

C
D
R
O

M
s
,
v
i
s
i
t
w

e b s

i
t
e
h

t
t
p

:
/
/
w

w w .

n
r
.
c
o
m
o
r
c
a
l
l
1
-

8 0 0 -

8 7 2 -

7 4 2 3

(
N
o
r
t
h
A
m
e
r
i
c
a

o n l

y
)
,
o
r
s

e n d

e
m
a
i
l
t
o
d
i
r
e
c
t
c
u
s
t
s
e
r
v
@
c
a
m
b
r
i

d g e .

o
r
g
(

o u t

s
i
d
e
N
o
r
t
h
A
m
e
r
i
c
a
)
.
if (julian >= IGREG) {
Cross-over to Gregorian Calendar produces this correc-
tion.
jalpha=(long)(((double) (julian-1867216)-0.25)/36524.25);
ja=julian+1+jalpha-(long) (0.25*jalpha);
} else if (julian < 0) {

Make day number positive by adding integer number of Julian centuries, then subtract them o\ufb00 at the end.

ja=julian+36525*(1-julian/36525);
} else
ja=julian;
jb=ja+1524;
jc=(long)(6680.0+((double) (jb-2439870)-122.1)/365.25);
jd=(long)(365*jc+(0.25*jc));
je=(long)((jb-jd)/30.6001);
*id=jb-jd-(long) (30.6001*je);
*mm=je-1;
if (*mm > 12) *mm -= 12;
*iyyy=jc-4715;

if (*mm > 2) --(*iyyy);
if (*iyyy <= 0) --(*iyyy);
if (julian < 0) *iyyy -= 100*(1-julian/36525);

}
(For additional calendrical algorithms, applicable to various historical calendars, see[8].)
CITED REFERENCES AND FURTHER READING:
Harbison, S.P., and Steele, G.L., Jr. 1991,C: A Reference Manual, 3rd ed. (Englewood Cliffs,
NJ: Prentice-Hall).
Kernighan, B.W. 1978,The Elements of Programming Style (New York: McGraw-Hill). [1]
Yourdon, E. 1975,Techniques of Program Structure and Design (Englewood Cliffs, NJ: Prentice-
Hall). [2]
Jones, R., and Stewart, I. 1987,The Art of C Programming (New York: Springer-Verlag). [3]
Hoare, C.A.R. 1981,Communications of the ACM, vol. 24, pp. 75\u201383.

Wirth, N. 1983,Programming in Modula-2, 3rd ed. (New York: Springer-Verlag). [4]
Stroustrup, B. 1986,The C++ Programming Language (Reading, MA: Addison-Wesley). [5]
Borland International, Inc. 1989,Turbo Pascal 5.5 Object-Oriented Programming Guide (Scotts

Valley, CA: Borland International). [6]
Meeus, J. 1982,Astronomical Formulae for Calculators, 2nd ed., revised and enlarged (Rich-
mond, VA: Willmann-Bell). [7]
Hatcher, D.A. 1984,Quarterly Journal of the Royal Astronomical Society, vol. 25, pp. 53\u201355; see
alsoop. cit. 1985, vol. 26, pp. 151\u2013155, and 1986, vol. 27, pp. 506\u2013507. [8]
1.2 Some C Conventions for Scienti\ufb01c
Computing

TheC language was devised originally for systems programming work, not for scienti\ufb01c computing. Relative to other high-level programming languages,C puts the programmer \u201cvery close to the machine\u201d in several respects. It is operator-rich, giving direct access to most capabilities of a machine-language instruction set. It has a large variety of intrinsic data types (short and long, signed and unsigned integers; \ufb02oating and double-precision reals; pointer types; etc.), and a concise syntax for effecting conversions and indirections. It de\ufb01nes an arithmetic on pointers (addresses) that relates gracefully to array addressing and is highly compatible with the index register structure of many computers.

16
Chapter1. Preliminaries
S
a
m
p
l
e

p a g e

f
r
o
m

N
U
M

E
R
I
C
A
L
R
E
C
I

P E S

I
N
C
:
T
H
E
A
R
T
O
F
S
C
I
E
N
T
I
F
I
C
C
O
M
P
U
T
I
N
G
(
I

S
B
N

0
-

5 2 1 -

4 3 1 0 8 -

5
)
C

o p y

r
i

g
h
t
(

C
)

1 9 8 8 -

1 9 9 2

b
y
C
a
m
b
r
i

d g e

U
n
i
v
e
r
s
i
t
y
P
r
e

s s .

P
r

o g r

a
m
s
C

o p y

r
i

g
h
t
(

C
)

1 9 8 8 -

1 9 9 2

b
y
N
u
m
e
r
i
c
a
l
R
e
c
i

p e s

S
o

f
t
w

a
r
e
.
P
e
r
m
i

s s i

o
n
i
s
g
r

a n t

e
d
f
o
r
i
n
t
e
r

n
e
t
u
s

e
r
s
t

o
m
a

k
e

o n e

p a p e r

c

o p y

f
o
r
t

h e i

r
o
w
n

p e r

s

o n a l

u
s

e
.
F

u
r
t

h e r

r

e p r

o d u c

t
i

o
n
,
o
r

a n y

c

o p y

i
n
g

o
f
m

a
c
h
i

n e -

r

e a d a b l

e
f

i
l
e

s
(
i
n
c
l

u d i

n
g
t
h
i
s

o n e )

t
o

a n y

s
e
r
v
e
r
c
o
m

p u t

e
r
,
i
s
s
t
r
i
c
t
l
y
p
r

o h i

b
i
t

e
d
.
T

o
o
r

d e r

N
u
m
e
r
i
c
a
l
R
e
c
i

p e s

b o o k

s
o
r

C
D
R
O

M
s
,
v
i
s
i
t
w

e b s

i
t
e
h

t
t
p

:
/
/
w

w w .

n
r
.
c
o
m
o
r
c
a
l
l
1
-

8 0 0 -

8 7 2 -

7 4 2 3

(
N
o
r
t
h
A
m
e
r
i
c
a

o n l

y
)
,
o
r
s

e n d

e
m
a
i
l
t
o
d
i
r
e
c
t
c
u
s
t
s
e
r
v
@
c
a
m
b
r
i

d g e .

o
r
g
(

o u t

s
i
d
e
N
o
r
t
h
A
m
e
r
i
c
a
)
.

Portability has always been another strong point of theC language.C is the underlying language of the UNIX operating system; both the language and the operating system have by now been implemented on literally hundreds of different computers. The language\u2019s universality, portability, and \ufb02exibility have attracted increasing numbers of scientists and engineers to it. It is commonly used for the real-time control of experimental hardware, often in spite of the fact that the standard UNIX kernel is less than ideal as an operating system for this purpose.

The use ofC for higher level scienti\ufb01c calculations such as data analysis, modeling, and \ufb02oating-point numerical work has generally been slower in developing. In part this is due to the entrenched position ofFORTRAN as the mother-tongue of virtually all scientists and engineers born before 1960, and most born after. In part, also, the slowness ofC\u2019s penetration into scienti\ufb01c computing has been due to de\ufb01ciencies in the language that computer scientists have been (we think, stubbornly) slow to recognize. Examples are the lack of a good way to raise numbers to small integer powers, and the \u201cimplicit conversion offloat todouble\u201d issue, discussed below. Many, though not all, of these de\ufb01ciencies are overcome in the ANSI C Standard. Some remaining de\ufb01ciencies will undoubtedly disappear over time.

Yet another inhibition to the mass conversion of scientists to theC cult has been, up to the time of writing, the decided lack of high-quality scienti\ufb01c or numerical libraries. That is the lacuna into which we thrust this edition ofNumerical Recipes. We certainly do not claim to be a complete solution to the problem. We do hope to inspire further efforts, and to lay out by example a set of sensible, practical conventions for scienti\ufb01cC programming.

The need for programming conventions inC is very great. Far from the problem
of overcoming constraints imposed by the language (our repeated experience with
Pascal), the problem in Cis to choose the best and most natural techniques from

multiple opportunities \u2014 and then to use those techniques completely consistently from program to program. In the rest of this section, we set out some of the issues, and describe the adopted conventions that are used in all of the routines in this book.

Function Prototypes and Header Files

ANSI C allows functions to be de\ufb01ned withfunction prototypes, which specify the type of each function parameter. If a function declaration or de\ufb01nition with a prototype is visible, the compiler can check that a given function call invokes the function with the correct argument types. All the routines printed in this book are in ANSI C prototype form. For the bene\ufb01t of readers with older \u201ctraditional K&R\u201dC compilers, theNumerical Recipes C Diskette includes two complete sets of programs, one in ANSI, the other in K&R.

The easiest way to understand prototypes is by example. A function de\ufb01nition
that would be written in traditionalC as
int g(x,y,z)
int x,y;
float z;
becomes in ANSI C
1.2 Some C Conventions for Scienti\ufb01c Computing
17
S
a
m
p
l
e

p a g e

f
r
o
m

N
U
M

E
R
I
C
A
L
R
E
C
I

P E S

I
N
C
:
T
H
E
A
R
T
O
F
S
C
I
E
N
T
I
F
I
C
C
O
M
P
U
T
I
N
G
(
I

S
B
N

0
-

5 2 1 -

4 3 1 0 8 -

5
)
C

o p y

r
i

g
h
t
(

C
)

1 9 8 8 -

1 9 9 2

b
y
C
a
m
b
r
i

d g e

U
n
i
v
e
r
s
i
t
y
P
r
e

s s .

P
r

o g r

a
m
s
C

o p y

r
i

g
h
t
(

C
)

1 9 8 8 -

1 9 9 2

b
y
N
u
m
e
r
i
c
a
l
R
e
c
i

p e s

S
o

f
t
w

a
r
e
.
P
e
r
m
i

s s i

o
n
i
s
g
r

a n t

e
d
f
o
r
i
n
t
e
r

n
e
t
u
s

e
r
s
t

o
m
a

k
e

o n e

p a p e r

c

o p y

f
o
r
t

h e i

r
o
w
n

p e r

s

o n a l

u
s

e
.
F

u
r
t

h e r

r

e p r

o d u c

t
i

o
n
,
o
r

a n y

c

o p y

i
n
g

o
f
m

a
c
h
i

n e -

r

e a d a b l

e
f

i
l
e

s
(
i
n
c
l

u d i

n
g
t
h
i
s

o n e )

t
o

a n y

s
e
r
v
e
r
c
o
m

p u t

e
r
,
i
s
s
t
r
i
c
t
l
y
p
r

o h i

b
i
t

e
d
.
T

o
o
r

d e r

N
u
m
e
r
i
c
a
l
R
e
c
i

p e s

b o o k

s
o
r

C
D
R
O

M
s
,
v
i
s
i
t
w

e b s

i
t
e
h

t
t
p

:
/
/
w

w w .

n
r
.
c
o
m
o
r
c
a
l
l
1
-

8 0 0 -

8 7 2 -

7 4 2 3

(
N
o
r
t
h
A
m
e
r
i
c
a

o n l

y
)
,
o
r
s

e n d

e
m
a
i
l
t
o
d
i
r
e
c
t
c
u
s
t
s
e
r
v
@
c
a
m
b
r
i

d g e .

o
r
g
(

o u t

s
i
d
e
N
o
r
t
h
A
m
e
r
i
c
a
)
.
int g(int x, int y, float z)
A function that has no parameters has the parameter type listvoid.

A function declaration (as contrasted to a function de\ufb01nition) is used to \u201cintroduce\u201d a function to a routine that is going tocall it. The calling routine needs to know the number and type of arguments and the type of the returned value. In a function declaration, you are allowed to omit the parameter names. Thus the declaration for the above function is allowed to be written

int g(int, int, float);

If aC program consists of multiple source \ufb01les, the compiler cannot check the consistency of each function call without some additional assistance. The safest way to proceed is as follows:

\u2022Every external function should have a single prototype declaration in a
header (.h) \ufb01le.
\u2022The source \ufb01le with the de\ufb01nition (body) of the function should also
include the header \ufb01le so that the compiler can check that the prototypes
in the declaration and the de\ufb01nition match.
\u2022Every source \ufb01le that calls the function should include the appropriate
header (.h) \ufb01le.
\u2022Optionally, a routine that calls a function can also include that function\u2019s

prototype declaration internally. This is often useful when you are developing a program, since it gives you a visible reminder (checked by the compiler through the common.h \ufb01le) of a function\u2019s argument types. Later, after your program is debugged, you can go back and delete the supernumary internal declarations.

For the routines in this book, the header \ufb01le containing all the prototypes isnr.h, listed in Appendix A. You should put the statement#include nr.h at the top of every source \ufb01le that containsNumerical Recipes routines. Since, more frequently than not, you will want to include more than oneNumerical Recipes routine in a single source \ufb01le, we have not printed this#include statement in front of this book\u2019s individual program listings, but you should make sure that it is present in your programs.

As backup, and in accordance with the last item on the indented list above, we declare the function prototype of allNumerical Recipes routines that are called by otherNumerical Recipes routinesinternally to the calling routine. (That also makes our routines much more readable.) The only exception to this rule is that the small number of utility routines that we use repeatedly (described below) are declared in the additional header \ufb01lenrutil.h, and the line#include nrutil.h is explicitly printed whenever it is needed.

A \ufb01nal important point about the header \ufb01lenr.h is that, as furnished on the diskette, it contains both ANSI C and traditional K&R-style declarations. The ANSI forms are invoked if any of the following macros are de\ufb01ned:__STDC__,

ANSI, or NRANSI. (The purpose of the last name is to give you an invocation that
does not con\ufb02ict with other possible uses of the \ufb01rst two names.) If you have an
ANSI compiler, it isessential that you invoke it with one or more of these macros

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->