C++ cmath header file


The C++ <cmath> header file is borrowed from the C library <math.h> header file.The C++ version however includes namespacing the content with std to standardize it and make it as a formal C++ Standard Library headers file.

Here we will see the macros and the functions available in this header file.To use all the content of this header file in your program include the header <cmath>.

MACROS

HUGE_VALRepresent a huge value
INFINITYA macro that represent an infinity.
NANA macro to represent a ‘nan’ value.

Types

float_tA float type but it can vary.
double_tA double type but it can vary.

Error types

Domain errorAn error that occurs if the supplied argument is inconsistent for which the function is defined.
Range errorIf the function’s result cannot be outputted as double value or within any of the specified type.

Functions

Return
type
Function name and
argument
Function definition

Classification function
boolfpclassify(double x)Returns value corresponding to the argument type.
boolisfinite(double x)Determines if the argument is finite.
boolisinf(double x)Determines if the argument is infinity
boolisnan(double x)Determines if the argument is nan
boolisnormal(double x)Determines if the argument is normal
boolsignbit(double x)Determine the sign of the argument

Trigonometric function
doubleacos(double arg)Returns arc cosine of arg
doubleasin(double arg)Return the arc sine of arg
doubleatan(double arg)Returns the arc tangent of arg
doubleatan2(double arg1,double arg2)Returns the arc tangent of arg1/arg2
doublecos(double arg)Returns the cosine of the arg
doublesin(double arg1)Returns the sine of arg
doubletan(double arg)Returns tangent of arg

Hyperbolic function
doubleacosh(double arg)(C++11)Returns inverse hyperbolic cosine of arg
doubleasinh(double arg)(C++11)Returns the inverse hyperbolic sine of arg
doubleatanh(double arg)(C++11)Returns the inverse hyperbolic tangent of arg
doublecosh(double arg)Returns the hyperbolic cosine of arg
doublesinh(double arg)Returns the hyperbolic sine of arg
doubletanh(double arg)Returns hyperbolic tangent of arg

Exponential and logarithmic
doubleexp(double arg)Returns the logarithm base e raise to the power arg
doubleexp2(double arg)(C++11)Returns base-2 exponential of arg
doubleexpm1(double arg)(C++11)Returns base-e exponential of arg minus 1
doublefrexp(double arg ,int *exp)Decomposes arg into mantissa*2 ^ e and returns the mantissa
intilogb(double arg)(C++11)Returns exponent of arg as a signed int value
doubleldexp(double arg,int exp)Returns arg*2 ^ exp
doublelog(double arg)Returns the logarithm of arg
doublelog10(double arg)Returns the base 10 logarithm of arg
doublelog1p(double arg)(C++11)Returns base-e logarithm of 1+arg
doublelog2(double arg)(C++11)Retuns base-2 logarithm of arg
intlogb(double arg)(C++11)Returns exponent of arg as signed int
doublemodf(double arg,double* ptr)Decompose arg into intger and fractional
doublescalbln(double arg1,long pow)
scalbn(double arg , int pow)
(C++11)
Returns x*FLT_RADIXpow

Power and absolute value
doublecbrt(double arg)(C++11)Returns the real cube root of arg
doublefabs(double arg)
abs(double arg)
Returns absolute value of arg
doublehypot(double arg1,double arg2)(C++11)Returns square root of the sum of square of arg1 and arg2
doublepow(double arg1,double arg2)Returns arg1 ^ arg2
doublesqrt(double arg)Returns square root of arg

Error and gamma
doubleerf(double arg)(C++11)Returns error function of arg
doubleerfc(double arg)(C++11)Returns complementary error function of arg
doublelgamma(double arg)(C++11)Returns natural logarithm of the absolute value of arg
doubletgamma(double arg)(C++11)Computes gamma function of arg

Nearest integer
doubleceil(double arg)Returns the smallest integer not less than arg
doublefloor(double arg)Returns largest integer representable by arg
doublenearbyint(double arg)(C++11)Returns rounded integer value
doublerint(double arg)(C++11)Returns rounded integer value
doublellrint(double arg)
lrint(double arg)

(C++11)
return nearest integer value after rounding off
doubleround(double arg)(C++11)Round arg to nearest integer
doublellround(double arg)
lround(double arg)
(C++11)
Round arg to nearest integer
doubletrunc(double arg)(C++11)Truncate arg to integer component

Remainder function
doublefmod(double arg1,double arg2)Returns the remainder of arg1/arg2
doubleremainder(double arg1,double arg2)(C++11)Computes the remainder of arg1/arg2
doubleremquo(double arg, double arg2,int *quo)(C++11)Returns remainder

Manipulation function
doublecopysign(double arg1,double arg2)(C++11)Returns arg1 with the sign of arg2
doublenan(const char* arg)(C++11)Returns quite nan
doublenextafter(double arg1,double arg2)(C++11)Returns representable value after arg1 toward arg2
doublenexttoward(double arg1,long double arg2)(C++11)Returns representable value after arg1 toward arg2

Maximum,minimum and positive difference
doublefdim(double arg1,double arg2)(C++11)Returns (arg1 – arg2) if arg1>arg2 ,else +0
doublefmax(double arg1,double arg2)(C++11)Returns maximum numeric value of the arguments
doublefmin(double arg1,double arg2)(C++11)Returns the minimum numeric value of arguments

Floating multiply
doublefma(double arg1 ,double arg2 ,double arg3)(C++11)Returns (arg1*arg2)+arg3

Comparison
intisgreater(double arg1 , double arg2)(C++11)Determine if arg1 is greater than arg2
intisgreaterequal(double arg1 , double arg2)(C++11)Determine if arg1 is greater or equal to arg2
intisless(double arg1,double arg2)(C++11)Determine if arg1 is less than arg2
intislessequal(double arg1,double arg2)(C++11)Determine if arg1 is less than or equla to arg2
intislessgreater(double arg1,double arg2)(C++11)Determine if arg1 is less than or greater than arg2
intisunordered(double arg1,double arg2)(C++11)Determine the arguments are undordered