411 REFERENCE · Wikibooks
C programming/tgmath.h
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
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.http://www.opengroup.org/onlinepubs/009695399/basedefs/tgmath.h.html
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 has been called this way:
tan((float)x)
then this expression will have a type floathttp://manpages.ubuntu.com/manpages/hardy/man7/tgmath.h.7posix.html.
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.http://www.qnx.com/developers/docs/6.4.1/dinkum_en/c99/tgmath.html.
Dependency Graph
The flowchart below shows the Dependency graph for tgmath.h.http://www-zeuthen.desy.de/apewww/APE/software/nlibc/html/tgmath_8h.html
center|400px|thumb|Dependency Graph
Functions from math.h
The functions listed below handle real arguments and return real output only. If complex arguments are passed to these functions, NaN or other special numbers are returned http://pubs.opengroup.org/onlinepubs/009604599/basedefs/tgmath.h.html.
Name Descriptionacos inverse cosineasin inverse sineatan one-parameter inverse tangentatan2 two-parameter inverse tangentceil ceiling, the smallest integer not less than parametercos cosinecosh hyperbolic cosineexp exponential functionfabs absolute value of a floating-point numberfloor floor, the largest integer not greater than parameterfmod floating-point remainderfrexp decomposes a number into significand and a power of 2ldexp scale floating-point number by exponent (see article)log natural logarithmlog10 base-10 logarithmmodf(x,p) returns fractional part of x and stores integral part where pointer p points topow(x,y) raise x to the power of y, xysin sinesinh hyperbolic sinesqrt square root, returns non-negative square-root of the numbertan tangenttanh hyperbolic tangentisgreater If x > y, it returns 1, otherwise 0isnan(x) returns 1, if x is NaN (Not A Number)islessequal returns 1, if x is less than or equal to y, otherwise returns 0isinf(x) represents whether x is infinitesignbit represents whether x is negative (returns 8 if x is less than 0, otherwise returns 0)
Functions from complex.h
The functions defined below handle complex parameters, but do not return complex output.
Name Descriptioncacos inverse cosinecasin inverse sinecatan one-parameter inverse tangentcatan2 two-parameter inverse tangentcceil ceiling, the smallest integer not less than parameterccos cosineccosh hyperbolic cosinecexp exponential functioncabs absolute value of a complex floating-point numbercfloor floor, the largest integer not greater than parametercfmod floating-point remaindercfrexp decomposes a number into significand and a power of 2cldexp scale floating-point number by exponent (see article)clog natural logarithmclog10 base-10 logarithmcmodf(x,p) returns fractional part of x and stores integral part where pointer p points tocpow(x,y) raise x to the power of y, xycsin sinecsinh hyperbolic sinecsqrt square root, returns non-negative square-root of the numberctan tangentctanh hyperbolic tangent
Usage example
The code below illustrates usage of ,atan function defined in tgmath.h, which computes the inverse of tangent of a number defined in the domain of tangent.
#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(Not a Number), whereas, csqrt returns 0.000000. Such differences may be noticed in other functions also.
Rationale
This header file is mainly included while calculating mathematical functions.As it includes both math.h and complex.h, the problem arising due to inconsistent input is solved. Including any of the header files, individually, involves inconsistent outputs for some inputs.
References
External Links
Documentation http://clang.llvm.org/doxygen/tgmath_8h-source.html
http://www.unix.com/man-page/OpenSolaris/3head/tgmath.h/
http://publib.boulder.ibm.com/infocenter/zos/v1r11/index.jsp?topic=/com.ibm.zos.r11.bpxbd00/tgmathh.htm
nlib.h http://read.pudn.com/downloads170/sourcecode/math/784268/nlib.h__.htm