Review: Mathematics in C (Library Functions, Representation, I/O)

COS 323 - Computing for the Physical and Social Sciences


Topics


Numerical Representation and Basic Operations

Representation of numbers is important since it can intrduce bugs. For example the expression 90/365 returns 0! This because integer division is performed on the two numbers. To force floating-point division, you must rewrite it as 90.0 / 365.0.


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);
56.12345
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!

Important Note: To compile programs on CIT machines including the MECA lab's SGI machines, use the -lm option. This forces the compiler to include the appropriate library files.

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.

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, storeing those values in an array and accessing the array instead can dramatically increase performance.


Tricks!

Horner's Method

Any polynomial in the form P(x) = ax^n + bx^(n-1) + ... dx + e can be expressed in a form requiring far less calculations:
P(x) = ax^3 + bx^2 + cx + d = x(x( ax + b ) + c) + d The extremely naive approach of directly evaluating every power of x will cost you Deg Multiplications and Deg Additions whereas Horner's Method which rewrites P(x) in the right form,

Return COS 323 Main Page
Written July 15, 1998 by Hide Oki
Last Modified September 10, 1998 by Roger Ahn