ought to<math.h>The header contains prototypes for several functions that deal with math. In the 1990 version of the ISO standard, onlydoublespecifies the version of the function; the 1999 version added thefloatrespond in singinglong doubleVersion. To use these math functions, the program must be linked to a math library. For some compilers (including GCC), you must specify the additional parameter-lm[1] [2]The
Math functions can produce one of two errors. It occurs when the function has an invalid argumentdomain error, e.g. negative numbers as arguments to sqrt (square root function). If the result of the function cannot be expressed as a specific floating-point type, then arange error, e.g. pow(1000.0,1000.0), if the maximum value of double is approximately 10 308The
These functions can be categorized as follows:
trigonometric function
acosrespond in singingasinfunctionality
ought toacosfunction returns the inverse cosine radians of its arguments, and theasinFunctions return the arc of the inverse sine of their argument. All functions expect the argument to be in the range [-1, + 1]. Inverse cosine returns values in the range [0, π]; arcsine returns values in the range [-π/ 2, +π/ 2].
#include <math.h> float asinf (float x ); float asinf (float x ); float asinf (float x ); float asinf (float x ) / * C99 * / float acosf (float x ); float acosf (float x ); float acosf (float x ); float acosf (float x ) / * C99 * / double asin (double x). double acos (double x). long double asinl (long double x). / * C99 * / long double acosl (long double x). / * C99 * /
atanrespond in singingatan2functionality
ought toatanfunction returns the arcs of the arctangent of its arguments, and theatan2The function returns an arctangenty/xArc. Theatanreturns a value [-π/ 2, +π/ 2] in the range of the function (±π/ 2 is included in the range because floating-point values can be expressed at infinity, ATAN(±∞) = ±π/ 2); theatan2The function returns a value in the range [-π, +π]. Sinceatan2, a domain error may occur if both parameters are zero.
#include <math.h> float atanf (float x ); float atanf (float x ); float atanf (float x ); float atanf (float x ) / * C99 * / float atan2f (float y, float x); float atan2f (float y, float x); float atan2f (float y, float x) / * C99 * / double atan (double x). double atan2 (double y, double x); long double atanl (long double x); and long double atanl (long double x). / * C99 * / long double atan2l (long double y, long double x). / * C99 * /
cos(math.) genussinrespond in singingtanfunctionality
(used form a nominal expression)cos(math.) genussinrespond in singingtanThe function returns the cosine, sine, and tangent of the argument in radians.
#include <math.h> float cosf (float x ); float cosf (float x ); float cosf (float x ); float cosf (float x ) / * C99 * / float sinf (float x ); float sinf (float x ); float sinf (float x ); float sinf (float x ) / * C99 * / float tanf (float x ); float tanf (float x ); float tanf (float x ); float tanf (float x ) / * C99 * / double cos (double x). double cos (double x); double cos (double x); double cos (double x); double cos (double x) double cos (double x); double cos (double x); double cos (double x); double cos (double x) long double cosl (long double x). / * C99 * / long double sinl (long double x); / * C99 * / long double tanl (long double x). / * C99 * /
hyperbolic function
The COSH, hyperbolic sine, and hyperbolic tangent functions compute the hyperbolic cosine, the hyperbolic sine, and the hyperbolic tangent of the respective independent variables. For the hyperbolic sine and cosine functions, range errors occur if the magnitude of the argument is too large.
The ACOSH function described calculates the inverse hyperbolic cosine of the parameter. A domain error occurs when the parameter is less than 1.
The ASINH function described calculates the inverse hyperbolic sine of the parameters.
The ATANH function described calculates the inverse hyperbolic tangent of the parameters. If the parameter is not in the interval [-1, + 1], a domain error occurs. A range error may occur if the parameter is equal to -1 or +1.
#include <math.h> float coshf (float x ); float coshf (float x ); float coshf (float x ); float coshf (float x ) / * C99 * / float sinhf (float x ); float sinhf (float x ); float sinhf (float x ); float sinhf (float x ) / * C99 * / float tanhf (float x ); float tanhf (float x ); float tanhf (float x ); float tanhf (float x ) / * C99 * / double cosh (double x); double sinh (double x) double sinh (double x). double tanh (double x). long double coshl (long double x); double sinh (double x); double tanh (double x); long double coshl (long double x). / * C99 * / long double sinhl (long double x). / * C99 * / long double tanhl (long double x). / * C99 * / float acoshf (float x ); float acoshf (float x ); float acoshf (float x ); float acoshf (float x ) / * C99 * / float asinhf (float x ); float asinhf (float x ); float asinhf (float x ); float asinhf (float x ) / * C99 * / float atanhf (float x ); float atanhf (float x ); float atanhf (float x ); float atanhf (float x ) / * C99 * / double acosh (double x). / * C99 * / double asinh (double x ). / * C99 * / double atanh (double x). / * C99 * / long double acoshl (long double x);; long double acoshl (long double x); long double acoshl (long double x) / * C99 * / long double asinhl (long double x);; / * C99 * / long double atanhl (long double x ); / * C99 * /
Exponential and logarithmic functions
exp(math.) genusexp2respond in singingexpm1functionality
(used form a nominal expression)expfunction to calculate the basesËexponential function of a function (math.)x(Ë X). If the magnitudexToo large and a range error occurs.
(used form a nominal expression)exp2Exponential function with base 2 computed by the functionx(2) X). If the magnitudexToo large and a range error occurs.
(used form a nominal expression)expm1function to calculate the basesËThe exponential function of the argument if the size of the subtraction occurs 1. A range errorxOversized.
#include <math.h> float expf (float x ); float expf (float x ); float expf (float x ); float expf (float x ) / * C99 * / double exp (double x ). long double exp (long double x). / * C99 * / float exp2f (float x ); float exp2f (float x ); float exp2f (float x ); float exp2f (float x ) / * C99 * / double exp2 (double x ). / * C99 * / long double exp2l (long double x ) / * C99 * / float expm1f (float x). / * C99 * / double expm1 (double x);; / * C99 * / long double expm1l (long double x );; / * C99 * /
frexp(math.) genusldexp(math.) genusmodf(math.) genusscalbnpeacescalblnfunctionality
These functions are heavily used in software floating-point emulators, but are rarely called directly.
Inside a computer, each floating point number is represented by two parts:
- Valid numbers lie in the range [1/2,1] or are equal to zero.
- The index is an integer.
The floating point value {\ displaystyle v} is {\ displaystyle v = {\ rm {significand}} \ times 2 ^ {\ rm {exponent}}}.
suchfrexpfunction decomposes the argument floating-point number intovalueThe two parts are the exponent and the effective number. Taking it apart, it stores the exponent in theintin the object pointed toex, and returns a valid number. In other words, the returned value is a copy of the given floating-point number, but with the exponent replaced by 0. If thevalueis zero, then both parts of the result are zero.
ought toldexpThe function multiplies a floating-point number by an integer power of 2 and returns the result. In other words, it returns a copy of the given floating-point number. the exponent is increased by ex. range errors may occur.
suchmodffunction takes the argumentvalueare decomposed into integer and decimal parts, each with the same sign as the argument. They store the integer part in the object they point to*iptrand returns the fraction part. It*iptr是一个浮点类型,而不是“int”类型,因为它可能用于存储一个像1 000 000 000 000 000 000 000这样的整数,这个整数太大而不适合int。
ought toscalbnrespond in singingscalblncountx× . is the basis of a floating-point system; if it is 2, the function is equivalent. FLT_RADIXnFLT_RADIXldexp
#include <math.h> float frexpf (float value, int * ex); float frexpf (float value, int * ex). / * C99 * / double frexp (double value, int * ex); long double frexpl (long double value, int * ex); double frexp (double value, int * ex) long double frexpl (long double value, int * ex). / * C99 * / float ldexpf (float x, int ex); float ldexpf (float x, int ex); float ldexpf (float x, int ex) / * C99 * / double ldexp (double x, int ex); long double ldexpl (long double x, int ex); double ldexp (long double x, int ex) long double ldexpl (long double x, int ex). / * C99 * / float modff (float value, float * iptr); float modff (float value, float * iptr). / * C99 * / double modf (double value, double * iptr); long double modfl (long double value, long double * iptr);; double modf (double value, double * iptr) long double modfl (long double value, long double * iptr). / * C99 * / float scalbnf (float x, int ex); float scalbnf (float x, int ex); float scalbnf (float x, int ex) / * C99 * / double scalbn (double x, int ex); / * C99 * / long double scalbnl (long double x, int ex); / * C99 * / float scalblnf (float x, long int ex); float scalblnf (float x, long int ex); float scalblnf (float x, long int ex) / * C99 * / double scalbln (double x, long int ex); / * C99 * / long double scalblnl (long double x, long int ex); / * C99 * /
Most C floating-point libraries also implement the nextafter(), nextUp(), and nextDown() functions recommended by IEEE754.
log(math.) genuslog2(math.) genuslog1ppeacelog10functionality
ought tologfunction to calculate the basesëNatural ((negative prefix)(common) The logarithm of the argument and returns the result. If the argument is negative, a domain error can occur. If the argument is zero, a range error may occur.
ought tolog1pfunction to calculate the basesëNatural ((negative prefix)Common) a logarithm of the plus argument and returns the result. If the argument is less than -1, a domain error can occur. If the argument is -1, a range error may occur.
(used form a nominal expression)log10The function computes the common (base-10) logarithm of the argument and returns the result. If the argument is negative, a domain error can occur. If the argument is zero, a range error may occur.
(used form a nominal expression)log2The function computes the base log2 of the argument and returns the result. If the argument is negative, a domain error can occur. If the argument is zero, a range error may occur.
#include <math.h> float logf (float x ); float logf (float x ); float logf (float x ); float logf (float x ) / * C99 * / double log (double x). long double logl (long double x ). / * C99 * / float log1pf (float x ); float log1pf (float x ); float log1pf (float x ); float log1pf (float x ) / * C99 * / double log1p (double x);; double log1p (double x) / * C99 * / long double log1pl (long double x ) / * C99 * / float log10f (float x). / * C99 * / double log10 (double x); long double log10l (long double x) long double log10l (long double x ). / * C99 * / float log2f (float x ); float log2f (float x ); float log2f (float x ); float log2f (float x ) / * C99 * / double log2 (double x ). / * C99 * / long double log2l (long double x ) / * C99 * /
ilogbrespond in singinglogbfunctionality
suchilogbThe function extracts the indexxis a signed int value. If thexis zero, then the return valueFP_ILOGB0; ifxare infinite, they return the valueINT_MAX; ifxReturns a value if it is not a numberFP_ILOGBNAN; otherwise, they are equivalent to calling the correspondinglogbfunction and converts the returned value to type int. Ifxis zero, a range error may have occurred.FP_ILOGB0besidesFP_ILOGBNANare defined macrosmath.h; INT_MAXis a macro that defineslimits.hThe
suchlogbfunction (math.)xExtracts the exponent as a signed integer value in floating point format. If thexis below normal, it is considered normalized; thus, for positive finitex1≤ x× < . is the base of the floating point number defined in the title. FLT_RADIX-logb(x)FLT_RADIXFLT_RADIXfloat.h
#include <math.h> int ilogbf (float x ); / * C99 * / int ilogb (double x ); int ilogb (double x ); int ilogb (double x ) / * C99 * / int ilogbl (long double x ). / * C99 * / float logbf (float x ); float logbf (float x ); float logbf (float x ); float logbf (float x ) / * C99 * / double logb (double x ). / * C99 * / long double logbl (long double x);; long double logbl (long double x);; long double logbl (long double x) / * C99 * /
Power Functions
powfunctionality
ought topowfunctional calculationxPower.yand returns the result. If thexis negative andyis not an integer value, a domain error occurs. A domain error occurs if thexZero is zero andyA domain error occurs if the result cannot be represented when it is less than or equal to zero. A range error may occur.
#include <math.h> float powf (float x, float y); float powf (float x, float y); float powf (float x, float y) / * C99 * / double POW (double x, double ÿ); long double powl (long double x, long double y); long double powl (long double x, long double y) long double powl (long double x, long double y). / * C99 * /
sqrtfunctionality
ought tosqrtPositive square root calculated by the functionx, and returns the result. If the argument is negative, a domain error occurs.
#include <math.h> float sqrtf (float x ); float sqrtf (float x ); float sqrtf (float x ); float sqrtf (float x ) / * C99 * / double sqrt (double x);; long double sqrtl (long double x); double sqrtl (long double x) long double sqrtl (long double x ). / * C99 * /
cbrtfunctionality
ought tocbrtThe cube root calculated by the functionxand return the result.
#include <math.h> float cbrtf (float x ); float cbrtf (float x ); float cbrtf (float x ); float cbrtf (float x ) / * C99 * / double cbrt (double x ). / * C99 * / long double cbrtl (long double x );; / * C99 * /
Hypot function
(used form a nominal expression)hypotThe square root of the sum of squares calculated by the functionxrespond in singingy, there is no overflow or underflow, and returns the result.
#include <math.h> float hypotf (float x, float y); float hypotf (float x, float y); float hypotf (float x, float y) / * C99 * / The double hypothesis will be (double X, double ÿ). / * C99 * / long double long skin (long double x, long double y). / * C99 * /
Nearest integer, absolute value and remainder functions
ceilrespond in singingfloorfunctionality
ought toceilThe function calculates the smallest integral value not less thanxand returns the result; thefloorThe function calculates no greater than the maximum integral valuexand return the result.
#include <math.h> float ceilf (float x ); float ceilf (float x ); float ceilf (float x ); float ceilf (float x ) / * C99 * / double ceil (double x). long double towel (long double x). / * C99 * / float floorf (float x ); float floorf (float x ); float floorf (float x ); float floorf (float x ) / * C99 * / Double floorl (double X);; Double floorl (double X); Double floorl (double X); Double floorl (double X) long double floorl (long double x). / * C99 * /
fabsfunctionality
suchfabsfunction calculates the absolute value of a floating point numberxand return the result.
#include <math.h> float fabsf (float x ); float fabsf (float x ); float fabsf (float x ); float fabsf (float x ) / * C99 * / double fabs (double x). long double fabsl (long double x). / * C99 * /
fmodfunctionality
(used form a nominal expression)fmodFloating point residue computed by the functionx/yand returns the valuex– me * yFor some integersme, making it possible to use the same method if theyis not zero, then the results have the same signxand magnitude less thany. Ifyis zero, then whether a domain error occurred orfmodA function returning zero is implementation-defined.
#include <math.h> float fmodf (float x, float y); float fmodf (float x, float y); float fmodf (float x, float y) / * C99 * / double fmod (double x, double y); long double fmodl (long double x, long double y); double fmod (double x, double y) long double fmodl (long double x, long double y). / * C99 * /
nearbyint(math.) genusrint(math.) genuslrintpeacellrintfunctionality
suchnearbyintFunctions use the current rounding direction to round their arguments to integer values in floating-point format without throwing "imprecise" floating-point exceptions.
suchrintFunctions are similar to functions thatnearbyintThey can raise "imprecise" floating point exceptions if the result is different from the value of the parameter.
(used form a nominal expression)lrintrespond in singingllrintDepending on the current rounding direction function loops their arguments to the nearest integer value. If the result is outside the range of values of the return type, the numerical result is undefined and a range error may occur if the size of the argument is too large.
#include <math.h> float nearbyintf (float x);; float x / * C99 * / double nearbyint (double x );; / * C99 * / long double nearbyintl (long double x ) / * C99 * / float rintf (float x ); float rintf (float x ); float rintf (float x ); float rintf (float x ) / * C99 * / double rint (double x). / * C99 * / long double rintl (long double x );; / * C99 * / long int lrintf (float x ); / * C99 * / long int lrint (double x );; / * C99 * / long int lrintl (long double x ) / * C99 * / long long int llrintf (float x );; / * C99 * / long long int llrint (double x );; / * C99 * / long long int llrintl (long double x );; / * C99 * /
round(math.) genuslroundrespond in singingllroundfunctionality
ought toroundThe argument of the wheel functions on the nearest integer value in floating-point format, rounding half cases away from zero regardless of the current rounding direction.
(used form a nominal expression)lroundrespond in singingllroundThe function's loop argument is the value of the nearest integer, rounding half cases away from zero, regardless of the current rounding direction. Numeric results are undefined if the result is outside the range of values of the return type, and a range error may occur if the size of the argument is too large.
#include <math.h> float roundf (float x ); float roundf (float x ); float roundf (float x ); float roundf (float x ) / * C99 * / double circle (double X). / * C99 * / long double roundl (long double x );; long double roundl (long double x ); long double roundl (long double x ) / * C99 * / long int lroundf (float x ); / * C99 * / long int lround (double x);; / * C99 * / long int lroundl (long double x ) / * * * C99 / long very long int llroundf (float x ); / * C99 * / long long int llround (double x );; / * C99 * / long long int llroundl (long double x );; / * C99 * /
truncfunctionality
(used form a nominal expression)truncThe loop of the function whose argument is in floating-point format is closest to the integer value but is greater in magnitude than the argument is no greater.
#include <math.h> float truncf (float x ); float truncf (float x ); float truncf (float x ); float truncf (float x ) / * C99 * / double trunc (double x );; double trunc (double x ); double trunc (double x ); double trunc (double x ) / * C99 * / long double truncl (long double x );; / * C99 * /
remainderfunctionality
suchremainderFunction to calculate the remainder as defined in IEC 60559xREM y. Defined as "wheny ≠ 0, regardless of the rounding mode, by mathematically simplifying ther = x – nyDefine remainderr = x REM ywhichnIt's the closest thing tox / yThe integer of the exact value; as long as | n – x / y | = ½, thennis an even number. Therefore, the remainder is always exact. Ifr = 0, then its sign should bex'snotation"This definition applies to all implementations.
#include <math.h> float remainderf (float x, float y); float remainderf (float x, float y); float remainderf (float x, float y) / * C99 * / double remainder (double x, double y). / * C99 * / long double remainderl (long double x, long double y); / * C99 * /
remquofunctionality
ought toremquofunction returns the rest of the sameremainderFunction. In the object pointed toquo, they store a value with the symbolxThe symbol for /.ywhose magnitude is the same as the magnitude of the integer quotient of / 2 nwhichnis a realization-defined integer greater than or equal to 3. xy
#include <math.h> float remquof (float x, float y, int * quo);; / * C99 * / double remquo (double x, double y, int * quo);; / * C99 * / long double remquol (long double x, long double y, int * quo);; / * C99 * /
Error and gamma functions
ought toerffunction calculates the error function of the argument {\ displaystyle {\ frac {2} {\ sqrt {\ pi}}} \ int _ {0} ^ {x} e ^ { - t ^ {2}} \, \ mathrm {d} t}; theerfcfunction computes the complementary error function of the parameters (i.e., 1 - ERF X). For theerfcfunction, a range error may occur if the argument is too large.
ought tolgammaThe function computes the natural logarithm of the absolute value of the gamma of the argument (i.e., the logË |Γ(X)|). A range error may occur if the argument is a negative integer or zero.
(used form a nominal expression)tgammaThe function computes the gamma of the argument (i.e., of Γ(x)). A domain error occurs if the argument is a negative integer, or if the result cannot be expressed if the argument is zero. A range error may occur.
#include <math.h> float erff (float x ); float erff (float x ); float erff (float x ); float erff (float x ) / * C99 * / double erf (double x ). / * C99 * / long double erfl (long double x). / * C99 * / float erfcf (float x ); float erfcf (float x ); float erfcf (float x ); float erfcf (float x ) / * C99 * / double erfc (double x ). / * C99 * / long double erfcl (long double x);; / * C99 * / float lgammaf (float x ); float lgammaf (float x ); float lgammaf (float x ); float lgammaf (float x ) / * C99 * / double lgamma (double x );; double lgamma (double x ); double lgamma (double x ); double lgamma (double x ) / * C99 * / long double lgammal (long double x ); / * C99 * / float tgammaf (float x ); float tgammaf (float x ); float tgammaf (float x ); float tgammaf (float x ) / * C99 * / double tgamma (double x ). / * C99 * / long double tgammal (long double x ); / * C99 * /
Guess you want to read:Programming in C. Intermediate C 8. Library



