Review: C for Numerical Computations


Topics


C Data types for Numerical Computation

When to reconsider: Running out of memory? Maybe int-arrays or float-arrays will do the job too? But only consider this when necessary.

Standard Math Operators

Arithmetic

Pitfalls: More Pitfalls:

Comparison

Pitfalls:

C Data Structures

1-D Arrays (also called Vectors)

Say you want to simulate the rottening behavior of apples. The simulation considers 100 apples. Here's how to store them:

double apples[100];

Indexing and Initializing an Array

long i;
for(i=0; i {
fresh_apples[i] = 1.0;
rotten_apples[i] = 0.0;
}

Pitfall: C array indices run from 0 to array size-1 i.e. fresh_apples runs from 0 to MAX_APPLES-1. If you use MAX_APPLES or bigger numbers you have a bug (which may be hard to find!).

2-D Arrays (or Matrices)

This time we happen to have an acre of apple trees:

#define X_APPLES 10
#define Y_APPLES 15

double apple_garden[X_APPLES][Y_APPLES];


Numerical Input and Output

Input

scanf ("%f", &x); Reads a float from standard input and stores it in x.

scanf ("%lf", &x); Reads a double from standard input and stores it in x.

scanf ("%Lf", &x); Reads a long double from standard input and stores it in x.

Output

Letting afloat = 56.12345, the following output can be generated.
Function call example Sample Output Comments
printf ("%e", afloat);
printf ("%10.2e", afloat);
printf ("%+10.2e", afloat);
5.612345e+01
5.61e+01
+5.61e+01
printf ("%f", afloat);
printf ("%10.2f", afloat);
printf ("%+10.2f,%+10.2f", afloat, -afloat);
printf ("% 10.2f", afloat);
56.12345
56.12
+56.12, -56.12
________56.12

C Standard Library Math Functions

There are numerous math functions available in the c standard library. There are many variants of each function, however so review the following carefully if you get stuck with strange numerical results!

For a comprehensive reference, see C: A Reference Manual by Samuel P. Harbison and Guy L. Steele, Jr. Englewood Cliffs, NJ: Prentice Hall, 1995.
(Copies available in the E-quad library. Call Number: QA76.73.C15 H38 1995)


Absolute Value

int abs (int x); Returns the absolute value of an integer x. Pitfall!! Use fabs for floats.

float fabs (float x); Returns the absolute value of a float x.

long labs (long x); Returns the absolute value of a long integer x.

Ceiling / Floor Function

double ceil (double x); returns smallest float-point number equal to an integer not less than x.

double floor (double x); returns larger float-point number equal to an integer not greater than x.

Exponential / nth Root Functions

double exp (double x); Returns e^x.

double pow (double x, double y); Returns x^y.

double sqrt (double x); Returns square root of x.

Logarithmic Functions

double log (double x); Returns the natural logarithm of x.

double log10 (double x); Returns the base-10 logarithm of x.

Trigonometric Functions

Note: All trig functions work with radians, not degrees.

Cosine related functions

double cos (double x); Calculates cosine of x.

double acos (double x); Calculates arccosine of x.

double cosh (double x); Calculates hyperbolic cosine of x.

Sine related functions

double sin (double x); Calculates sine of x.

double asin (double x); Calculates arcsine of x.

double sinh(double x); Calculates hyperbolic sine of x.

Tangent related functions

double tan (double x); Calculates tangent of x.

double atan (double x); Calculates arctangent of x.

double tanh (double x); Calculates hyperbolic tangent of x.


Notes on Performance

Some of the above math functions, especially transcendental functions, may be considerably slower to execute than standard arithmetic operations. If you use results from those functions repeatedly, storing those values in an array and accessing the array instead can dramatically increase performance.


Compiling

To compile your program inputfile.c to the executable outputfile use the following:

cc -o outputfile inputfile.c -lm

Instead of cc you can also use gcc or lcc. (Try which one gives you the most useful error/warning messages.)

Warning: the flag -lm may be necessary when floating point arithmetic is used. Some compilers will compile and not complain if it's missing. The results of calculations with these programs may be wrong!

Debugging

C programmers rule: Be thankful for a core dump. It's an array index or pointer bug, which is nice enough to express itself! What to do with bugs:

Tricks!

Horner's Method

Any polynomial in the form P(x) = axn + bxn-1 + ... dx + e can be expressed in a form requiring fewer operations. For example:
P(x) = ax3 + bx2 + cx + d = x(x( ax + b ) + c) + d
What exactly is the savings?


Based on a Page Written 7/15/98 by Hide Oki
Modified 9/10/98 by Roger Ahn
Written 9/27/99 by Georg Essl
Meddled with 8/14/02 by Ken Steiglitz