]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/tgmath.h
Update.
[thirdparty/glibc.git] / math / tgmath.h
CommitLineData
dfd2257a
UD
1/* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
18
19/*
20 * ISO C 9X Standard: 7.9 Type-generic math <tgmath.h>
21 */
22
23#ifndef _TGMATH_H
24#define _TGMATH_H 1
25
26/* Include the needed headers. */
27#include <math.h>
28#include <complex.h>
29
30
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. */
36
37#if defined __GNUC__ && (__GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7)
38
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))))
48
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))))
55
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))))
64
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))))
73
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)))))
86
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))))
93
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
114
115
116/* Unary functions defined for real and complex values. */
117
118
119/* Trigonometric functions. */
120
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)
129
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)
136
137
138/* Hyperbolic functions. */
139
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)
146
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)
153
154
155/* Exponential and logarithmic functions. */
156
157/* Exponential function of X. */
158#define exp(Val) __TGMATH_UNARY_REAL_IMAG (Val, exp, cexp)
159
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)
162
163/* X times (two to the EXP power). */
164#define ldexp(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, ldexp)
165
166/* Natural logarithm of X. */
167#define log(Val) __TGMATH_UNARY_REAL_IMAG (Val, log, clog)
168
169/* Base-ten logarithm of X. */
170#define log10(Val) __TGMATH_UNARY_REAL_IMAG (Val, log10, __clog10)
171
172/* Return exp(X) - 1. */
173#define expm1(Val) __TGMATH_UNARY_REAL_ONLY (Val, expm1)
174
175/* Return log(1 + X). */
176#define log1p(Val) __TGMATH_UNARY_REAL_ONLY (Val, log1p)
177
178/* Return the base 2 signed integral exponent of X. */
179#define logb(Val) __TGMATH_UNARY_REAL_ONLY (Val, logb)
180
181/* Compute base-2 exponential of X. */
182#define exp2(Val) __TGMATH_UNARY_REAL_ONLY (Val, exp2)
183
184/* Compute base-2 logarithm of X. */
185#define log2(Val) __TGMATH_UNARY_REAL_ONLY (Val, log2)
186
187
188/* Power functions. */
189
190/* Return X to the Y power. */
191#define pow(Val1, Val2) __TGMATH_BINARY_REAL_IMAG (Val1, Val2, pow, cpow)
192
193/* Return the square root of X. */
194#define sqrt(Val) __TGMATH_UNARY_REAL_IMAG (Val, sqrt, csqrt)
195
196/* Return `sqrt(X*X + Y*Y)'. */
197#define hypot(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, hypot)
198
199/* Return the cube root of X. */
200#define cbrt(Val) __TGMATH_UNARY_REAL_ONLY (Val, cbrt)
201
202
203/* Nearest integer, absolute value, and remainder functions. */
204
205/* Smallest integral value not less than X. */
206#define ceil(Val) __TGMATH_UNARY_REAL_ONLY (Val, ceil)
207
208/* Absolute value of X. */
209#define fabs(Val) __TGMATH_UNARY_REAL_IMAG (Val, fabs, cabs)
210
211/* Largest integer not greater than X. */
212#define floor(Val) __TGMATH_UNARY_REAL_ONLY (Val, floor)
213
214/* Floating-point modulo remainder of X/Y. */
215#define fmod(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmod)
216
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)
220
221/* Round X to nearest integral value, rounding halfway cases away from
222 zero. */
223#define round(Val) __TGMATH_UNARY_REAL_ONLY (Val, round)
224
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)
228
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)
234
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)
239
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)
244
245
246/* Return X with its signed changed to Y's. */
247#define copysign(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, copysign)
248
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)
254
255
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)
259
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)
264
265/* Return the remainder of integer divison X / Y with infinite precision. */
266#define remainder(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, remainder)
267
268/* Return X times (2 to the Nth power). */
269#define scalb(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, scalb)
270
271/* Return X times (2 to the Nth power). */
272#define scalbn(Val1, Val2) __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbn)
273
274/* Return X times (2 to the Nth power). */
275#define scalbln(Val1, Val2) \
276 __TGMATH_BINARY_FIRST_REAL_ONLY (Val1, Val2, scalbln)
277
278/* Return the binary exponent of X, which must be nonzero. */
279#define ilogb(Val) __TGMATH_UNARY_REAL_ONLY (Val, ilogb)
280
281
282/* Return positive difference between X and Y. */
283#define fdim(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fdim)
284
285/* Return maximum numeric value from X and Y. */
286#define fmax(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmax)
287
288/* Return minimum numeric value from X and Y. */
289#define fmin(Val1, Val2) __TGMATH_BINARY_REAL_ONLY (Val1, Val2, fmin)
290
291
292/* Absolute value, conjugates, and projection. */
293
294/* Argument value of Z. */
295#define carg(Val) __TGMATH_UNARY_IMAG_ONLY (Val, carg)
296
297/* Complex conjugate of Z. */
298#define conj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, conj)
299
300/* Projection of Z onto the Riemann sphere. */
301#define cproj(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cproj)
302
303
304/* Decomposing complex values. */
305
306/* Imaginary part of Z. */
307#define cimag(Val) __TGMATH_UNARY_IMAG_ONLY (Val, cimag)
308
309/* Real part of Z. */
310#define creal(Val) __TGMATH_UNARY_IMAG_ONLY (Val, creal)
311
312#endif /* tgmath.h */