Jump to content

tgmath.h

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jobin RV (talk | contribs) at 09:20, 11 October 2011 (Type-generic macro). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

tgmath.h is a Standard C header that defines many type-generic macros that can be used for a variety of mathematical operations. This header also includes math.h and complex.h. For all of the functions in the math.h and complex.h headers that do not have an f (float) or l (long double) suffix, and whose corresponding type is double (with the exception of modf()), there is a corresponding macro.[1]

Type-generic macro

A type generic macro is something which allows calling a function whose type is determined by the type of argument in the macro. This means, for example, x is declared as an int data type but tan has been called this way:

 tan((float)x)


then this expression will have a type float[2].
Also, if any one of the parameters or arguments of a type-generic macro is complex, it will call a complex function, otherwise a real function will be called. The type of function that is called, ultimately depends upon the final converted type of parameter.[3].

Dependency Graph

Dependency graph for tgmath.h has been shown in the adjacent image.[4]

File:Dependency Graph for tgmath.h.PNG
Dependency Graph

Functions from math.h [5]

Name Description
acos inverse cosine
asin inverse sine
atan one-parameter inverse tangent
atan2 two-parameter inverse tangent
ceil ceiling, the smallest integer not less than parameter
cos cosine
cosh hyperbolic cosine
exp exponential function
fabs absolute value (of a floating-point number)
floor floor, the largest integer not greater than parameter
fmod floating-point remainder
frexp break floating-point number down into mantissa and exponent
ldexp scale floating-point number by exponent (see article)
log natural logarithm
log10 base-10 logarithm
modf(x,p) returns fractional part of x and stores integral part where pointer p points to
pow(x,y) raise x to the power of y, xy
sin sine
sinh hyperbolic sine
sqrt square root, returns non-negative square-root of the number
tan tangent
tanh hyperbolic tangent

Functions from complex.h [5]

Name Description
cacos inverse cosine
casin inverse sine
catan one-parameter inverse tangent
catan2 two-parameter inverse tangent
cceil ceiling, the smallest integer not less than parameter
ccos cosine
ccosh hyperbolic cosine
cexp exponential function
cfabs absolute value (of a floating-point number)
cfloor floor, the largest integer not greater than parameter
cfmod floating-point remainder
cfrexp break floating-point number down into mantissa and exponent
cldexp scale floating-point number by exponent (see article)
clog natural logarithm
clog10 base-10 logarithm
cmodf(x,p) returns fractional part of x and stores integral part where pointer p points to
cpow(x,y) raise x to the power of y, xy
csin sine
csinh hyperbolic sine
csqrt square root
ctan tangent
ctanh hyperbolic tangent

Usage example

#include <stdio.h>
#include <tgmath.h>
int main()
{
        float ang, ans;
        scanf("%f", &ang);
        ans = atan(ang);
        printf("%0.3f\n", ans);
        return 0;
}

Notable differences

The similar functions defined here have notable difference when it comes to return value of "tricky" numbers. For example, using sqrt to compute square root of -25 returns -nan, whereas, csqrt returns 0.000000. Such differences may be noticed in other functions also.

References

See also