Guarantee a definition of NAN.
[gnulib.git] / lib / math.in.h
1 /* A GNU-like <math.h>.
2
3    Copyright (C) 2002-2003, 2007, 2008 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program 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
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _GL_MATH_H
19
20 /* The include_next requires a split double-inclusion guard.  */
21 #@INCLUDE_NEXT@ @NEXT_MATH_H@
22
23 #ifndef _GL_MATH_H
24 #define _GL_MATH_H
25
26
27 /* The definition of GL_LINK_WARNING is copied here.  */
28
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34
35 /* POSIX allows platforms that don't support NAN.  But all major
36    machines in the past 15 years have supported something close to
37    IEEE NaN, so we define this unconditionally.  */
38 #ifndef NAN
39   /* The Compaq (ex-DEC) C 6.4 compiler chokes on the expression 0.0 / 0.0.  */
40 # ifdef __DECC
41 static float
42 _NaN ()
43 {
44   static float zero = 0.0f;
45   return zero / zero;
46 }
47 #  define NAN (_NaN())
48 # else
49 #  define NAN (0.0f / 0.0f)
50 # endif
51 #endif
52
53 /* Write x as
54      x = mantissa * 2^exp
55    where
56      If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
57      If x is zero: mantissa = x, exp = 0.
58      If x is infinite or NaN: mantissa = x, exp unspecified.
59    Store exp in *EXPPTR and return mantissa.  */
60 #if @GNULIB_FREXP@
61 # if @REPLACE_FREXP@
62 #  define frexp rpl_frexp
63 extern double frexp (double x, int *expptr);
64 # endif
65 #elif defined GNULIB_POSIXCHECK
66 # undef frexp
67 # define frexp(x,e) \
68     (GL_LINK_WARNING ("frexp is unportable - " \
69                       "use gnulib module frexp for portability"), \
70      frexp (x, e))
71 #endif
72
73
74 #if @GNULIB_MATHL@ || !@HAVE_DECL_ACOSL@
75 extern long double acosl (long double x);
76 #endif
77 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
78 # undef acosl
79 # define acosl(x) \
80     (GL_LINK_WARNING ("acosl is unportable - " \
81                       "use gnulib module mathl for portability"), \
82      acosl (x))
83 #endif
84
85
86 #if @GNULIB_MATHL@ || !@HAVE_DECL_ASINL@
87 extern long double asinl (long double x);
88 #endif
89 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
90 # undef asinl
91 # define asinl(x) \
92     (GL_LINK_WARNING ("asinl is unportable - " \
93                       "use gnulib module mathl for portability"), \
94      asinl (x))
95 #endif
96
97
98 #if @GNULIB_MATHL@ || !@HAVE_DECL_ATANL@
99 extern long double atanl (long double x);
100 #endif
101 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
102 # undef atanl
103 # define atanl(x) \
104     (GL_LINK_WARNING ("atanl is unportable - " \
105                       "use gnulib module mathl for portability"), \
106      atanl (x))
107 #endif
108
109
110 #if @GNULIB_CEILF@
111 # if @REPLACE_CEILF@
112 #  define ceilf rpl_ceilf
113 extern float ceilf (float x);
114 # endif
115 #elif defined GNULIB_POSIXCHECK
116 # undef ceilf
117 # define ceilf(x) \
118     (GL_LINK_WARNING ("ceilf is unportable - " \
119                       "use gnulib module ceilf for portability"), \
120      ceilf (x))
121 #endif
122
123 #if @GNULIB_CEILL@
124 # if @REPLACE_CEILL@
125 #  define ceill rpl_ceill
126 extern long double ceill (long double x);
127 # endif
128 #elif defined GNULIB_POSIXCHECK
129 # undef ceill
130 # define ceill(x) \
131     (GL_LINK_WARNING ("ceill is unportable - " \
132                       "use gnulib module ceill for portability"), \
133      ceill (x))
134 #endif
135
136
137 #if @GNULIB_MATHL@ || !@HAVE_DECL_COSL@
138 extern long double cosl (long double x);
139 #endif
140 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
141 # undef cosl
142 # define cosl(x) \
143     (GL_LINK_WARNING ("cosl is unportable - " \
144                       "use gnulib module mathl for portability"), \
145      cosl (x))
146 #endif
147
148
149 #if @GNULIB_MATHL@ || !@HAVE_DECL_EXPL@
150 extern long double expl (long double x);
151 #endif
152 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
153 # undef expl
154 # define expl(x) \
155     (GL_LINK_WARNING ("expl is unportable - " \
156                       "use gnulib module mathl for portability"), \
157      expl (x))
158 #endif
159
160
161 #if @GNULIB_FLOORF@
162 # if @REPLACE_FLOORF@
163 #  define floorf rpl_floorf
164 extern float floorf (float x);
165 # endif
166 #elif defined GNULIB_POSIXCHECK
167 # undef floorf
168 # define floorf(x) \
169     (GL_LINK_WARNING ("floorf is unportable - " \
170                       "use gnulib module floorf for portability"), \
171      floorf (x))
172 #endif
173
174 #if @GNULIB_FLOORL@
175 # if @REPLACE_FLOORL@
176 #  define floorl rpl_floorl
177 extern long double floorl (long double x);
178 # endif
179 #elif defined GNULIB_POSIXCHECK
180 # undef floorl
181 # define floorl(x) \
182     (GL_LINK_WARNING ("floorl is unportable - " \
183                       "use gnulib module floorl for portability"), \
184      floorl (x))
185 #endif
186
187
188 /* Write x as
189      x = mantissa * 2^exp
190    where
191      If x finite and nonzero: 0.5 <= |mantissa| < 1.0.
192      If x is zero: mantissa = x, exp = 0.
193      If x is infinite or NaN: mantissa = x, exp unspecified.
194    Store exp in *EXPPTR and return mantissa.  */
195 #if @GNULIB_FREXPL@ && @REPLACE_FREXPL@
196 # define frexpl rpl_frexpl
197 #endif
198 #if (@GNULIB_FREXPL@ && @REPLACE_FREXPL@) || !@HAVE_DECL_FREXPL@
199 extern long double frexpl (long double x, int *expptr);
200 #endif
201 #if !@GNULIB_FREXPL@ && defined GNULIB_POSIXCHECK
202 # undef frexpl
203 # define frexpl(x,e) \
204     (GL_LINK_WARNING ("frexpl is unportable - " \
205                       "use gnulib module frexpl for portability"), \
206      frexpl (x, e))
207 #endif
208
209
210 /* Return x * 2^exp.  */
211 #if @GNULIB_LDEXPL@ && @REPLACE_LDEXPL@
212 # define ldexpl rpl_ldexpl
213 #endif
214 #if (@GNULIB_LDEXPL@ && @REPLACE_LDEXPL@) || !@HAVE_DECL_LDEXPL@
215 extern long double ldexpl (long double x, int exp);
216 #endif
217 #if !@GNULIB_LDEXPL@ && defined GNULIB_POSIXCHECK
218 # undef ldexpl
219 # define ldexpl(x,e) \
220     (GL_LINK_WARNING ("ldexpl is unportable - " \
221                       "use gnulib module ldexpl for portability"), \
222      ldexpl (x, e))
223 #endif
224
225
226 #if @GNULIB_MATHL@ || !@HAVE_DECL_LOGL@
227 extern long double logl (long double x);
228 #endif
229 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
230 # undef logl
231 # define logl(x) \
232     (GL_LINK_WARNING ("logl is unportable - " \
233                       "use gnulib module mathl for portability"), \
234      logl (x))
235 #endif
236
237
238 #if @GNULIB_ROUNDF@
239 # if @REPLACE_ROUNDF@
240 #  undef roundf
241 #  define roundf rpl_roundf
242 extern float roundf (float x);
243 # endif
244 #elif defined GNULIB_POSIXCHECK
245 # undef roundf
246 # define roundf(x) \
247     (GL_LINK_WARNING ("roundf is unportable - " \
248                       "use gnulib module roundf for portability"), \
249      roundf (x))
250 #endif
251
252 #if @GNULIB_ROUND@
253 # if @REPLACE_ROUND@
254 #  undef round
255 #  define round rpl_round
256 extern double round (double x);
257 # endif
258 #elif defined GNULIB_POSIXCHECK
259 # undef round
260 # define round(x) \
261     (GL_LINK_WARNING ("round is unportable - " \
262                       "use gnulib module round for portability"), \
263      round (x))
264 #endif
265
266 #if @GNULIB_ROUNDL@
267 # if @REPLACE_ROUNDL@
268 #  undef roundl
269 #  define roundl rpl_roundl
270 extern long double roundl (long double x);
271 # endif
272 #elif defined GNULIB_POSIXCHECK
273 # undef roundl
274 # define roundl(x) \
275     (GL_LINK_WARNING ("roundl is unportable - " \
276                       "use gnulib module roundl for portability"), \
277      roundl (x))
278 #endif
279
280
281 #if @GNULIB_MATHL@ || !@HAVE_DECL_SINL@
282 extern long double sinl (long double x);
283 #endif
284 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
285 # undef sinl
286 # define sinl(x) \
287     (GL_LINK_WARNING ("sinl is unportable - " \
288                       "use gnulib module mathl for portability"), \
289      sinl (x))
290 #endif
291
292
293 #if @GNULIB_MATHL@ || !@HAVE_DECL_SQRTL@
294 extern long double sqrtl (long double x);
295 #endif
296 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
297 # undef sqrtl
298 # define sqrtl(x) \
299     (GL_LINK_WARNING ("sqrtl is unportable - " \
300                       "use gnulib module mathl for portability"), \
301      sqrtl (x))
302 #endif
303
304
305 #if @GNULIB_MATHL@ || !@HAVE_DECL_TANL@
306 extern long double tanl (long double x);
307 #endif
308 #if !@GNULIB_MATHL@ && defined GNULIB_POSIXCHECK
309 # undef tanl
310 # define tanl(x) \
311     (GL_LINK_WARNING ("tanl is unportable - " \
312                       "use gnulib module mathl for portability"), \
313      tanl (x))
314 #endif
315
316
317 #if @GNULIB_TRUNCF@
318 # if !@HAVE_DECL_TRUNCF@
319 #  define truncf rpl_truncf
320 extern float truncf (float x);
321 # endif
322 #elif defined GNULIB_POSIXCHECK
323 # undef truncf
324 # define truncf(x) \
325     (GL_LINK_WARNING ("truncf is unportable - " \
326                       "use gnulib module truncf for portability"), \
327      truncf (x))
328 #endif
329
330 #if @GNULIB_TRUNC@
331 # if !@HAVE_DECL_TRUNC@
332 #  define trunc rpl_trunc
333 extern double trunc (double x);
334 # endif
335 #elif defined GNULIB_POSIXCHECK
336 # undef trunc
337 # define trunc(x) \
338     (GL_LINK_WARNING ("trunc is unportable - " \
339                       "use gnulib module trunc for portability"), \
340      trunc (x))
341 #endif
342
343 #if @GNULIB_TRUNCL@
344 # if !@HAVE_DECL_TRUNCL@
345 #  define truncl rpl_truncl
346 extern long double truncl (long double x);
347 # endif
348 #elif defined GNULIB_POSIXCHECK
349 # undef truncl
350 # define truncl(x) \
351     (GL_LINK_WARNING ("truncl is unportable - " \
352                       "use gnulib module truncl for portability"), \
353      truncl (x))
354 #endif
355
356
357 #if @GNULIB_ISFINITE@
358 # if @REPLACE_ISFINITE@
359 extern int gl_isfinitef (float x);
360 extern int gl_isfinited (double x);
361 extern int gl_isfinitel (long double x);
362 #  undef isfinite
363 #  define isfinite(x) \
364    (sizeof (x) == sizeof (long double) ? gl_isfinitel (x) : \
365     sizeof (x) == sizeof (double) ? gl_isfinited (x) : \
366     gl_isfinitef (x))
367 # endif
368 #elif defined GNULIB_POSIXCHECK
369   /* How to override a macro?  */
370 #endif
371
372
373 #if @GNULIB_SIGNBIT@
374 # if @REPLACE_SIGNBIT_USING_GCC@
375 #  undef signbit
376    /* GCC 4.0 and newer provides three built-ins for signbit.  */
377 #  define signbit(x) \
378    (sizeof (x) == sizeof (long double) ? __builtin_signbitl (x) : \
379     sizeof (x) == sizeof (double) ? __builtin_signbit (x) : \
380     __builtin_signbitf (x))
381 # endif
382 # if @REPLACE_SIGNBIT@
383 #  undef signbit
384 extern int gl_signbitf (float arg);
385 extern int gl_signbitd (double arg);
386 extern int gl_signbitl (long double arg);
387 #  if __GNUC__ >= 2 && !__STRICT_ANSI__
388 #   if defined FLT_SIGNBIT_WORD && defined FLT_SIGNBIT_BIT
389 #    define gl_signbitf(arg) \
390        ({ union { float _value;                                         \
391                   unsigned int _word[(sizeof (float) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
392                 } _m;                                                   \
393           _m._value = (arg);                                            \
394           (_m._word[FLT_SIGNBIT_WORD] >> FLT_SIGNBIT_BIT) & 1;          \
395         })
396 #   endif
397 #   if defined DBL_SIGNBIT_WORD && defined DBL_SIGNBIT_BIT
398 #    define gl_signbitd(arg) \
399        ({ union { double _value;                                                \
400                   unsigned int _word[(sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
401                 } _m;                                                   \
402           _m._value = (arg);                                            \
403           (_m._word[DBL_SIGNBIT_WORD] >> DBL_SIGNBIT_BIT) & 1;          \
404         })
405 #   endif
406 #   if defined LDBL_SIGNBIT_WORD && defined LDBL_SIGNBIT_BIT
407 #    define gl_signbitl(arg) \
408        ({ union { long double _value;                                   \
409                   unsigned int _word[(sizeof (long double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)]; \
410                 } _m;                                                   \
411           _m._value = (arg);                                            \
412           (_m._word[LDBL_SIGNBIT_WORD] >> LDBL_SIGNBIT_BIT) & 1;                \
413         })
414 #   endif
415 #  endif
416 #  define signbit(x) \
417    (sizeof (x) == sizeof (long double) ? gl_signbitl (x) : \
418     sizeof (x) == sizeof (double) ? gl_signbitd (x) : \
419     gl_signbitf (x))
420 # endif
421 #elif defined GNULIB_POSIXCHECK
422   /* How to override a macro?  */
423 #endif
424
425
426 #ifdef __cplusplus
427 }
428 #endif
429
430 #endif /* _GL_MATH_H */
431 #endif /* _GL_MATH_H */