Jump to content

Tgmath.h: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Compilation using gcc
(38 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{merge to|C mathematical operations|discuss=Talk:C_standard_library#Pages_for_each_function_and_WP:NOTMANUAL|date=October 2011}}
{{lowercase|title=tgmath.h}}
{{lowercase|title=tgmath.h}}
{{C_Standard_library}}
{{C_Standard_library}}


'''tgmath.h''' is a [[Standard C]] header that defines many type-generic [[macro]]s that can be used for a variety of mathematical operations. This header also includes <code>[[math.h]]</code> and <code>[[complex.h]]</code>. For all of the functions in the <code>math.h</code> and <code>complex.h</code> headers that do not have an f (float) or l (long double) suffix, and whose corresponding type is double (with the exception of <code>modf()</code>), there is a corresponding macro.<ref>http://www.opengroup.org/onlinepubs/009695399/basedefs/tgmath.h.html</ref>
'''tgmath.h''' is a [[Standard C]] header that defines many type-generic [[macro]]s that can be used for a variety of mathematical operations. This header also includes <code>[[math.h]]</code> and <code>[[complex.h]]</code>. For all of the [[Function (computer science)|functions]] in the <code>math.h</code> and <code>complex.h</code> headers that do not have an f ([[Floating point|float]]) or l ([[Long double|long double]]) suffix, and whose corresponding type is [[Double|double]] (with the exception of <code>modf()</code>), there is a corresponding macro.<ref>http://www.opengroup.org/onlinepubs/009695399/basedefs/tgmath.h.html</ref>


== Type-generic macro ==
== Functions from <code>math.h</code>==
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 (computer science)|int]] data type but [[tangent|tan]] has been called this way:<br />
tan((float)x)<br />
then this [[Expression (computer science)|expression]] will have a type [[floating point|float]]<ref>http://manpages.ubuntu.com/manpages/hardy/man7/tgmath.h.7posix.html</ref>.<br />
Also, if any one of the [[Parameter (computer programming)|parameters]] or [[Argument (computer science)|arguments]] of a type-generic macro is [[Complex number|complex]], it will call a complex function, otherwise a [[Real number|real]] function will be called. The type of function that is called, ultimately depends upon the final converted type of parameter.<ref>http://www.qnx.com/developers/docs/6.4.1/dinkum_en/c99/tgmath.html</ref>.

== Dependency Graph ==
Dependency graph for tgmath.h has been shown in the adjacent image.<ref>http://www-zeuthen.desy.de/apewww/APE/software/nlibc/html/tgmath_8h.html</ref>
[[File:Dependency Graph for tgmath.h.PNG|thumb|Dependency Graph]]

== Functions from <code>math.h <ref>http://pubs.opengroup.org/onlinepubs/009604599/basedefs/tgmath.h.html</ref></code>==
{|class="wikitable"
{|class="wikitable"
|-
|-
Line 29: Line 40:
|<code>floor</code> || [[floor (programming)|floor]], the largest integer not greater than parameter
|<code>floor</code> || [[floor (programming)|floor]], the largest integer not greater than parameter
|-
|-
|<code>fmod</code> || [[fmod|floating-point remainder]]
|<code>fmod</code> || [[floating-point]] [[remainder]]
|-
|-
|<code>frexp</code> || break floating-point number down into [[significand|mantissa]] and [[exponent]]
|<code>frexp</code> || break [[floating-point]] number down into [[significand|mantissa]] and [[exponent]]
|-
|-
|<code>ldexp</code> || scale floating-point number by exponent (see [[ldexp|article]])
|<code>ldexp</code> || scale [[floating-point]] number by [[exponent]] (see [[ldexp|article]])
|-
|-
|<code>log</code> || [[natural logarithm]]
|<code>log</code> || [[natural logarithm]]
Line 39: Line 50:
|<code>log10</code> || [[common logarithm|base-10 logarithm]]
|<code>log10</code> || [[common logarithm|base-10 logarithm]]
|-
|-
|<code>modf(''x'',''p'')</code> || returns [[fractional part]] of ''x'' and stores [[floor and ceiling functions|integral part]] where pointer ''p'' points to
|<code>modf(''x'',''p'')</code> || returns [[fractional part]] of ''x'' and stores [[floor and ceiling functions|integral part]] where [[pointer (computing)|pointer]] ''p'' points to
|-
|-
|<code>pow(''x'',''y'')</code> || raise ''x'' to the [[exponentiation|power]] of ''y'', ''x<sup>y</sup>''
|<code>pow(''x'',''y'')</code> || raise ''x'' to the [[exponentiation|power]] of ''y'', ''x<sup>y</sup>''
Line 47: Line 58:
|<code>sinh</code> || [[hyperbolic sine]]
|<code>sinh</code> || [[hyperbolic sine]]
|-
|-
|<code>sqrt</code> || [[square root]]
|<code>sqrt</code> || [[square root]], returns non-negative square-root of the number
|-
|-
|<code>tan</code> || [[tangent (trigonometric function)|tangent]]
|<code>tan</code> || [[tangent (trigonometric function)|tangent]]
Line 54: Line 65:
|}
|}


