std_cmath.h revision 102782
1// -*- C++ -*- C forwarding header.
2
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4// Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library.  This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16
17// You should have received a copy of the GNU General Public License along
18// with this library; see the file COPYING.  If not, write to the Free
19// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20// USA.
21
22// As a special exception, you may use this file as part of a free software
23// library without restriction.  Specifically, if other files instantiate
24// templates or use macros or inline functions from this file, or you compile
25// this file and link it with other files to produce an executable, this
26// file does not by itself cause the resulting executable to be covered by
27// the GNU General Public License.  This exception does not however
28// invalidate any other reasons why the executable file might be covered by
29// the GNU General Public License.
30
31//
32// ISO C++ 14882: 26.5  C library
33//
34
35/** @file cmath
36 *  This is a Standard C++ Library file.  You should @c #include this file
37 *  in your programs, rather than any of the "*.h" implementation files.
38 *
39 *  This is the C++ version of the Standard C Library header @c math.h,
40 *  and its contents are (mostly) the same as that header, but are all
41 *  contained in the namespace @c std.
42 */
43
44#ifndef _CPP_CMATH
45#define _CPP_CMATH 1
46
47#pragma GCC system_header
48
49#include <bits/c++config.h>
50
51#include <math.h>
52
53// Get rid of those macros defined in <math.h> in lieu of real functions.
54#undef abs
55#undef div
56#undef acos
57#undef asin
58#undef atan
59#undef atan2
60#undef ceil
61#undef cos
62#undef cosh
63#undef exp
64#undef fabs
65#undef floor
66#undef fmod
67#undef frexp
68#undef ldexp
69#undef log
70#undef log10
71#undef modf
72#undef pow
73#undef sin
74#undef sinh
75#undef sqrt
76#undef tan
77#undef tanh
78
79namespace std
80{
81  // Forward declaration of a helper function.  This really should be
82  // an `exported' forward declaration.
83  template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
84
85  inline double
86  abs(double __x)
87  { return __builtin_fabs(__x); }
88
89  inline float
90  abs(float __x)
91  { return __builtin_fabsf(__x); }
92
93  inline long double
94  abs(long double __x)
95  { return __builtin_fabsl(__x); }
96
97#if _GLIBCPP_HAVE_ACOSF
98  inline float
99  acos(float __x) { return ::acosf(__x); }
100#else
101  inline float
102  acos(float __x) { return ::acos(static_cast<double>(__x)); }
103#endif
104
105  using ::acos;
106
107#if _GLIBCPP_HAVE_ACOSL
108  inline long double
109  acos(long double __x) { return ::acosl(__x); }
110#else
111  inline long double
112  acos(long double __x) { return ::acos(static_cast<double>(__x)); }
113#endif
114
115  using ::asin;
116
117#if _GLIBCPP_HAVE_ASINF
118  inline float
119  asin(float __x) { return ::asinf(__x); }
120#else
121  inline float
122  asin(float __x) { return ::asin(static_cast<double>(__x)); }
123#endif
124
125#if _GLIBCPP_HAVE_ASINL
126  inline long double
127  asin(long double __x) { return ::asinl(__x); }
128#else
129  inline long double
130  asin(long double __x) { return ::asin(static_cast<double>(__x)); }
131#endif
132
133  using ::atan;
134
135#if _GLIBCPP_HAVE_ATANF
136  inline float
137  atan(float __x) { return ::atanf(__x); }
138#else
139  inline float
140  atan(float __x) { return ::atan(static_cast<double>(__x)); }
141#endif
142
143#if _GLIBCPP_HAVE_ATANL
144  inline long double
145  atan(long double __x) { return ::atanl(__x); }
146#else
147  inline long double
148  atan(long double __x) { return ::atan(static_cast<double>(__x)); }
149#endif
150
151  using ::atan2;
152
153#if _GLIBCPP_HAVE_ATAN2F
154  inline float
155  atan2(float __y, float __x) { return ::atan2f(__y, __x); }
156#else
157  inline float
158  atan2(float __y, float __x)
159  { return ::atan2(static_cast<double>(__y), static_cast<double>(__x)); }
160#endif
161
162#if _GLIBCPP_HAVE_ATAN2L
163  inline long double
164  atan2(long double __y, long double __x) { return ::atan2l(__y, __x); }
165#else
166  inline long double
167  atan2(long double __y, long double __x)
168  { return ::atan2(static_cast<double>(__y), static_cast<double>(__x)); }
169#endif
170
171  using ::ceil;
172
173#if _GLIBCPP_HAVE_CEILF
174  inline float
175  ceil(float __x) { return ::ceilf(__x); }
176#else
177  inline float
178  ceil(float __x) { return ::ceil(static_cast<double>(__x)); }
179#endif
180
181#if _GLIBCPP_HAVE_CEILL
182  inline long double
183  ceil(long double __x) { return ::ceill(__x); }
184#else
185  inline long double
186  ceil(long double __x) { return ::ceil(static_cast<double>(__x)); }
187#endif
188
189  using ::cos;
190
191  inline float
192  cos(float __x)
193  { return __builtin_cosf(__x); }
194
195  inline long double
196  cos(long double __x)
197  { return __builtin_cosl(__x); }
198
199  using ::cosh;
200
201#if _GLIBCPP_HAVE_COSHF
202  inline float
203  cosh(float __x) { return ::coshf(__x); }
204#else
205  inline float
206  cosh(float __x) { return ::cosh(static_cast<double>(__x)); }
207#endif
208
209#if _GLIBCPP_HAVE_COSHL
210  inline long double
211  cosh(long double __x) { return ::coshl(__x); }
212#else
213  inline long double
214  cosh(long double __x) { return ::cosh(static_cast<double>(__x)); }
215#endif
216
217  using ::exp;
218
219#if _GLIBCPP_HAVE_EXPF
220  inline float
221  exp(float __x) { return ::expf(__x); }
222#else
223  inline float
224  exp(float __x) { return ::exp(static_cast<double>(__x)); }
225#endif
226
227#if _GLIBCPP_HAVE_EXPL
228  inline long double
229  exp(long double __x) { return ::expl(__x); }
230#else
231  inline long double
232  exp(long double __x) { return ::exp(static_cast<double>(__x)); }
233#endif
234
235  using ::fabs;
236
237  inline float
238  fabs(float __x)
239  { return __builtin_fabsf(__x); }
240
241  inline long double
242  fabs(long double __x)
243  { return __builtin_fabsl(__x); }
244
245  using ::floor;
246
247#if _GLIBCPP_HAVE_FLOORF
248  inline float
249  floor(float __x) { return ::floorf(__x); }
250#else
251  inline float
252  floor(float __x) { return ::floor(static_cast<double>(__x)); }
253#endif
254
255#if _GLIBCPP_HAVE_FLOORL
256  inline long double
257  floor(long double __x) { return ::floorl(__x); }
258#else
259  inline long double
260  floor(long double __x) { return ::floor(static_cast<double>(__x)); }
261#endif
262
263  using ::fmod;
264
265#if _GLIBCPP_HAVE_FMODF
266  inline float
267  fmod(float __x, float __y) { return ::fmodf(__x, __y); }
268#else
269  inline float
270  fmod(float __x, float __y)
271  { return ::fmod(static_cast<double>(__x), static_cast<double>(__y)); }
272#endif
273
274#if _GLIBCPP_HAVE_FMODL
275  inline long double
276  fmod(long double __x, long double __y) { return ::fmodl(__x, __y); }
277#else
278  inline long double
279  fmod(long double __x, long double __y)
280  { return ::fmod(static_cast<double>(__x), static_cast<double>(__y)); }
281#endif
282
283  using ::frexp;
284
285#if _GLIBCPP_HAVE_FREXPF
286  inline float
287  frexp(float __x, int* __exp) { return ::frexpf(__x, __exp); }
288#else
289  inline float
290  frexp(float __x, int* __exp) { return ::frexp(__x, __exp); }
291#endif
292
293#if _GLIBCPP_HAVE_FREXPL
294  inline long double
295  frexp(long double __x, int* __exp) { return ::frexpl(__x, __exp); }
296#else
297  inline long double
298  frexp(long double __x, int* __exp)
299  { return ::frexp(static_cast<double>(__x), __exp); }
300#endif
301
302  using ::ldexp;
303
304#if _GLIBCPP_HAVE_LDEXPF
305  inline float
306  ldexp(float __x, int __exp) { return ::ldexpf(__x, __exp); }
307#else
308  inline float
309  ldexp(float __x, int __exp)
310  { return ::ldexp(static_cast<double>(__x), __exp); }
311#endif
312
313#if _GLIBCPP_HAVE_LDEXPL
314  inline long double
315  ldexp(long double __x, int __exp) { return ::ldexpl(__x, __exp); }
316#else
317  inline long double
318  ldexp(long double __x, int __exp)
319  { return ::ldexp(static_cast<double>(__x), __exp); }
320#endif
321
322  using ::log;
323
324#if _GLIBCPP_HAVE_LOGF
325  inline float
326  log(float __x) { return ::logf(__x); }
327#else
328  inline float log(float __x)
329  { return ::log(static_cast<double>(__x)); }
330#endif
331
332#if _GLIBCPP_HAVE_LOGL
333  inline long double
334  log(long double __x) { return ::logl(__x); }
335#else
336  inline long double
337  log(long double __x) { return ::log(static_cast<double>(__x)); }
338#endif
339
340  using ::log10;
341
342#if _GLIBCPP_HAVE_LOG10F
343  inline float
344  log10(float __x) { return ::log10f(__x); }
345#else
346  inline float
347  log10(float __x) { return ::log10(static_cast<double>(__x)); }
348#endif
349
350#if _GLIBCPP_HAVE_LOG10L
351  inline long double
352  log10(long double __x) { return ::log10l(__x); }
353#else
354  inline long double
355  log10(long double __x) { return ::log10(static_cast<double>(__x)); }
356#endif
357
358  using ::modf;
359
360#if _GLIBCPP_HAVE_MODFF
361  inline float
362  modf(float __x, float* __iptr) { return ::modff(__x, __iptr); }
363#else
364  inline float
365  modf(float __x, float* __iptr)
366  {
367    double __tmp;
368    double __res = ::modf(static_cast<double>(__x), &__tmp);
369    *__iptr = static_cast<float>(__tmp);
370    return __res;
371  }
372#endif
373
374#if _GLIBCPP_HAVE_MODFL
375  inline long double
376  modf(long double __x, long double* __iptr) { return ::modfl(__x, __iptr); }
377#else
378  inline long double
379  modf(long double __x, long double* __iptr)
380  {
381    double __tmp;
382    double __res = ::modf(static_cast<double>(__x), &__tmp);
383    * __iptr = static_cast<long double>(__tmp);
384    return __res;
385  }
386#endif
387
388  template<typename _Tp>
389    inline _Tp
390    __pow_helper(_Tp __x, int __n)
391    {
392      return __n < 0
393        ? _Tp(1)/__cmath_power(__x, -__n)
394        : __cmath_power(__x, __n);
395    }
396
397  using ::pow;
398
399#if _GLIBCPP_HAVE_POWF
400  inline float
401  pow(float __x, float __y) { return ::powf(__x, __y); }
402#else
403  inline float
404  pow(float __x, float __y)
405  { return ::pow(static_cast<double>(__x), static_cast<double>(__y)); }
406#endif
407
408#if _GLIBCPP_HAVE_POWL
409  inline long double
410  pow(long double __x, long double __y) { return ::powl(__x, __y); }
411#else
412  inline long double
413  pow(long double __x, long double __y)
414  { return ::pow(static_cast<double>(__x), static_cast<double>(__y)); }
415#endif
416
417  inline double
418  pow(double __x, int __i)
419  { return __pow_helper(__x, __i); }
420
421  inline float
422  pow(float __x, int __n)
423  { return __pow_helper(__x, __n); }
424
425  inline long double
426  pow(long double __x, int __n)
427  { return __pow_helper(__x, __n); }
428
429  using ::sin;
430
431  inline float
432  sin(float __x)
433  { return __builtin_sinf(__x); }
434
435  inline long double
436  sin(long double __x)
437  { return __builtin_sinl(__x); }
438
439  using ::sinh;
440
441#if _GLIBCPP_HAVE_SINHF
442  inline float
443  sinh(float __x) { return ::sinhf(__x); }
444#else
445  inline float
446  sinh(float __x) { return ::sinh(static_cast<double>(__x)); }
447#endif
448
449#if _GLIBCPP_HAVE_SINHL
450  inline long double
451  sinh(long double __x) { return ::sinhl(__x); }
452#else
453  inline long double
454  sinh(long double __x) { return ::sinh(static_cast<double>(__x)); }
455#endif
456
457  using ::sqrt;
458
459  inline float
460  sqrt(float __x)
461  { return __builtin_sqrtf(__x); }
462
463  inline long double
464  sqrt(long double __x)
465  { return __builtin_sqrtl(__x); }
466
467  using ::tan;
468
469#if _GLIBCPP_HAVE_TANF
470  inline float
471  tan(float __x) { return ::tanf(__x); }
472#else
473  inline float
474  tan(float __x) { return ::tan(static_cast<double>(__x)); }
475#endif
476
477#if _GLIBCPP_HAVE_TANL
478  inline long double
479  tan(long double __x) { return ::tanl(__x); }
480#else
481  inline long double
482  tan(long double __x) { return ::tan(static_cast<double>(__x)); }
483#endif
484
485  using ::tanh;
486
487#if _GLIBCPP_HAVE_TANHF
488  inline float
489  tanh(float __x) { return ::tanhf(__x); }
490#else
491  inline float
492  tanh(float __x) { return ::tanh(static_cast<double>(__x)); }
493#endif
494
495#if _GLIBCPP_HAVE_TANHL
496  inline long double
497  tanh(long double __x) { return ::tanhl(__x); }
498#else
499  inline long double
500  tanh(long double __x) { return ::tanh(static_cast<double>(__x)); }
501#endif
502}
503
504
505#if _GLIBCPP_USE_C99
506// These are possible macros imported from C99-land. For strict
507// conformance, remove possible C99-injected names from the global
508// namespace, and sequester them in the __gnu_cxx extension namespace.
509namespace __gnu_cxx
510{
511  template<typename _Tp>
512    int
513    __capture_fpclassify(_Tp __f) { return fpclassify(__f); }
514
515  template<typename _Tp>
516    int
517    __capture_isfinite(_Tp __f) { return isfinite(__f); }
518
519  template<typename _Tp>
520    int
521    __capture_isinf(_Tp __f) { return isinf(__f); }
522
523  template<typename _Tp>
524    int
525    __capture_isnan(_Tp __f) { return isnan(__f); }
526
527  template<typename _Tp>
528    int
529    __capture_isnormal(_Tp __f) { return isnormal(__f); }
530
531  template<typename _Tp>
532    int
533    __capture_signbit(_Tp __f) { return signbit(__f); }
534
535  template<typename _Tp>
536    int
537    __capture_isgreater(_Tp __f1, _Tp __f2)
538    { return isgreater(__f1, __f2); }
539
540  template<typename _Tp>
541     int
542     __capture_isgreaterequal(_Tp __f1, _Tp __f2)
543     { return isgreaterequal(__f1, __f2); }
544
545  template<typename _Tp>
546     int
547     __capture_isless(_Tp __f1, _Tp __f2) { return isless(__f1, __f2); }
548
549  template<typename _Tp>
550     int
551     __capture_islessequal(_Tp __f1, _Tp __f2)
552     { return islessequal(__f1, __f2); }
553
554  template<typename _Tp>
555     int
556     __capture_islessgreater(_Tp __f1, _Tp __f2)
557     { return islessgreater(__f1, __f2); }
558
559  template<typename _Tp>
560     int
561     __capture_isunordered(_Tp __f1, _Tp __f2)
562     { return isunordered(__f1, __f2); }
563}
564#endif
565
566#undef fpclassify
567#undef isfinite
568#undef isinf
569#undef isnan
570#undef isnormal
571#undef signbit
572#undef isgreater
573#undef isgreaterequal
574#undef isless
575#undef islessequal
576#undef islessgreater
577#undef isunordered
578
579#if _GLIBCPP_USE_C99
580namespace __gnu_cxx
581{
582  template<typename _Tp>
583    int
584    fpclassify(_Tp __f) { return __capture_fpclassify(__f); }
585
586  template<typename _Tp>
587    int
588    isfinite(_Tp __f) { return __capture_isfinite(__f); }
589
590  template<typename _Tp>
591    int
592    isinf(_Tp __f) { return __capture_isinf(__f); }
593
594  template<typename _Tp>
595    int
596    isnan(_Tp __f) { return __capture_isnan(__f); }
597
598  template<typename _Tp>
599    int
600    isnormal(_Tp __f) { return __capture_isnormal(__f); }
601
602  template<typename _Tp>
603    int
604    signbit(_Tp __f) { return __capture_signbit(__f); }
605
606  template<typename _Tp>
607    int
608    isgreater(_Tp __f1, _Tp __f2) { return __capture_isgreater(__f1, __f2); }
609
610  template<typename _Tp>
611    int
612    isgreaterequal(_Tp __f1, _Tp __f2)
613    { return __capture_isgreaterequal(__f1, __f2); }
614
615  template<typename _Tp>
616    int
617    isless(_Tp __f1, _Tp __f2) { return __capture_isless(__f1, __f2); }
618
619  template<typename _Tp>
620    int
621    islessequal(_Tp __f1, _Tp __f2)
622    { return __capture_islessequal(__f1, __f2); }
623
624  template<typename _Tp>
625    int
626    islessgreater(_Tp __f1, _Tp __f2)
627    { return __capture_islessgreater(__f1, __f2); }
628
629  template<typename _Tp>
630    int
631    isunordered(_Tp __f1, _Tp __f2)
632    { return __capture_isunordered(__f1, __f2); }
633}
634
635namespace std
636{
637  using __gnu_cxx::fpclassify;
638  using __gnu_cxx::isfinite;
639  using __gnu_cxx::isinf;
640  using __gnu_cxx::isnan;
641  using __gnu_cxx::isnormal;
642  using __gnu_cxx::signbit;
643  using __gnu_cxx::isgreater;
644  using __gnu_cxx::isgreaterequal;
645  using __gnu_cxx::isless;
646  using __gnu_cxx::islessequal;
647  using __gnu_cxx::islessgreater;
648  using __gnu_cxx::isunordered;
649}
650#endif
651
652#ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
653#  define export
654#  include <bits/cmath.tcc>
655#endif
656
657#endif
658