Previous | Table of Contents | Next |
3.10.5.2. Trigonometric Functions
double sin(double x) double cos(double x) double tan(double x) double asin(double x) double acos(double x) double atan(double x) double atan2(double y, double x)
These functions all operate on (or return) angles in radians. atan2(y, x) is equivalent to atan(y / x) except that it returns an angle with a sign appropriate for all four quadrants of a rectangular-to-polar conversion, and works correctly if x is 0.
3.10.5.3. Hyperbolic Functions
double sinh(double x) double cosh(double x) double tanh(double x)
These three functions all perform hyperbolic trigonometry.
3.10.5.4. Absolute Value and Nearest-Integer Functions
double fabs(double x) double ceil(double x) double floor(double x)
fabs(x) returns the absolute value of x. ceil(x) returns (as a double) the smallest integer not less than x. Similarly, floor(x) returns the largest integer not greater than x.
3.10.5.5. Other Functions
double fmod(double x, double y) double modf(double x, double *iptr) double frexp(double x, int *ip) double ldexp(double x, int exponent)
fmod returns the remainder when x is divided by y. modf breaks x into integer and fractional parts, storing the integer part (as a double) in *iptr and returning the fractional part. frexp splits x into its base-2 floating-point mantissa and exponent, storing the exponent in *ip and returning the mantissa. ldexp builds a floating-point number from a mantissa and base-2 exponent. For example, frexp(2.5, &i) would return 0.625 (which is 0.101 base 2) and set i to 2, and ldexp(0.625, 2) would reconstruct the value 2.5.
The header <time.h> defines several functions for dealing with dates and times. Many of these functions work with the abstract type time_t, a typedef for an arithmetic type capable of representing a date/time stamp. Note that no particular size or representation for type time_t is mandated by the C standard. Many systems implement time_t as a long integer representing the number of seconds since the beginning of January 1, 1970, but this representation is not universal.
Several other functions work with a structure that contains individual date and time components. That structure is declared somewhat as follows:
struct tm { int tm_sec; /* 0..61 */ int tm_min; /* 0..59 */ int tm_hour; /* 0..23 */ int tm_mday; /* 1..31 */ int tm_mon; /* 0..11 */ int tm_year; int tm_wday; /* 0..6 */ int tm_yday; /* 0..365 */ int tm_isdst; };
A particular implementation may add additional fields to this structure, and the fields are not necessarily in the preceding order. The ranges of several of the members are as indicated in the comments. tm_sec is allowed to range above 59 to accommodate the possibility of leap seconds. tm_mon contains 0 for January, 11 for December. tm_year contains the year minus 1900; it will contain the value 100 in the year 2000. tm_wday contains 0 for Sunday, 6 for Saturday. tm_yday contains 0 for January 1, 364 or 365 for December 31. tm_isdst contains 0 if Daylight Saving Time is not in effect, a positive value if it is in effect, and a negative value if DST information is unknown.
Several of these functions attempt to make distinctions between local time and UTC (Coordinated Universal Time, also known as GMT), or to account for Daylight Saving Time, but the information required for making these corrections is not always available from the underlying operating system.
3.10.6.1. time
time_t time(time_t *tp)
time returns a time_t value representing the current date and time. If tp is non-null, the same value is stored in *tp.
3.10.6.2. localtime, gmtime
struct tm *localtime(const time_t *t) struct tm *gmtime(const time_t *t)
localtime builds a tm structure representing the local time corresponding to the time value t. gmtime does the same thing, if possible, but building a UTC representation. Note that the time_t value is passed by reference. The return pointer is to a static structure that is overwritten by each call, or is a null pointer if the conversion is impossible (e.g., if time zone information for UTC conversion is not available).
3.10.6.3. ctime, asctime
char *ctime(const time_t *t) char *asctime(const struct tm *tp)
These functions construct a human-readable string corresponding to a date and time. The string is of the form
Tue Oct 28 07:50:28 1997\n\0
(and is therefore equivalent to calling strftime with the format string %a %b %m %H:%M:%S %Y\n). asctime builds the string from the information in the tm structure pointed to by tp. ctime builds the string from the time_t value pointed to by t; it is equivalent to asctime(localtime(t)). The return pointer is to a static string that is overwritten by each call.
Previous | Table of Contents | Next |