std_cmath.h revision 117397
1// -*- C++ -*- C forwarding header.
2
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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
79// ...and in the darkness bind them...
80namespace __gnu_cxx
81{
82  namespace  __c99_binding
83  {
84#if _GLIBCPP_USE_C99_FLOAT_TRANSCENDENTALS_CHECK || \
85    _GLIBCPP_USE_C99_FLOAT_TRANSCENDENTALS_DYNAMIC
86    extern "C" float (acosf)(float);
87    extern "C" float (asinf)(float);
88    extern "C" float (atanf)(float);
89    extern "C" float (atan2f)(float, float);
90    extern "C" float (ceilf)(float);
91    extern "C" float (coshf)(float);
92    extern "C" float (expf)(float);
93    extern "C" float (floorf)(float);
94    extern "C" float (fmodf)(float, float);
95    extern "C" float (frexpf)(float, int*);
96    extern "C" float (ldexpf)(float, int);
97    extern "C" float (logf)(float);
98    extern "C" float (log10f)(float);
99    extern "C" float (modff)(float, float*);
100    extern "C" float (powf)(float, float);
101    extern "C" float (sinhf)(float);
102    extern "C" float (tanf)(float);
103    extern "C" float (tanhf)(float);
104#endif
105#if !_GLIBCPP_USE_C99_FLOAT_TRANSCENDENTALS_DYNAMIC
106#if _GLIBCPP_HAVE_ACOSF
107    using ::acosf;
108#endif
109#if _GLIBCPP_HAVE_ASINF
110    using ::asinf;
111#endif
112#if _GLIBCPP_HAVE_ATANF
113    using ::atanf;
114#endif
115#if _GLIBCPP_HAVE_ATAN2F
116    using ::atan2f;
117#endif
118#if _GLIBCPP_HAVE_CEILF
119    using ::ceilf;
120#endif
121#if _GLIBCPP_HAVE_COSHF
122    using ::coshf;
123#endif
124#if _GLIBCPP_HAVE_EXPF
125    using ::expf;
126#endif
127#if _GLIBCPP_HAVE_FLOORF
128    using ::floorf;
129#endif
130#if _GLIBCPP_HAVE_FMODF
131    using ::fmodf;
132#endif
133#if _GLIBCPP_HAVE_FREXPF
134    using ::frexpf;
135#endif
136#if _GLIBCPP_HAVE_LDEXPF
137    using ::ldexpf;
138#endif
139#if _GLIBCPP_HAVE_LOGF
140    using ::logf;
141#endif
142#if _GLIBCPP_HAVE_LOG10F
143    using ::log10f;
144#endif
145#if _GLIBCPP_HAVE_MODFF
146    using ::modff;
147#endif
148#if _GLIBCPP_HAVE_POWF
149    using ::powf;
150#endif
151#if _GLIBCPP_HAVE_SINHF
152    using ::sinhf;
153#endif
154#if _GLIBCPP_HAVE_TANF
155    using ::tanf;
156#endif
157#if _GLIBCPP_HAVE_TANHF
158    using ::tanhf;
159#endif
160#endif /* _GLIBCPP_USE_C99_FLOAT_TRANSCENDENTALS_DYNAMIC */
161  }
162}
163
164namespace std
165{
166  // Forward declaration of a helper function.  This really should be
167  // an `exported' forward declaration.
168  template<typename _Tp> _Tp __cmath_power(_Tp, unsigned int);
169
170  inline double
171  abs(double __x)
172  { return __builtin_fabs(__x); }
173
174  inline float
175  abs(float __x)
176  { return __builtin_fabsf(__x); }
177
178  inline long double
179  abs(long double __x)
180  { return __builtin_fabsl(__x); }
181
182#if _GLIBCPP_HAVE_ACOSF
183  inline float
184  acos(float __x) { return __gnu_cxx::__c99_binding::acosf(__x); }
185#else
186  inline float
187  acos(float __x) { return ::acos(static_cast<double>(__x)); }
188#endif
189
190  using ::acos;
191
192#if _GLIBCPP_HAVE_ACOSL
193  inline long double
194  acos(long double __x) { return ::acosl(__x); }
195#else
196  inline long double
197  acos(long double __x) { return ::acos(static_cast<double>(__x)); }
198#endif
199
200  using ::asin;
201
202#if _GLIBCPP_HAVE_ASINF
203  inline float
204  asin(float __x) { return __gnu_cxx::__c99_binding::asinf(__x); }
205#else
206  inline float
207  asin(float __x) { return ::asin(static_cast<double>(__x)); }
208#endif
209
210#if _GLIBCPP_HAVE_ASINL
211  inline long double
212  asin(long double __x) { return ::asinl(__x); }
213#else
214  inline long double
215  asin(long double __x) { return ::asin(static_cast<double>(__x)); }
216#endif
217
218  using ::atan;
219
220#if _GLIBCPP_HAVE_ATANF
221  inline float
222  atan(float __x) { return __gnu_cxx::__c99_binding::atanf(__x); }
223#else
224  inline float
225  atan(float __x) { return ::atan(static_cast<double>(__x)); }
226#endif
227
228#if _GLIBCPP_HAVE_ATANL
229  inline long double
230  atan(long double __x) { return ::atanl(__x); }
231#else
232  inline long double
233  atan(long double __x) { return ::atan(static_cast<double>(__x)); }
234#endif
235
236  using ::atan2;
237
238#if _GLIBCPP_HAVE_ATAN2F
239  inline float
240  atan2(float __y, float __x) { return __gnu_cxx::__c99_binding::atan2f(__y, __x); }
241#else
242  inline float
243  atan2(float __y, float __x)
244  { return ::atan2(static_cast<double>(__y), static_cast<double>(__x)); }
245#endif
246
247#if _GLIBCPP_HAVE_ATAN2L
248  inline long double
249  atan2(long double __y, long double __x) { return ::atan2l(__y, __x); }
250#else
251  inline long double
252  atan2(long double __y, long double __x)
253  { return ::atan2(static_cast<double>(__y), static_cast<double>(__x)); }
254#endif
255
256  using ::ceil;
257
258#if _GLIBCPP_HAVE_CEILF
259  inline float
260  ceil(float __x) { return __gnu_cxx::__c99_binding::ceilf(__x); }
261#else
262  inline float
263  ceil(float __x) { return ::ceil(static_cast<double>(__x)); }
264#endif
265
266#if _GLIBCPP_HAVE_CEILL
267  inline long double
268  ceil(long double __x) { return ::ceill(__x); }
269#else
270  inline long double
271  ceil(long double __x) { return ::ceil(static_cast<double>(__x)); }
272#endif
273
274  using ::cos;
275
276  inline float
277  cos(float __x)
278  { return __builtin_cosf(__x); }
279
280  inline long double
281  cos(long double __x)
282  { return __builtin_cosl(__x); }
283
284  using ::cosh;
285
286#if _GLIBCPP_HAVE_COSHF
287  inline float
288  cosh(float __x) { return __gnu_cxx::__c99_binding::coshf(__x); }
289#else
290  inline float
291  cosh(float __x) { return ::cosh(static_cast<double>(__x)); }
292#endif
293
294#if _GLIBCPP_HAVE_COSHL
295  inline long double
296  cosh(long double __x) { return ::coshl(__x); }
297#else
298  inline long double
299  cosh(long double __x) { return ::cosh(static_cast<double>(__x)); }
300#endif
301
302  using ::exp;
303
304#if _GLIBCPP_HAVE_EXPF
305  inline float
306  exp(float __x) { return __gnu_cxx::__c99_binding::expf(__x); }
307#else
308  inline float
309  exp(float __x) { return ::exp(static_cast<double>(__x)); }
310#endif
311
312#if _GLIBCPP_HAVE_EXPL
313  inline long double
314  exp(long double __x) { return ::expl(__x); }
315#else
316  inline long double
317  exp(long double __x) { return ::exp(static_cast<double>(__x)); }
318#endif
319
320  using ::fabs;
321
322  inline float
323  fabs(float __x)
324  { return __builtin_fabsf(__x); }
325
326  inline long double
327  fabs(long double __x)
328  { return __builtin_fabsl(__x); }
329
330  using ::floor;
331
332#if _GLIBCPP_HAVE_FLOORF
333  inline float
334  floor(float __x) { return __gnu_cxx::__c99_binding::floorf(__x); }
335#else
336  inline float
337  floor(float __x) { return ::floor(static_cast<double>(__x)); }
338#endif
339
340#if _GLIBCPP_HAVE_FLOORL
341  inline long double
342  floor(long double __x) { return ::floorl(__x); }
343#else
344  inline long double
345  floor(long double __x) { return ::floor(static_cast<double>(__x)); }
346#endif
347
348  using ::fmod;
349
350#if _GLIBCPP_HAVE_FMODF
351  inline float
352  fmod(float __x, float __y) { return __gnu_cxx::__c99_binding::fmodf(__x, __y); }
353#else
354  inline float
355  fmod(float __x, float __y)
356  { return ::fmod(static_cast<double>(__x), static_cast<double>(__y)); }
357#endif
358
359#if _GLIBCPP_HAVE_FMODL
360  inline long double
361  fmod(long double __x, long double __y) { return ::fmodl(__x, __y); }
362#else
363  inline long double
364  fmod(long double __x, long double __y)
365  { return ::fmod(static_cast<double>(__x), static_cast<double>(__y)); }
366#endif
367
368  using ::frexp;
369
370#if _GLIBCPP_HAVE_FREXPF
371  inline float
372  frexp(float __x, int* __exp) { return __gnu_cxx::__c99_binding::frexpf(__x, __exp); }
373#else
374  inline float
375  frexp(float __x, int* __exp) { return ::frexp(__x, __exp); }
376#endif
377
378#if _GLIBCPP_HAVE_FREXPL
379  inline long double
380  frexp(long double __x, int* __exp) { return ::frexpl(__x, __exp); }
381#else
382  inline long double
383  frexp(long double __x, int* __exp)
384  { return ::frexp(static_cast<double>(__x), __exp); }
385#endif
386
387  using ::ldexp;
388
389#if _GLIBCPP_HAVE_LDEXPF
390  inline float
391  ldexp(float __x, int __exp) { return __gnu_cxx::__c99_binding::ldexpf(__x, __exp); }
392#else
393  inline float
394  ldexp(float __x, int __exp)
395  { return ::ldexp(static_cast<double>(__x), __exp); }
396#endif
397
398#if _GLIBCPP_HAVE_LDEXPL
399  inline long double
400  ldexp(long double __x, int __exp) { return ::ldexpl(__x, __exp); }
401#else
402  inline long double
403  ldexp(long double __x, int __exp)
404  { return ::ldexp(static_cast<double>(__x), __exp); }
405#endif
406
407  using ::log;
408
409#if _GLIBCPP_HAVE_LOGF
410  inline float
411  log(float __x) { return __gnu_cxx::__c99_binding::logf(__x); }
412#else
413  inline float log(float __x)
414  { return ::log(static_cast<double>(__x)); }
415#endif
416
417#if _GLIBCPP_HAVE_LOGL
418  inline long double
419  log(long double __x) { return ::logl(__x); }
420#else
421  inline long double
422  log(long double __x) { return ::log(static_cast<double>(__x)); }
423#endif
424
425  using ::log10;
426
427#if _GLIBCPP_HAVE_LOG10F
428  inline float
429  log10(float __x) { return __gnu_cxx::__c99_binding::log10f(__x); }
430#else
431  inline float
432  log10(float __x) { return ::log10(static_cast<double>(__x)); }
433#endif
434
435#if _GLIBCPP_HAVE_LOG10L
436  inline long double
437  log10(long double __x) { return ::log10l(__x); }
438#else
439  inline long double
440  log10(long double __x) { return ::log10(static_cast<double>(__x)); }
441#endif
442
443  using ::modf;
444
445#if _GLIBCPP_HAVE_MODFF
446  inline float
447  modf(float __x, float* __iptr) { return __gnu_cxx::__c99_binding::modff(__x, __iptr); }
448#else
449  inline float
450  modf(float __x, float* __iptr)
451  {
452    double __tmp;
453    double __res = ::modf(static_cast<double>(__x), &__tmp);
454    *__iptr = static_cast<float>(__tmp);
455    return __res;
456  }
457#endif
458
459#if _GLIBCPP_HAVE_MODFL
460  inline long double
461  modf(long double __x, long double* __iptr) { return ::modfl(__x, __iptr); }
462#else
463  inline long double
464  modf(long double __x, long double* __iptr)
465  {
466    double __tmp;
467    double __res = ::modf(static_cast<double>(__x), &__tmp);
468    * __iptr = static_cast<long double>(__tmp);
469    return __res;
470  }
471#endif
472
473  template<typename _Tp>
474    inline _Tp
475    __pow_helper(_Tp __x, int __n)
476    {
477      return __n < 0
478        ? _Tp(1)/__cmath_power(__x, -__n)
479        : __cmath_power(__x, __n);
480    }
481
482  using ::pow;
483
484#if _GLIBCPP_HAVE_POWF
485  inline float
486  pow(float __x, float __y) { return __gnu_cxx::__c99_binding::powf(__x, __y); }
487#else
488  inline float
489  pow(float __x, float __y)
490  { return ::pow(static_cast<double>(__x), static_cast<double>(__y)); }
491#endif
492
493#if _GLIBCPP_HAVE_POWL
494  inline long double
495  pow(long double __x, long double __y) { return ::powl(__x, __y); }
496#else
497  inline long double
498  pow(long double __x, long double __y)
499  { return ::pow(static_cast<double>(__x), static_cast<double>(__y)); }
500#endif
501
502  inline double
503  pow(double __x, int __i)
504  { return __pow_helper(__x, __i); }
505
506  inline float
507  pow(float __x, int __n)
508  { return __pow_helper(__x, __n); }
509
510  inline long double
511  pow(long double __x, int __n)
512  { return __pow_helper(__x, __n); }
513
514  using ::sin;
515
516  inline float
517  sin(float __x)
518  { return __builtin_sinf(__x); }
519
520  inline long double
521  sin(long double __x)
522  { return __builtin_sinl(__x); }
523
524  using ::sinh;
525
526#if _GLIBCPP_HAVE_SINHF
527  inline float
528  sinh(float __x) { return __gnu_cxx::__c99_binding::sinhf(__x); }
529#else
530  inline float
531  sinh(float __x) { return ::sinh(static_cast<double>(__x)); }
532#endif
533
534#if _GLIBCPP_HAVE_SINHL
535  inline long double
536  sinh(long double __x) { return ::sinhl(__x); }
537#else
538  inline long double
539  sinh(long double __x) { return ::sinh(static_cast<double>(__x)); }
540#endif
541
542  using ::sqrt;
543
544  inline float
545  sqrt(float __x)
546  { return __builtin_sqrtf(__x); }
547
548  inline long double
549  sqrt(long double __x)
550  { return __builtin_sqrtl(__x); }
551
552  using ::tan;
553
554#if _GLIBCPP_HAVE_TANF
555  inline float
556  tan(float __x) { return __gnu_cxx::__c99_binding::tanf(__x); }
557#else
558  inline float
559  tan(float __x) { return ::tan(static_cast<double>(__x)); }
560#endif
561
562#if _GLIBCPP_HAVE_TANL
563  inline long double
564  tan(long double __x) { return ::tanl(__x); }
565#else
566  inline long double
567  tan(long double __x) { return ::tan(static_cast<double>(__x)); }
568#endif
569
570  using ::tanh;
571
572#if _GLIBCPP_HAVE_TANHF
573  inline float
574  tanh(float __x) { return __gnu_cxx::__c99_binding::tanhf(__x); }
575#else
576  inline float
577  tanh(float __x) { return ::tanh(static_cast<double>(__x)); }
578#endif
579
580#if _GLIBCPP_HAVE_TANHL
581  inline long double
582  tanh(long double __x) { return ::tanhl(__x); }
583#else
584  inline long double
585  tanh(long double __x) { return ::tanh(static_cast<double>(__x)); }
586#endif
587}
588
589
590#if _GLIBCPP_USE_C99
591#if !_GLIBCPP_USE_C99_FP_MACROS_DYNAMIC
592// These are possible macros imported from C99-land. For strict
593// conformance, remove possible C99-injected names from the global
594// namespace, and sequester them in the __gnu_cxx extension namespace.
595namespace __gnu_cxx
596{
597  template<typename _Tp>
598    int
599    __capture_fpclassify(_Tp __f) { return fpclassify(__f); }
600
601  template<typename _Tp>
602    int
603    __capture_isfinite(_Tp __f) { return isfinite(__f); }
604
605  template<typename _Tp>
606    int
607    __capture_isinf(_Tp __f) { return isinf(__f); }
608
609  template<typename _Tp>
610    int
611    __capture_isnan(_Tp __f) { return isnan(__f); }
612
613  template<typename _Tp>
614    int
615    __capture_isnormal(_Tp __f) { return isnormal(__f); }
616
617  template<typename _Tp>
618    int
619    __capture_signbit(_Tp __f) { return signbit(__f); }
620
621  template<typename _Tp>
622    int
623    __capture_isgreater(_Tp __f1, _Tp __f2)
624    { return isgreater(__f1, __f2); }
625
626  template<typename _Tp>
627     int
628     __capture_isgreaterequal(_Tp __f1, _Tp __f2)
629     { return isgreaterequal(__f1, __f2); }
630
631  template<typename _Tp>
632     int
633     __capture_isless(_Tp __f1, _Tp __f2) { return isless(__f1, __f2); }
634
635  template<typename _Tp>
636     int
637     __capture_islessequal(_Tp __f1, _Tp __f2)
638     { return islessequal(__f1, __f2); }
639
640  template<typename _Tp>
641     int
642     __capture_islessgreater(_Tp __f1, _Tp __f2)
643     { return islessgreater(__f1, __f2); }
644
645  template<typename _Tp>
646     int
647     __capture_isunordered(_Tp __f1, _Tp __f2)
648     { return isunordered(__f1, __f2); }
649}
650#endif /* _GLIBCPP_USE_C99_FP_MACROS_DYNAMIC */
651#endif
652
653#undef fpclassify
654#undef isfinite
655#undef isinf
656#undef isnan
657#undef isnormal
658#undef signbit
659#undef isgreater
660#undef isgreaterequal
661#undef isless
662#undef islessequal
663#undef islessgreater
664#undef isunordered
665
666#if _GLIBCPP_USE_C99
667#if !_GLIBCPP_USE_C99_FP_MACROS_DYNAMIC
668namespace __gnu_cxx
669{
670  template<typename _Tp>
671    int
672    fpclassify(_Tp __f) { return __capture_fpclassify(__f); }
673
674  template<typename _Tp>
675    int
676    isfinite(_Tp __f) { return __capture_isfinite(__f); }
677
678  template<typename _Tp>
679    int
680    isinf(_Tp __f) { return __capture_isinf(__f); }
681
682  template<typename _Tp>
683    int
684    isnan(_Tp __f) { return __capture_isnan(__f); }
685
686  template<typename _Tp>
687    int
688    isnormal(_Tp __f) { return __capture_isnormal(__f); }
689
690  template<typename _Tp>
691    int
692    signbit(_Tp __f) { return __capture_signbit(__f); }
693
694  template<typename _Tp>
695    int
696    isgreater(_Tp __f1, _Tp __f2) { return __capture_isgreater(__f1, __f2); }
697
698  template<typename _Tp>
699    int
700    isgreaterequal(_Tp __f1, _Tp __f2)
701    { return __capture_isgreaterequal(__f1, __f2); }
702
703  template<typename _Tp>
704    int
705    isless(_Tp __f1, _Tp __f2) { return __capture_isless(__f1, __f2); }
706
707  template<typename _Tp>
708    int
709    islessequal(_Tp __f1, _Tp __f2)
710    { return __capture_islessequal(__f1, __f2); }
711
712  template<typename _Tp>
713    int
714    islessgreater(_Tp __f1, _Tp __f2)
715    { return __capture_islessgreater(__f1, __f2); }
716
717  template<typename _Tp>
718    int
719    isunordered(_Tp __f1, _Tp __f2)
720    { return __capture_isunordered(__f1, __f2); }
721}
722
723namespace std
724{
725  using __gnu_cxx::fpclassify;
726  using __gnu_cxx::isfinite;
727  using __gnu_cxx::isinf;
728  using __gnu_cxx::isnan;
729  using __gnu_cxx::isnormal;
730  using __gnu_cxx::signbit;
731  using __gnu_cxx::isgreater;
732  using __gnu_cxx::isgreaterequal;
733  using __gnu_cxx::isless;
734  using __gnu_cxx::islessequal;
735  using __gnu_cxx::islessgreater;
736  using __gnu_cxx::isunordered;
737}
738#endif /* _GLIBCPP_USE_C99_FP_MACROS_DYNAMIC */
739#endif
740
741#ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
742#  define export
743#  include <bits/cmath.tcc>
744#endif
745
746#endif
747