<tgmath.h>

<tgmath.h>

[added with C99]


· acos · acosh · asin · asinh · atan · atanh · atan2 · carg · cbrt · ceil · cimag · conj · copysign · cos · cosh · cproj · creal · erf · erfc · exp · exp2 · expm1 · fabs · fdim · floor · fma · fmax · fmin · fmod · frexp · hypot · ilogb · ldexp · lgamma · llrint · llround · log · log10 · log1p · log2 · logb · lrint · lround · modf · nearbyint · nextafter · nexttoward · pow · remainder · remquo · rint · round · scalbln · scalbn · sin · sinh · sqrt · tan · tanh · tgamma · trunc


Include the standard header <tgmath> to define several families of generic functions. A generic function has the same name for two or more distinct parameter lists, as in:

float _Complex fc;
long double ld;

cos(1)       // same as cos((double)1)
cos(ld)      // same as cosl(ld)
pow(fc, ld)  // same as cpowl((long double _Complex)fc,
                   (long double _Complex)ld)

The actual (non-generic) function called depends on the type(s) of the actual argument(s) on a function call:

  • For a function argument that can have a floating-point type, an integer argument is converted (by a type cast) to double before determining the function to call.
  • For a function with two or more arguments, the arguments are each converted as above, then converted (by a type cast) to the type of the sum of all the arguments before determining the function to call.

The actual function called is the one whose parameters exactly match the type(s) of the converted argument(s).

Note that C++ adds sufficient overloads (not shown here) to provide the same conversion rules. In this implementation, these overloads are declared in <math.h> and <complex2.h>, and hence are present whether or not you include <tgmath.h>.


#include <complex.h>
#include <math.h>

    /* FUNCTIONS */
double acos(double x);
float acos(float x);
long double acos(long double x);
double _Complex acos(double _Complex x);
float _Complex acos(float _Complex x);
long double _Complex acos(long double _Complex x);

double acosh(double x);
float acosh(float x);
long double acosh(long double x);
double _Complex acosh(double _Complex x);
float _Complex acosh(float _Complex x);
long double _Complex acosh(long double _Complex x);

double asin(double x);
float asin(float x);
long double asin(long double x);
double _Complex asin(double _Complex x);
float _Complex asin(float _Complex x);
long double _Complex asin(long double _Complex x);

double asinh(double x);
float asinh(float x);
long double asinh(long double x);
double _Complex asinh(double _Complex x);
float _Complex asinh(float _Complex x);
long double _Complex asinh(long double _Complex x);

double atan(double x);
float atan(float x);
long double atan(long double x);
double _Complex atan(double _Complex x);
float _Complex atan(float _Complex x);
long double _Complex atan(long double _Complex x);

double atan2(double y, double x);
float atan2(float y, float x);
long double atan2(long double y, long double x);

double atanh(double x);
float atanh(float x);
long double atanh(long double x);
double _Complex atanh(double _Complex x);
float _Complex atanh(float _Complex x);
long double _Complex atanh(long double _Complex x);

double carg(double _Complex x);
float carg(float _Complex x);
long double carg(long double _Complex x);

double ceil(double x);
float ceil(float x);
long double ceil(long double x);

double cbrt(double x);
float cbrt(float x);
long double cbrt(long double x);

double cimag(double _Complex x);
float cimag(float _Complex x);
long double cimag(long double _Complex x);

double _Complex conj(double _Complex x);
float _Complex conj(float _Complex x);
long double _Complex conj(long double _Complex x);

double cos(double x);
float cos(float x);
long double cos(long double x);
double _Complex cos(double _Complex x);
float _Complex cos(float _Complex x);
long double _Complex cos(long double _Complex x);

double copysign(double x, double y);
float copysign(float x, float y);
long double copysign(long double x, long double y);

double _Complex cproj(double _Complex x);
float _Complex cproj(float _Complex x);
long double _Complex cproj(long double _Complex x);

double cosh(double x);
float cosh(float x);
long double cosh(long double x);
double _Complex cosh(double _Complex x);
float _Complex cosh(float _Complex x);
long double _Complex cosh(long double _Complex x);

double creal(double _Complex x);
float creal(float _Complex x);
long double creal(long double _Complex x);

double erf(double x);
float erf(float x);
long double erf(long double x);

double erfc(double x);
float erfc(float x);
long double erfc(long double x);

double exp(double x);
float exp(float x);
long double exp(long double x);
double _Complex exp(double _Complex x);
float _Complex exp(float _Complex x);
long double _Complex exp(long double _Complex x);

