Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://jet.sao.ru/hq/sts/linux/book/c_marshall/node17.html
Дата изменения: Unknown Дата индексирования: Tue Oct 2 07:56:33 2012 Кодировка: Поисковые слова: carl sagan |
Mathematics is relatively straightforward library to use again. You
must #include <math.h>
and must remember to link in the math
library at compilation:
cc mathprog.c -o mathprog -lm
A common source of error is in forgetting to include the <math.h> file (and yes experienced programmers make this error also). Unfortunately the C compiler does not help much. Consider:
double x; x = sqrt(63.9);
Having not seen the prototype for sqrt the compiler (by default) assumes that the function returns an int and converts the value to a double with meaningless results.
Below we list some common math functions. Apart from the note above they should be easy to use and we have already used some in previous examples. We give no further examples here:
double acos(double x)
-- Compute arc cosine of x.
double asin(double x)
-- Compute arc sine of x.
double atan(double x)
-- Compute arc tangent of x.
double atan2(double y, double x)
-- Compute arc tangent of
y/x.
double ceil(double x)
-- Get smallest integral value that
exceeds x.
double cos(double x)
-- Compute cosine of angle in radians.
double cosh(double x)
-- Compute the hyperbolic cosine of x.
div_t div(int number, int denom)
-- Divide one integer by
another.
double exp(double x
-- Compute exponential of x
double fabs (double x )
-- Compute absolute value of x.
double floor(double x)
-- Get largest integral value less
than x.
double fmod(double x, double y)
-- Divide x by y with
integral quotient and return remainder.
double frexp(double x, int *expptr)
-- Breaks down x into
mantissa and exponent of no.
labs(long n)
-- Find absolute value of long integer n.
double ldexp(double x, int exp)
-- Reconstructs x out of
mantissa and exponent of two.
ldiv_t ldiv(long number, long denom)
-- Divide one long
integer by another.
double log(double x)
-- Compute log(x).
double log10 (double x )
-- Compute log to the base 10 of
x.
double modf(double x, double *intptr)
-- Breaks x into
fractional and integer parts.
double pow (double x, double y)
-- Compute x raised to
the power y.
double sin(double x)
-- Compute sine of angle in
radians.
double sinh(double x)
- Compute the hyperbolic sine of x.
double sqrt(double x)
-- Compute the square root of x.
void srand(unsigned seed)
-- Set a new seed for the random
number generator (rand).
double tan(double x)
-- Compute tangent of angle in radians.
double tanh(double x)
-- Compute the hyperbolic tangent of x.
The math.h library defines many (often neglected) constants. It is always advisable to use these definitions:
There are also a number a machine dependent values defined in
#include <value.h>
-- see man value or list value.h
for further details.