Update.
[glibc.git] / math / tgmath.h
blob4c7d957b37c9c1212d2043e1cfdadabf5bf64d36
1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
20 * ISO C 9X Standard: 7.9 Type-generic math <tgmath.h>
23 #ifndef _TGMATH_H
24 #define _TGMATH_H 1
26 /* Include the needed headers. */
27 #include <math.h>
28 #include <complex.h>
31 /* Since `complex' is currently not really implemented in most C compilers
32 and if it is implemented, the implementations differ. This makes it
33 quite difficult to write a generic implementation of this header. We
34 do not try this for now and instead concentrate only on GNU CC. Once
35 we have more information support for other compilers might follow. */
37 #if defined __GNUC__ && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
39 /* We have two kinds of generic macros: to support functions which are
40 only defined on real valued parameters and those which are defined
41 for complex functions as well. */
42 # define __TGMATH_UNARY_REAL_ONLY(Val, Fct) \
43 (__extension__ (sizeof (__real__ (Val)) == sizeof (long double) \
44 ? Fct##l (Val) \
45 : (sizeof (__real__ (Val)) == sizeof (double) \
46 ? Fct (Val) \
47 : Fct##f (Val))))
49 # define __TGMATH_BINARY_FIRST_REAL_ONLY(Val1, Val2, Fct) \
50 (__extension__ (sizeof (__real__ (Val1)) == sizeof (long double) \
51 ? Fct##l (Val1, Val2) \
52 : (sizeof (__real__ (Val1)) == sizeof (double) \
53 ? Fct (Val1, Val2) \
54 : Fct##f (Val1, Val2))))
56 # define __TGMATH_BINARY_REAL_ONLY(Val1, Val2, Fct) \
57 (__extension__ (sizeof (Val1) == sizeof (long double) \
58 || sizeof (Val2) == sizeof (long double) \
59 ? Fct##l (Val1, Val2) \
60 : (sizeof (Val1) == sizeof (double) \
61 || sizeof (Val2) == sizeof (double) \
62 ? Fct (Val1, Val2) \
63 : Fct##f (Val1, Val2))))
65 # define __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY(Val1, Val2, Val3, Fct) \
66 (__extension__ (sizeof (Val1) == sizeof (long double) \
67 || sizeof (Val2) == sizeof (long double) \
68 ? Fct##l (Val1, Val2, Val3) \
69 : (sizeof (Val1) == sizeof (double) \
70 || sizeof (Val2) == sizeof (double) \
71 ? Fct (Val1, Val2, Val3) \
72 : Fct##f (Val1, Val2, Val3))))
74 # define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
75 (__extension__ (sizeof (__real__ (Val)) == sizeof (long double) \
76 ? (sizeof (__real__ (Val)) == sizeof (Val) \
77 ? Fct##l (Val) \
78 : Cfct##l (Val)) \
79 : (sizeof (__real__ (Val)) == sizeof (double) \
80 ? (sizeof (__real__ (Val)) == sizeof (Val) \
81 ? Fct (Val) \
82 : Cfct (Val)) \
83 : (sizeof (__real__ (Val)) == sizeof (Val) \
84 ? Fct##f (Val) \
85 : Cfct##f (Val)))))
87 # define __TGMATH_UNARY_IMAG_ONLY(Val, Fct) \
88 (__extension__ (sizeof (Val) == sizeof (__complex__ long double) \
89 ? Fct##l (Val) \
90 : (sizeof (Val) == sizeof (__complex__ double) \
91 ? Fct (Val) \
92 : Fct##f (Val))))
94 # define __TGMATH_BINARY_REAL_IMAG(Val1, Val2, Fct, Cfct) \
95 (__extension__ (sizeof (__real__ (Val1)) == sizeof (long double) \
96 || sizeof (__real__ (Val2)) == sizeof (long double) \
97 ? (sizeof (__real__ (Val1)) == sizeof (Val1) \
98 && sizeof (__real__ (Val2)) == sizeof (Val2) \
99 ? Fct##l (Val1, Val2) \
100 : Cfct##l (Val1, Val2)) \
101 : (sizeof (__real__ (Val1)) == sizeof (double) \
102 || sizeof (__real__ (Val2)) == sizeof (double) \
103 ? (sizeof (__real__ (Val1)) == sizeof (Val1) \
104 && sizeof (__real__ (Val2)) == sizeof (Val2) \
105 ? Fct (Val1, Val2) \
106 : Cfct (Val1, Val2)) \
107 : (sizeof (__real__ (Val1)) == sizeof (Val1) \
108 && sizeof (__real__ (Val2)) == sizeof (Val2) \
109 ? Fct##f (Val1, Val2) \
110 : Cfct##f (Val1, Val2)))))
111 #else
112 # error "Unsupported compiler; you cannot use <tgmath.h>"
113 #endif
116 /* Unary functions defined for real and complex values. */
119 /* Trigonometric functions. */
121 /* Arc cosine of X. */
122 #define acos(Val) __TGMATH_UNARY_REAL_IMAG (Val, acos, cacos)
123 /* Arc sine of X. */
124 #define asin(Val) __TGMATH_UNARY_REAL_IMAG (Val, asin, casin)
125 /* Arc tangent of X. */
126 #define atan(Val) __TGMATH_UNARY_REAL_IMAG (Val, atan, catan)
127 /* Arc tangent of Y/X. */
128 #define atan2(Val) __TGMATH_UNARY_REAL_ONLY (Val, atan2)
130 /* Cosine of X. */
131 #define cos(Val) __TGMATH_UNARY_REAL_IMAG (Val, cos, ccos)
132 /* Sine of X. */
133 #define sin(Val) __TGMATH_UNARY_REAL_IMAG (Val, sin, csin)
134 /* Tangent of X. */
135 #define tan(Val) __TGMATH_UNARY_REAL_IMAG (Val, tan, ctan)
138 /* Hyperbolic functions. */
140 /* Hyperbolic arc cosine of X. */
141 #define acosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, acosh, cacosh)
142 /* Hyperbolic arc sine of X. */
143 #define asinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, asinh, casinh)
144 /* Hyperbolic arc tangent of X. */
145 #define atanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, atanh, catanh)
147 /* Hyperbolic cosine of X. */
148 #define cosh(Val) __TGMATH_UNARY_REAL_IMAG (Val, cosh, ccosh)
149 /* Hyperbolic sine of X. */
150 #define sinh(Val) __TGMATH_UNARY_REAL_IMAG (Val, sinh, csinh)
151 /* Hyperbolic tangent of X. */
152 #define tanh(Val) __TGMATH_UNARY_REAL_IMAG (Val, tanh, ctanh)
155 /* Exponential and logarithmic functions. */
157 /* Exponential function of X. */
158 #define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
160 /* Break VALUE into a normalized fraction and an integral power of 2. */
161 #define frexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, frexp)
163 /* X times (two to the EXP power). */
164 #define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
166 /* Natural logarithm of X. */
167 #define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
169 /* Base-ten logarithm of X. */
170 #define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
172 /* Return exp(X) - 1. */
173 #define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
175 /* Return log(1 + X). */
176 #define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
178 /* Return the base 2 signed integral exponent of X. */
179 #define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
181 /* Compute base-2 exponential of X. */
182 #define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
184 /* Compute base-2 logarithm of X. */
185 #define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
188 /* Power functions. */
190 /* Return X to the Y power. */
191 #define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
193 /* Return the square root of X. */
194 #define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
196 /* Return `sqrt(X*X + Y*Y)'. */
197 #define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
199 /* Return the cube root of X. */
200 #define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
203 /* Nearest integer, absolute value, and remainder functions. */
205 /* Smallest integral value not less than X. */
206 #define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
208 /* Absolute value of X. */
209 #define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs)
211 /* Largest integer not greater than X. */
212 #define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
214 /* Floating-point modulo remainder of X/Y. */
215 #define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
217 /* Round X to integral valuein floating-point format using current
218 rounding direction, but do not raise inexact exception. */
219 #define nearbyint(Val) __TGMATH_UNARY_REAL_ONLY (Val, nearbyint)
221 /* Round X to nearest integral value, rounding halfway cases away from
222 zero. */
223 #define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
225 /* Round X to the integral value in floating-point format nearest but
226 not larger in magnitude. */
227 #define trunc(Val) __TGMATH_UNARY_REAL_ONLY (Val, trunc)
229 /* Compute remainder of X and Y and put in *QUO a value with sign of x/y
230 and magnitude congruent `mod 2^n' to the magnitude of the integral
231 quotient x/y, with n >= 3. */
232 #define remquo(Val1, Val2, Val3) \
233 __TGMATH_TERNARY_FIRST_SECOND_REAL_ONLY (Val1, Val2, Val3, remquo)
235 /* Round X to nearest integral value according to current rounding
236 direction. */
237 #define lrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, lrint)
238 #define llrint(Val) __TGMATH_UNARY_REAL_ONLY (Val, llrint)
240 /* Round X to nearest integral value, rounding halfway cases away from
241 zero. */
242 #define lround(Val) __TGMATH_UNARY_REAL_ONLY (Val, lround)
243 #define llround(Val) __TGMATH_UNARY_REAL_ONLY (Val, llround)
246 /* Return X with its signed changed to Y's. */
247 #define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
249 /* Error and gamma functions. */
250 #define erf(Val) __TGMATH_UNARY_REAL_ONLY (Val, erf)
251 #define erfc(Val) __TGMATH_UNARY_REAL_ONLY (Val, erfc)
252 #define gamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, gamma)
253 #define lgamma(Val) __TGMATH_UNARY_REAL_ONLY (Val, lgamma)
256 /* Return the integer nearest X in the direction of the
257 prevailing rounding mode. */
258 #define rint(Val) __TGMATH_UNARY_REAL_ONLY (Val, rint)
260 /* Return X + epsilon if X < Y, X - epsilon if X > Y. */
261 #define nextafter(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, nextafter)
262 #define nextafterx(Val1, Val2) \
263 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, nextafterx)
265 /* Return the remainder of integer divison X / Y with infinite precision. */
266 #define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
268 /* Return X times (2 to the Nth power). */
269 #define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
271 /* Return X times (2 to the Nth power). */
272 #define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
274 /* Return X times (2 to the Nth power). */
275 #define scalbln(Val1, Val2) \
276 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
278 /* Return the binary exponent of X, which must be nonzero. */
279 #define ilogb(Val) __TGMATH_UNARY_REAL_ONLY (Val, ilogb)
282 /* Return positive difference between X and Y. */
283 #define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
285 /* Return maximum numeric value from X and Y. */
286 #define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
288 /* Return minimum numeric value from X and Y. */
289 #define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
292 /* Absolute value, conjugates, and projection. */
294 /* Argument value of Z. */
295 #define carg(Val) __TGMATH_UNARY_IMAG_ONLY (Val, carg)
297 /* Complex conjugate of Z. */
298 #define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj)
300 /* Projection of Z onto the Riemann sphere. */
301 #define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj)
304 /* Decomposing complex values. */
306 /* Imaginary part of Z. */
307 #define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag)
309 /* Real part of Z. */
310 #define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal)
312 #endif /* tgmath.h */