double exp2(double x);
float exp2(float x);
long double exp2(long double x);

double expm1(double x);
float expm1(float x);
long double expm1(long double x);

double fabs(double x);
float fabs(float x);
long double fabs(long double x);
double fabs(double _Complex x);
float fabs(float _Complex x);
long double fabs(long double _Complex x);

double fdim(double x, double y);
float fdim(float x, float y);
long double fdim(long double x, long double y);

double floor(double x);
float floor(float x);
long double floor(long double x);

double fma(double x, double y, double z);
float fma(float x, float y, float z);
long double fma(long double x, long double y, long double z);

double fmax(double x, double y);
float fmax(float x, float y);
long double fmax(long double x, long double y);

double fmin(double x, double y);
float fmin(float x, float y);
long double fmin(long double x, long double y);

double fmod(double x, double y);
float fmod(float x, float y);
long double fmod(long double x, long double y);

double frexp(double x, int *pexp);
float frexp(float x, int *pexp);
long double frexp(long double x, int *pexp);

double hypot(double x, double y);
float hypot(float x, float y);
long double hypot(long double x, long double y);

int ilogb(double x);
int ilogb(float x);
int ilogb(long double x);

double ldexp(double x, int ex);
float ldexp(float x, int ex);
long double ldexp(long double x, int ex);

double lgamma(double x);
float lgamma(float x);
long double lgamma(long double x);

long long llrint(double x);
long long llrint(float x);
long long llrint(long double x);

long long llround(double x);
long long llround(float x);
long long llround(long double x);

double log(double x);
float log(float x);
long double log(long double x);
double _Complex log(double _Complex x);
float _Complex log(float _Complex x);
long double _Complex log(long double _Complex x);

double log10(double x);
float log10(float x);
long double log10(long double x);

double log1p(double x);
float log1p(float x);
long double log1p(long double x);

double log2(double x);
float log2(float x);
long double log2(long double x);

double logb(double x);
float logb(float x);
long double logb(long double x);

long lrint(double x);
long lrint(float x);
long lrint(long double x);

long lround(double x);
long lround(float x);
long lround(long double x);

double modf(double x, double *pint);
float modf(float x, float *pint);
long double modf(long double x, long double *pint);

double nearbyint(double x);
float nearbyint(float x);
long double nearbyint(long double x);

double nextafter(double x, double y);
float nextafter(float x, float y);
long double nextafter(long double x, long double y);

double nexttoward(double x, long double y);
float nexttoward(float x, long double y);
long double nexttoward(long double x, long double y);

double pow(double x, double y);
float pow(float x, float y);
long double pow(long double x, long double y);
double _Complex pow(double _Complex x);
float _Complex pow(float _Complex x);
long double _Complex pow(long double _Complex x);

double remainder(double x, double y);
float remainder(float x, float y);
long double remainder(long double x, long double y);

double remquo(double x, double y, int *pquo);
float remquo(float x, float y, int *pquo);
long double remquo(long double x, long double y, int *pquo);

double rint(double x);
float rint(float x);
long double rint(long double x);

double round(double x);
float round(float x);
long double round(long double x);

double scalbln(double x, long ex);
float scalbln(float x, long ex);
long double scalbln(long double x, long ex);

double scalbn(double x, int ex);
float scalbn(float x, int ex);
long double scalbn(long double x, int ex);

double sin(double x);
float sin(float x);
long double sin(long double x);
double _Complex sin(double _Complex x);
float _Complex sin(float _Complex x);
long double _Complex sin(long double _Complex x);

double sinh(double x);
float sinh(float x);
long double sinh(long double x);
double _Complex sinh(double _Complex x);
float _Complex sinh(float _Complex x);
long double _Complex sinh(long double _Complex x);

double sqrt(double x);
float sqrt(float x);
long double sqrt(long double x);
double _Complex sqrt(double _Complex x);
float _Complex sqrt(float _Complex x);
long double _Complex sqrt(long double _Complex x);

double tan(double x);
float tan(float x);
long double tan(long double x);
double _Complex tan(double _Complex x);
float _Complex tan(float _Complex x);
long double _Complex tan(long double _Complex x);

double tanh(double x);
float tanh(float x);
long double tanh(long double x);
double _Complex tanh(double _Complex x);
float _Complex tanh(float _Complex x);
long double _Complex tanh(long double _Complex x);

double tgamma(double x);
float tgamma(float x);
long double tgamma(long double x);

double trunc(double x);
float trunc(float x);
long double trunc(long double x);

