COS 323 - Computing for the Physical and Social Sciences
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.
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.
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 |
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)
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.
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.
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.
double log (double x); Returns the natural logarithm of x.
double log10 (double x); Returns the base-10 logarithm of x.
Note: All trig functions work with radians, not degrees.
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.
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.
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.
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.
Return COS 323 Main Page
Written July 15, 1998 by Hide Oki
Last Modified September 10, 1998 by Roger Ahn