1 /* Declarations for math functions.
2 Copyright (C) 1991, 92, 93, 95, 96, 97 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 * ISO C Standard: 4.5 MATHEMATICS <math.h>
31 /* Get machine-dependent HUGE_VAL value (returned on overflow).
32 On all IEEE754 machines, this is +Infinity. */
33 #include <bits/huge_val.h>
35 /* Get machine-dependent NAN value (returned for some domain errors). */
37 # include <bits/nan.h>
41 /* The file <bits/mathcalls.h> contains the prototypes for all the
42 actual math functions. These macros are used for those prototypes,
43 so we can easily declare each function as both `name' and `__name',
44 and can declare the float versions `namef' and `__namef'. */
46 #define __MATHCALL(function,suffix, args) \
47 __MATHDECL (_Mdouble_,function,suffix, args)
48 #define __MATHDECL(type, function,suffix, args) \
49 __MATHDECL_1(type, function,suffix, args); \
50 __MATHDECL_1(type, __CONCAT(__,function),suffix, args)
51 #define __MATHCALLX(function,suffix, args, attrib) \
52 __MATHDECLX (_Mdouble_,function,suffix, args, attrib)
53 #define __MATHDECLX(type, function,suffix, args, attrib) \
54 __MATHDECL_1(type, function,suffix, args) __attribute__ (attrib); \
55 __MATHDECL_1(type, __CONCAT(__,function),suffix, args) __attribute__ (attrib)
56 #define __MATHDECL_1(type, function,suffix, args) \
57 extern type __MATH_PRECNAME(function,suffix) args
59 #define _Mdouble_ double
60 #define __MATH_PRECNAME(name,r) __CONCAT(name,r)
61 #include <bits/mathcalls.h>
63 #undef __MATH_PRECNAME
65 #if defined __USE_MISC || defined __USE_ISOC9X
68 /* Include the file of declarations again, this time using `float'
69 instead of `double' and appending f to each function name. */
72 # define _Mfloat_ float
74 #define _Mdouble_ _Mfloat_
76 # define __MATH_PRECNAME(name,r) name##f##r
78 # define __MATH_PRECNAME(name,r) name/**/f/**/r
80 #include <bits/mathcalls.h>
82 #undef __MATH_PRECNAME
84 #if __STDC__ - 0 || __GNUC__ - 0
85 /* Include the file of declarations again, this time using `long double'
86 instead of `double' and appending l to each function name. */
88 # ifndef _Mlong_double_
89 # define _Mlong_double_ long double
91 # define _Mdouble_ _Mlong_double_
93 # define __MATH_PRECNAME(name,r) name##l##r
95 # define __MATH_PRECNAME(name,r) name/**/l/**/r
97 # include <bits/mathcalls.h>
99 # undef __MATH_PRECNAME
101 #endif /* __STDC__ || __GNUC__ */
103 #endif /* Use misc or ISO C 9X. */
109 #if defined __USE_MISC || defined __USE_XOPEN || defined __USE_ISOC9X
110 /* This variable is used by `gamma' and `lgamma'. */
115 /* ISO C 9X defines some generic macros which work on any data type. */
118 /* Get the architecture specific values describing the floating-point
119 evaluation. The following symbols will get defined:
121 float_t floating-point type at least as wide as `float' used
122 to evaluate `float' expressions
123 double_t floating-point type at least as wide as `double' used
124 to evaluate `double' expressions
128 0 if `float_t' is `float' and `double_t' is `double'
129 1 if `float_t' and `double_t' are `double'
130 2 if `float_t' and `double_t' are `long double'
131 else `float_t' and `double_t' are unspecified
133 INFINITY representation of the infinity value of type `float_t'
138 If defined it indicates that the the `fma' function
139 generally executes about as fast as a multiply and an add.
140 This macro is defined only iff the `fma' function is
141 implemented directly with a hardware multiply-add instructions.
143 FP_ILOGB0 Expands to a value returned by `ilogb (0.0)'.
144 FP_ILOGBNAN Expands to a value returned by `ilogb (NAN)'.
147 # include <bits/mathdef.h>
149 /* All floating-point numbers can be put in one of these categories. */
153 # define FP_NAN FP_NAN
155 # define FP_INFINITE FP_INFINITE
157 # define FP_ZERO FP_ZERO
159 # define FP_SUBNORMAL FP_SUBNORMAL
161 # define FP_NORMAL FP_NORMAL
164 /* Return number of classification appropriate for X. */
165 # define fpclassify(x) \
166 (sizeof (x) == sizeof (float) ? \
168 : sizeof (x) == sizeof (double) ? \
169 __fpclassify (x) : __fpclassifyl (x))
171 /* Return nonzero value if sign of X is negative. */
172 # define signbit(x) \
173 (sizeof (x) == sizeof (float) ? \
175 : sizeof (x) == sizeof (double) ? \
176 __signbit (x) : __signbitl (x))
178 /* Return nonzero value if X is not +-Inf or NaN. */
179 # define isfinite(x) \
180 (sizeof (x) == sizeof (float) ? \
182 : sizeof (x) == sizeof (double) ? \
183 __finite (x) : __finitel (x))
185 /* Return nonzero value if X is neither zero, subnormal, Inf, nor NaN. */
186 # define isnormal(x) (fpclassify (x) == FP_NORMAL)
188 /* Return nonzero value if X is a NaN. We could use `fpclassify' but
189 we already have this functions `__isnan' and it is faster. */
191 (sizeof (x) == sizeof (float) ? \
193 : sizeof (x) == sizeof (double) ? \
194 __isnan (x) : __isnanl (x))
196 #endif /* Use ISO C 9X. */
199 /* Support for various different standard error handling behaviors. */
201 typedef enum { _IEEE_
= -1, _SVID_
, _XOPEN_
, _POSIX_
} _LIB_VERSION_TYPE
;
203 /* This variable can be changed at run-time to any of the values above to
204 affect floating point error handling behavior (it may also be necessary
205 to change the hardware FPU exception settings). */
206 extern _LIB_VERSION_TYPE _LIB_VERSION
;
211 /* In SVID error handling, `matherr' is called with this description
212 of the exceptional condition.
214 We have a problem when using C++ since `exception' is reserved in
230 extern int __matherr
__P ((struct __exception
*__exc
));
231 extern int matherr
__P ((struct __exception
*__exc
));
233 extern int __matherr
__P ((struct exception
*__exc
));
234 extern int matherr
__P ((struct exception
*__exc
));
237 # define X_TLOSS 1.41484755040568800000e+16
239 /* Types of exceptions in the `type' field. */
247 /* SVID mode specifies returning this large value instead of infinity. */
248 # define HUGE FLT_MAX
249 # include <float.h> /* Defines FLT_MAX. */
254 /* X/Open wants another strange constant. */
255 # define MAXFLOAT FLT_MAX
264 /* Some useful constants. */
265 # define M_E _Mldbl(2.7182818284590452354) /* e */
266 # define M_LOG2E _Mldbl(1.4426950408889634074) /* log_2 e */
267 # define M_LOG10E _Mldbl(0.43429448190325182765) /* log_10 e */
268 # define M_LN2 _Mldbl(0.69314718055994530942) /* log_e 2 */
269 # define M_LN10 _Mldbl(2.30258509299404568402) /* log_e 10 */
270 # define M_PI _Mldbl(3.14159265358979323846) /* pi */
271 # define M_PI_2 _Mldbl(1.57079632679489661923) /* pi/2 */
272 # define M_PI_4 _Mldbl(0.78539816339744830962) /* pi/4 */
273 # define M_1_PI _Mldbl(0.31830988618379067154) /* 1/pi */
274 # define M_2_PI _Mldbl(0.63661977236758134308) /* 2/pi */
275 # define M_2_SQRTPI _Mldbl(1.12837916709551257390) /* 2/sqrt(pi) */
276 # define M_SQRT2 _Mldbl(1.41421356237309504880) /* sqrt(2) */
277 # define M_SQRT1_2 _Mldbl(0.70710678118654752440) /* 1/sqrt(2) */
281 /* Our constants might specify more precision than `double' can represent.
282 Use `long double' constants in standard and GNU C, where they are
283 supported and the cast to `double'.
285 If the constants are use in code which does not use prototypes, one
286 might get problems if a function takes a `double' argument and any
287 of the constants are provided as the argument. In this case, cast
288 the argument to `double'.
290 Please note we define the macro even if the constants are not defined.
291 This helps us to use the macros in other places. */
292 #if __STDC__ - 0 || __GNUC__ - 0
293 # define _Mldbl(x) x##L
294 #else /* Traditional C. */
296 #endif /* Standard or GNU C. */
299 /* Get machine-dependent inline versions (if there are any). */
301 # include <bits/mathinline.h>
306 /* ISO C 9X defines some macros to compare number while taking care
307 for unordered numbers. Since many FPUs provide special
308 instructions to support these operations and these tests are
309 defined in <bits/mathinline.h>, we define the generic macros at
312 /* Return nonzero value if X is greater than Y. */
314 # define isgreater(x, y) \
316 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
317 !isunordered (__x, __y) && __x > __y; }))
320 /* Return nonzero value if X is greater than or equal to Y. */
321 # ifndef isgreaterequal
322 # define isgreaterequal(x, y) \
324 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
325 !isunordered (__x, __y) && __x >= __y; }))
328 /* Return nonzero value if X is less than Y. */
330 # define isless(x, y) \
332 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
333 !isunordered (__x, __y) && __x < __y; }))
336 /* Return nonzero value if X is less than or equal to Y. */
338 # define islessequal(x, y) \
340 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
341 !isunordered (__x, __y) && __x <= __y; }))
344 /* Return nonzero value if either X is less than Y or Y is less than X. */
345 # ifndef islessgreater
346 # define islessgreater(x, y) \
348 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
349 !isunordered (__x, __y) && (__x < __y || __y < __x); }))
352 /* Return nonzero value if arguments are unordered. */
354 # define isunordered(x, y) \
356 ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
357 fpclassify (__x) == FP_NAN || fpclassify (__y) == FP_NAN; }))