acos

double acos(double x);
float acos(float x);
long double acos(long double x);
double _Complex acos(double _Complex x);
float _Complex acos(float _Complex x);
long double _Complex acos(long double _Complex x);

The function returns the arccosine of x.

acosh

double acosh(double x);
float acosh(float x);
long double acosh(long double x);
double _Complex acosh(double _Complex x);
float _Complex acosh(float _Complex x);
long double _Complex acosh(long double _Complex x);

The function returns the hyperbolic arccosine of x.

carg

double carg(double _Complex x);
float carg(float _Complex x);
long double carg(long double _Complex x);

The function returns the phase angle of x.

asin

double asin(double x);
float asin(float x);
long double asin(long double x);
double _Complex asin(double _Complex x);
float _Complex asin(float _Complex x);
long double _Complex asin(long double _Complex x);

The function returns the arcsine of x.

asinh

double asinh(double x);
float asinh(float x);
long double asinh(long double x);
double _Complex asinh(double _Complex x);
float _Complex asinh(float _Complex x);
long double _Complex asinh(long double _Complex x);

The function returns the hyperbolic arcsine of x.

atan

double atan(double x);
float atan(float x);
long double atan(long double x);
double _Complex atan(double _Complex x);
float _Complex atan(float _Complex x);
long double _Complex atan(long double _Complex x);

The function returns the arctangent of x.

atan2

double atan2(double y, double x);
float atan2(float y, float x);
long double atan2(long double y, long double x);

The function returns the angle whose tangent is y/x, in the full angular range [-pi, +pi] radians. A domain error may occur if both x and y are zero.

atanh

double atanh(double x);
float atanh(float x);
long double atanh(long double x);
double _Complex atanh(double _Complex x);
float _Complex atanh(float _Complex x);
long double _Complex atanh(long double _Complex x);

The function returns the hyperbolic arctangent of x.

cbrt

double cbrt(double x);
float cbrt(float x);
long double cbrt(long double x);

The function returns the real cube root of x, x^(1/3).

ceil

double ceil(double x);
float ceil(float x);
long double ceil(long double x);

The function returns the smallest integer value not less than x.

cimag

double cimag(double _Complex x);
float cimag(float _Complex x);
long double cimag(long double _Complex x);

The function returns the imaginary part of x.

conj

double _Complex conj(double _Complex x);
float _Complex conj(float _Complex x);
long double _Complex conj(long double _Complex x);

The function returns the conjugate of x.

copysign

double copysign(double x, double y);
float copysign(float x, float y);
long double copysign(long double x, long double y);

The function returns x, with its sign bit replaced from y.

cos

double cos(double x);
float cos(float x);
long double cos(long double x);
double _Complex cos(double _Complex x);
float _Complex cos(float _Complex x);
long double _Complex cos(long double _Complex x);

The function returns the cosine of x.

cosh

double cosh(double x);
float cosh(float x);
long double cosh(long double x);
double _Complex cosh(double _Complex x);
float _Complex cosh(float _Complex x);
long double _Complex cosh(long double _Complex x);

The function returns the hyperbolic cosine of x.

cproj

double _Complex cproj(double _Complex x);
float _Complex cproj(float _Complex x);
long double _Complex cproj(long double _Complex x);

The function returns a projection of x onto the Riemann sphere. Specifically, if either component of x is an infinity of either sign, the function returns a value whose real part is positive infinity and whose imaginary part is zero with the same sign as the imaginary part of x. Otherwise, the function returns x.

creal

double creal(double _Complex x);
float creal(float _Complex x);
long double creal(long double _Complex x);

The function returns the real part of x.

erf

double erf(double x);
float erf(float x);
long double erf(long double x);

The function returns the error function of x.

erfc

double erfc(double x);
float erfc(float x);
long double erfc(long double x);

The function returns the complementary error function of x.

exp

double exp(double x);
float exp(float x);
long double exp(long double x);
double _Complex exp(double _Complex x);
float _Complex exp(float _Complex x);
long double _Complex exp(long double _Complex x);

The function returns the exponential of x, e^x.

exp2

double exp2(double x);
float exp2(float x);
long double exp2(long double x);

The function returns two raised to the power x, 2^x.

expm1

double expm1(double x);
float expm1(float x);
long double expm1(long double x);

The function returns one less than the exponential function of x, e^x - 1.

fabs

double fabs(double x);
float fabs(float x);
long double fabs(long double x);
double fabs(double _Complex x);
float fabs(float _Complex x);
long double fabs(long double _Complex x);