== Functions from complex.h ==
== Functions from <code>complex.h </code><ref>http://pubs.opengroup.org/onlinepubs/009604599/basedefs/tgmath.h.html</ref>==
{|class="wikitable"
{|class="wikitable"
|-
|-
Line 83: Line 94:
|<code>cfrexp</code> || break floating-point number down into [[significand|mantissa]] and [[exponent]]
|<code>cfrexp</code> || break floating-point number down into [[significand|mantissa]] and [[exponent]]
|-
|-
|<code>cldexp</code> || scale floating-point number by exponent (see [[ldexp|article]])
|<code>cldexp</code> || scale [[floating-point]] number by [[exponent]] (see [[ldexp|article]])
|-
|-
|<code>clog</code> || [[natural logarithm]]
|<code>clog</code> || [[natural logarithm]]
Line 89: Line 100:
|<code>clog10</code> || [[common logarithm|base-10 logarithm]]
|<code>clog10</code> || [[common logarithm|base-10 logarithm]]
|-
|-
|<code>cmodf(''x'',''p'')</code> || returns fractional part of ''x'' and stores integral part where pointer ''p'' points to
|<code>cmodf(''x'',''p'')</code> || returns fractional part of ''x'' and stores integral part where [[pointer (computing)|pointer]] ''p'' points to
|-
|-
|<code>cpow(''x'',''y'')</code> || raise ''x'' to the power of ''y'', ''x<sup>y</sup>''
|<code>cpow(''x'',''y'')</code> || raise ''x'' to the power of ''y'', ''x<sup>y</sup>''
Line 104: Line 115:
|}
|}


== Compilation using gcc==
== 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|nan]], whereas, csqrt returns 0.000000. Such differences may be noticed in other functions also.
In order to successfully compile a code using a gcc compiler, -lm has to be linked while compiling. For example, consider the following code:<br />
<nowiki>#</nowiki>include <stdio.h><br />
<nowiki>#</nowiki>include <math.h><br />
int main()<br />
{<br />
float angle, sine;<br />
scanf("%f", &angle);<br />
sine = sin(angle);<br />
printf("%f", sine);<br />
return 0;<br />
}<br />
<br />
If it is compiled using the command gcc <filename>, an error is reported saying there is an undefined reference to 'sin'. In order to compile it successfully, it has to be compiled as follows:<br />
gcc <filename> -lm


==References==
==References==
Line 126: Line 124:
[[Category:C standard library headers]]
[[Category:C standard library headers]]


== See also ==
* [[math.h]]
* [[complex.h]]
* [[Trigonometric Functions]]
* [[Hyperbolic trigonometric functions]]


== External Links ==
{{Compu-lang-stub}}
* 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

Revision as of 05:41, 7 October 2011

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 [6]

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

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