The function returns the magnitude of x, |x|.

fdim

double fdim(double x, double y);
float fdim(float x, float y);
long double fdim(long double x, long double y);

The function returns the larger of x - y and zero.

floor

double floor(double x);
float floor(float x);
long double floor(long double x);

The function returns the largest integer value not greater than x.

fma

double fma(double x, double y, double z);
float fma(float x, float y, float z);
long double fma(long double x, long double y, long double z);

The function returns x * y + z, to arbitrary intermediate precision.

fmax

double fmax(double x, double y);
float fmax(float x, float y);
long double fmax(long double x, long double y);

The function returns the larger (more positive) of x and y.

fmin

double fmin(double x, double y);
float fmin(float x, float y);
long double fmin(long double x, long double y);

The function returns the smaller (more negative) of x and y.

fmod

double fmod(double x, double y);
float fmod(float x, float y);
long double fmod(long double x, long double y);

The function returns the remainder of x/y, which is defined as follows:

  • If y is zero, the function either reports a domain error or simply returns zero.
  • Otherwise, if 0 <= x, the value is x - i*y for some integer i such that:
    0 <= i*|y| <= x < (i + 1)*|y|
  • Otherwise, x < 0 and the value is x - i*y for some integer i such that:
    i*|y| <= x < (i + 1)*|y| <= 0

frexp

double frexp(double x, int *pexp);
float frexp(float x, int *pexp);
long double frexp(long double x, int *pexp);

The function determines a fraction frac and an exponent integer ex that represent the value of x. It returns the value frac and stores the integer ex in *pexp, such that:

  • |frac| is in the interval [1/2, 1) or is zero
  • x == frac * 2^ex

If x is zero, *pexp is also zero.

hypot

double hypot(double x, double y);
float hypot(float x, float y);
long double hypot(long double x, long double y);

The function returns the square root of x^2 + y^2.

ilogb

int ilogb(double x);
int ilogb(float x);
int ilogb(long double x);

The function returns:

  • for x not-a-number (NaN), the value of the macro FP_ILOGBNAN
  • for x equal to zero, the value of the macro FP_ILOGB0
  • for x equal to positive or negative infinity, the value of the macro INT_MAX

Otherwise, it returns (int)logb(x).

ldexp

double ldexp(double x, int ex);
float ldexp(float x, int ex);
long double ldexp(long double x, int ex);

The function returns x * 2^ex.

lgamma

double lgamma(double x);
float lgamma(float x);
long double lgamma(long double x);

The function returns the natural logarithm of the absolute value of the gamma function of x.

llrint

long long llrint(double x);
long long llrint(float x);
long long llrint(long double x);

The function returns the nearest long long integer to x, consistent with the current rounding mode. It raises an invalid floating-point exception if the magnitude of the rounded value is too large to represent. And it raises an inexact floating-point exception if the return value does not equal x.

llround

long long llround(double x);
long long llround(float x);
long long llround(long double x);

The function returns the nearest long long integer to x, rounding halfway values away from zero, regardless of the current rounding mode.

log

double log(double x);
float log(float x);
long double log(long double x);
double _Complex log(double _Complex x);
float _Complex log(float _Complex x);
long double _Complex log(long double _Complex x);

The function returns the natural logarithm of x.

log10

double log10(double x);
float log10(float x);
long double log10(long double x);

The function returns the base-10 logarithm of x. A domain error occurs if x < 0.

log1p

double log1p(double x);
float log1p(float x);
long double log1p(long double x);

The function returns the natural logarithm of 1 + x. A domain error occurs if x < -1.

log2

double log2(double x);
float log2(float x);
long double log2(long double x);

The function returns the base-2 logarithm of x. A domain error occurs if x < 0.

logb

double logb(double x);
float logb(float x);
long double logb(long double x);

The function determines an integer exponent ex and a fraction frac that represent the value of a finite x. It returns the value ex such that:

  • x == frac * ex^FLT_RADIX
  • |frac| is in the interval [1, FLT_RADIX)

A domain error may occur if x is zero.

lrint

long lrint(double x);
long lrint(float x);
long lrint(long double x);

The function returns the nearest long integer to x, consistent with the current rounding mode. It raises an invalid floating-point exception if the magnitude of the rounded value is too large to represent. And it raises an inexact floating-point exception if the return value does not equal x.

lround

long lround(double x);
long lround(float x);
long lround(long double x);

The function returns the nearest long integer to x, rounding halfway values away from zero, regardless of the current rounding mode.

modf

double modf(double x, double *pint);
float modf(float x, float *pint);
long double modf(long double x, long double *pint);

The function determines an integer i plus a fraction frac that represent the value of x. It returns the value frac and stores the integer i in *pint, such that:

  • x == frac + i
  • |frac| is in the interval [0, 1)
  • both frac and i have the same sign as x

nearbyint

double nearbyint(double x);
float nearbyint(float x);
long double nearbyint(long double x);

The function returns x rounded to the nearest integer, using the current rounding mode but without raising an inexact floating-point exception.

nextafter

double nextafter(double x, double y);
float nextafter(float x, float y);
long double nextafter(long double x, long double y);

The function returns:

  • if x < y, the next representable value after x
  • if x == y, y
  • if x > y, the next representable value before x

nexttoward

double nexttoward(double x, long double y);
float nexttoward(float x, long double y);
long double nexttoward(long double x, long double y);

The function returns:

  • if x < y, the next representable value after x
  • if x == y, y
  • if x > y, the next representable value before x

pow

double pow(double x, double y);
float pow(float x, float y);
long double pow(long double x, long double y);
double _Complex pow(double _Complex x, double _Complex y);
float _Complex pow(float _Complex x, float _Complex y);
long double _Complex pow(long double _Complex x, long double _Complex y);

The function returns x raised to the power y, x^y.

remainder

double remainder(double x, double y);
float remainder(float x, float y);
long double remainder(long double x, long double y);

The function effectively returns remquo(x, y, &temp), where temp is a temporary object of type int local to the function.

remquo

double remquo(double x, double y, int *pquo);
float remquo(float x, float y, int *pquo);
long double remquo(long double x, long double y, int *pquo);

The function computes the remainder rem == x - n*y, where n == x/y rounded to the nearest integer, or to the nearest even integer if |n - x/y| == 1/2. If rem is zero, it has the same sign as x. A domain error occurs if y is zero.

The function stores in *pquo at least three of the low-order bits of |x/y|, negated if x/y < 0. It returns rem.

rint

double rint(double x);
float rint(float x);
long double rint(long double x);

The function returns x rounded to the nearest integer, using the current rounding mode. It may raise an inexact floating-point exception if the return value does not equal x.

round

double round(double x);
float round(float x);
long double round(long double x);

The function returns x rounded to the nearest integer n, or to the value with larger magnitude if |n - x| == 1/2.

scalbln

double scalbln(double x, long ex);
float scalbln(float x, long ex);
long double scalbln(long double x, long ex);

The function returns x * FLT_RADIX^ex.

scalbn

double scalbn(double x, int ex);
float scalbn(float x, int ex);
long double scalbn(long double x, int ex);

The function returns x * FLT_RADIX^ex.

sin

double sin(double x);
float sin(float x);
long double sin(long double x);
double _Complex sin(double _Complex x);
float _Complex sin(float _Complex x);
long double _Complex sin(long double _Complex x);

The function returns the sine of x.

sinh

double sinh(double x);
float sinh(float x);
long double sinh(long double x);
double _Complex sinh(double _Complex x);
float _Complex sinh(float _Complex x);
long double _Complex sinh(long double _Complex x);

The function returns the hyperbolic sine of x.

sqrt

double sqrt(double x);
float sqrt(float x);
long double sqrt(long double x);
double _Complex sqrt(double _Complex x);
float _Complex sqrt(float _Complex x);
long double _Complex sqrt(long double _Complex x);

The function returns the real square root of x, x^(1/2).

tan

double tan(double x);
float tan(float x);
long double tan(long double x);
double _Complex tan(double _Complex x);
float _Complex tan(float _Complex x);
long double _Complex tan(long double _Complex x);

The function returns the tangent of x.

tanh

double tanh(double x);
float tanh(float x);
long double tanh(long double x);
double _Complex tanh(double _Complex x);
float _Complex tanh(float _Complex x);
long double _Complex tanh(long double _Complex x);

The function returns the hyperbolic tangent of x.

tgamma

double tgamma(double x);
float tgamma(float x);
long double tgamma(long double x);

The function computes the gamma function of x. A domain error occurs if x is a negative integer.

trunc

double trunc(double x);
float trunc(float x);
long double trunc(long double x);

The function returns x rounded to the nearest integer n not larger in magnitude than x (toward zero).


See also the Table of Contents and the Index.

Copyright © 1992-2006 by P.J. Plauger. All rights reserved.

Last modified: 2013-12-21

comments powered by Disqus