cmath 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. // -*- C++ -*- C forwarding header.
  2. // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. // 2006, 2007, 2008, 2009
  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 3, or (at your option)
  10. // any later version.
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License and
  19. // a copy of the GCC Runtime Library Exception along with this program;
  20. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. // <http://www.gnu.org/licenses/>.
  22. /** @file include/cmath
  23. * This is a Standard C++ Library file. You should @c #include this file
  24. * in your programs, rather than any of the "*.h" implementation files.
  25. *
  26. * This is the C++ version of the Standard C Library header @c math.h,
  27. * and its contents are (mostly) the same as that header, but are all
  28. * contained in the namespace @c std (except for names which are defined
  29. * as macros in C).
  30. */
  31. //
  32. // ISO C++ 14882: 26.5 C library
  33. //
  34. #pragma GCC system_header
  35. #include <bits/c++config.h>
  36. #include <bits/cpp_type_traits.h>
  37. #include <ext/type_traits.h>
  38. #include <math.h>
  39. #ifndef _GLIBCXX_CMATH
  40. #define _GLIBCXX_CMATH 1
  41. // Get rid of those macros defined in <math.h> in lieu of real functions.
  42. #undef abs
  43. #undef div
  44. #undef acos
  45. #undef asin
  46. #undef atan
  47. #undef atan2
  48. #undef ceil
  49. #undef cos
  50. #undef cosh
  51. #undef exp
  52. #undef fabs
  53. #undef floor
  54. #undef fmod
  55. #undef frexp
  56. #undef ldexp
  57. #undef log
  58. #undef log10
  59. #undef modf
  60. #undef pow
  61. #undef sin
  62. #undef sinh
  63. #undef sqrt
  64. #undef tan
  65. #undef tanh
  66. _GLIBCXX_BEGIN_NAMESPACE(std)
  67. // Forward declaration of a helper function. This really should be
  68. // an `exported' forward declaration.
  69. template<typename _Tp>
  70. _Tp __cmath_power(_Tp, unsigned int);
  71. template<typename _Tp>
  72. inline _Tp
  73. __pow_helper(_Tp __x, int __n)
  74. {
  75. return __n < 0
  76. ? _Tp(1)/__cmath_power(__x, -__n)
  77. : __cmath_power(__x, __n);
  78. }
  79. inline double
  80. abs(double __x)
  81. { return __builtin_fabs(__x); }
  82. inline float
  83. abs(float __x)
  84. { return __builtin_fabsf(__x); }
  85. inline long double
  86. abs(long double __x)
  87. { return __builtin_fabsl(__x); }
  88. using ::acos;
  89. inline float
  90. acos(float __x)
  91. { return __builtin_acosf(__x); }
  92. inline long double
  93. acos(long double __x)
  94. { return __builtin_acosl(__x); }
  95. template<typename _Tp>
  96. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  97. double>::__type
  98. acos(_Tp __x)
  99. { return __builtin_acos(__x); }
  100. using ::asin;
  101. inline float
  102. asin(float __x)
  103. { return __builtin_asinf(__x); }
  104. inline long double
  105. asin(long double __x)
  106. { return __builtin_asinl(__x); }
  107. template<typename _Tp>
  108. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  109. double>::__type
  110. asin(_Tp __x)
  111. { return __builtin_asin(__x); }
  112. using ::atan;
  113. inline float
  114. atan(float __x)
  115. { return __builtin_atanf(__x); }
  116. inline long double
  117. atan(long double __x)
  118. { return __builtin_atanl(__x); }
  119. template<typename _Tp>
  120. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  121. double>::__type
  122. atan(_Tp __x)
  123. { return __builtin_atan(__x); }
  124. using ::atan2;
  125. inline float
  126. atan2(float __y, float __x)
  127. { return __builtin_atan2f(__y, __x); }
  128. inline long double
  129. atan2(long double __y, long double __x)
  130. { return __builtin_atan2l(__y, __x); }
  131. template<typename _Tp, typename _Up>
  132. inline
  133. typename __gnu_cxx::__promote_2<
  134. typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
  135. && __is_arithmetic<_Up>::__value,
  136. _Tp>::__type, _Up>::__type
  137. atan2(_Tp __y, _Up __x)
  138. {
  139. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  140. return atan2(__type(__y), __type(__x));
  141. }
  142. using ::ceil;
  143. inline float
  144. ceil(float __x)
  145. { return __builtin_ceilf(__x); }
  146. inline long double
  147. ceil(long double __x)
  148. { return __builtin_ceill(__x); }
  149. template<typename _Tp>
  150. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  151. double>::__type
  152. ceil(_Tp __x)
  153. { return __builtin_ceil(__x); }
  154. using ::cos;
  155. inline float
  156. cos(float __x)
  157. { return __builtin_cosf(__x); }
  158. inline long double
  159. cos(long double __x)
  160. { return __builtin_cosl(__x); }
  161. template<typename _Tp>
  162. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  163. double>::__type
  164. cos(_Tp __x)
  165. { return __builtin_cos(__x); }
  166. using ::cosh;
  167. inline float
  168. cosh(float __x)
  169. { return __builtin_coshf(__x); }
  170. inline long double
  171. cosh(long double __x)
  172. { return __builtin_coshl(__x); }
  173. template<typename _Tp>
  174. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  175. double>::__type
  176. cosh(_Tp __x)
  177. { return __builtin_cosh(__x); }
  178. using ::exp;
  179. inline float
  180. exp(float __x)
  181. { return __builtin_expf(__x); }
  182. inline long double
  183. exp(long double __x)
  184. { return __builtin_expl(__x); }
  185. template<typename _Tp>
  186. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  187. double>::__type
  188. exp(_Tp __x)
  189. { return __builtin_exp(__x); }
  190. using ::fabs;
  191. inline float
  192. fabs(float __x)
  193. { return __builtin_fabsf(__x); }
  194. inline long double
  195. fabs(long double __x)
  196. { return __builtin_fabsl(__x); }
  197. template<typename _Tp>
  198. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  199. double>::__type
  200. fabs(_Tp __x)
  201. { return __builtin_fabs(__x); }
  202. using ::floor;
  203. inline float
  204. floor(float __x)
  205. { return __builtin_floorf(__x); }
  206. inline long double
  207. floor(long double __x)
  208. { return __builtin_floorl(__x); }
  209. template<typename _Tp>
  210. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  211. double>::__type
  212. floor(_Tp __x)
  213. { return __builtin_floor(__x); }
  214. using ::fmod;
  215. inline float
  216. fmod(float __x, float __y)
  217. { return __builtin_fmodf(__x, __y); }
  218. inline long double
  219. fmod(long double __x, long double __y)
  220. { return __builtin_fmodl(__x, __y); }
  221. using ::frexp;
  222. inline float
  223. frexp(float __x, int* __exp)
  224. { return __builtin_frexpf(__x, __exp); }
  225. inline long double
  226. frexp(long double __x, int* __exp)
  227. { return __builtin_frexpl(__x, __exp); }
  228. template<typename _Tp>
  229. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  230. double>::__type
  231. frexp(_Tp __x, int* __exp)
  232. { return __builtin_frexp(__x, __exp); }
  233. using ::ldexp;
  234. inline float
  235. ldexp(float __x, int __exp)
  236. { return __builtin_ldexpf(__x, __exp); }
  237. inline long double
  238. ldexp(long double __x, int __exp)
  239. { return __builtin_ldexpl(__x, __exp); }
  240. template<typename _Tp>
  241. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  242. double>::__type
  243. ldexp(_Tp __x, int __exp)
  244. { return __builtin_ldexp(__x, __exp); }
  245. using ::log;
  246. inline float
  247. log(float __x)
  248. { return __builtin_logf(__x); }
  249. inline long double
  250. log(long double __x)
  251. { return __builtin_logl(__x); }
  252. template<typename _Tp>
  253. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  254. double>::__type
  255. log(_Tp __x)
  256. { return __builtin_log(__x); }
  257. using ::log10;
  258. inline float
  259. log10(float __x)
  260. { return __builtin_log10f(__x); }
  261. inline long double
  262. log10(long double __x)
  263. { return __builtin_log10l(__x); }
  264. template<typename _Tp>
  265. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  266. double>::__type
  267. log10(_Tp __x)
  268. { return __builtin_log10(__x); }
  269. using ::modf;
  270. inline float
  271. modf(float __x, float* __iptr)
  272. { return __builtin_modff(__x, __iptr); }
  273. inline long double
  274. modf(long double __x, long double* __iptr)
  275. { return __builtin_modfl(__x, __iptr); }
  276. using ::pow;
  277. inline float
  278. pow(float __x, float __y)
  279. { return __builtin_powf(__x, __y); }
  280. inline long double
  281. pow(long double __x, long double __y)
  282. { return __builtin_powl(__x, __y); }
  283. #ifndef __GXX_EXPERIMENTAL_CXX0X__
  284. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  285. // DR 550. What should the return type of pow(float,int) be?
  286. inline double
  287. pow(double __x, int __i)
  288. { return __builtin_powi(__x, __i); }
  289. inline float
  290. pow(float __x, int __n)
  291. { return __builtin_powif(__x, __n); }
  292. inline long double
  293. pow(long double __x, int __n)
  294. { return __builtin_powil(__x, __n); }
  295. #endif
  296. template<typename _Tp, typename _Up>
  297. inline
  298. typename __gnu_cxx::__promote_2<
  299. typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
  300. && __is_arithmetic<_Up>::__value,
  301. _Tp>::__type, _Up>::__type
  302. pow(_Tp __x, _Up __y)
  303. {
  304. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  305. return pow(__type(__x), __type(__y));
  306. }
  307. using ::sin;
  308. inline float
  309. sin(float __x)
  310. { return __builtin_sinf(__x); }
  311. inline long double
  312. sin(long double __x)
  313. { return __builtin_sinl(__x); }
  314. template<typename _Tp>
  315. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  316. double>::__type
  317. sin(_Tp __x)
  318. { return __builtin_sin(__x); }
  319. using ::sinh;
  320. inline float
  321. sinh(float __x)
  322. { return __builtin_sinhf(__x); }
  323. inline long double
  324. sinh(long double __x)
  325. { return __builtin_sinhl(__x); }
  326. template<typename _Tp>
  327. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  328. double>::__type
  329. sinh(_Tp __x)
  330. { return __builtin_sinh(__x); }
  331. using ::sqrt;
  332. inline float
  333. sqrt(float __x)
  334. { return __builtin_sqrtf(__x); }
  335. inline long double
  336. sqrt(long double __x)
  337. { return __builtin_sqrtl(__x); }
  338. template<typename _Tp>
  339. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  340. double>::__type
  341. sqrt(_Tp __x)
  342. { return __builtin_sqrt(__x); }
  343. using ::tan;
  344. inline float
  345. tan(float __x)
  346. { return __builtin_tanf(__x); }
  347. inline long double
  348. tan(long double __x)
  349. { return __builtin_tanl(__x); }
  350. template<typename _Tp>
  351. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  352. double>::__type
  353. tan(_Tp __x)
  354. { return __builtin_tan(__x); }
  355. using ::tanh;
  356. inline float
  357. tanh(float __x)
  358. { return __builtin_tanhf(__x); }
  359. inline long double
  360. tanh(long double __x)
  361. { return __builtin_tanhl(__x); }
  362. template<typename _Tp>
  363. inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
  364. double>::__type
  365. tanh(_Tp __x)
  366. { return __builtin_tanh(__x); }
  367. _GLIBCXX_END_NAMESPACE
  368. #if _GLIBCXX_USE_C99_MATH
  369. #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
  370. // These are possible macros imported from C99-land.
  371. #undef fpclassify
  372. #undef isfinite
  373. #undef isinf
  374. #undef isnan
  375. #undef isnormal
  376. #undef signbit
  377. #undef isgreater
  378. #undef isgreaterequal
  379. #undef isless
  380. #undef islessequal
  381. #undef islessgreater
  382. #undef isunordered
  383. _GLIBCXX_BEGIN_NAMESPACE(std)
  384. template<typename _Tp>
  385. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  386. int>::__type
  387. fpclassify(_Tp __f)
  388. {
  389. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  390. return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
  391. FP_SUBNORMAL, FP_ZERO, __type(__f));
  392. }
  393. template<typename _Tp>
  394. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  395. int>::__type
  396. isfinite(_Tp __f)
  397. {
  398. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  399. return __builtin_isfinite(__type(__f));
  400. }
  401. template<typename _Tp>
  402. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  403. int>::__type
  404. isinf(_Tp __f)
  405. {
  406. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  407. return __builtin_isinf(__type(__f));
  408. }
  409. template<typename _Tp>
  410. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  411. int>::__type
  412. isnan(_Tp __f)
  413. {
  414. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  415. return __builtin_isnan(__type(__f));
  416. }
  417. template<typename _Tp>
  418. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  419. int>::__type
  420. isnormal(_Tp __f)
  421. {
  422. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  423. return __builtin_isnormal(__type(__f));
  424. }
  425. template<typename _Tp>
  426. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  427. int>::__type
  428. signbit(_Tp __f)
  429. {
  430. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  431. return __builtin_signbit(__type(__f));
  432. }
  433. template<typename _Tp>
  434. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  435. int>::__type
  436. isgreater(_Tp __f1, _Tp __f2)
  437. {
  438. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  439. return __builtin_isgreater(__type(__f1), __type(__f2));
  440. }
  441. template<typename _Tp>
  442. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  443. int>::__type
  444. isgreaterequal(_Tp __f1, _Tp __f2)
  445. {
  446. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  447. return __builtin_isgreaterequal(__type(__f1), __type(__f2));
  448. }
  449. template<typename _Tp>
  450. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  451. int>::__type
  452. isless(_Tp __f1, _Tp __f2)
  453. {
  454. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  455. return __builtin_isless(__type(__f1), __type(__f2));
  456. }
  457. template<typename _Tp>
  458. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  459. int>::__type
  460. islessequal(_Tp __f1, _Tp __f2)
  461. {
  462. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  463. return __builtin_islessequal(__type(__f1), __type(__f2));
  464. }
  465. template<typename _Tp>
  466. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  467. int>::__type
  468. islessgreater(_Tp __f1, _Tp __f2)
  469. {
  470. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  471. return __builtin_islessgreater(__type(__f1), __type(__f2));
  472. }
  473. template<typename _Tp>
  474. inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
  475. int>::__type
  476. isunordered(_Tp __f1, _Tp __f2)
  477. {
  478. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  479. return __builtin_isunordered(__type(__f1), __type(__f2));
  480. }
  481. _GLIBCXX_END_NAMESPACE
  482. #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
  483. #endif
  484. #ifndef _GLIBCXX_EXPORT_TEMPLATE
  485. # include <bits/cmath.tcc>
  486. #endif
  487. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  488. # if defined(_GLIBCXX_INCLUDE_AS_TR1)
  489. # error C++0x header cannot be included from TR1 header
  490. # endif
  491. # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
  492. # include <tr1_impl/cmath>
  493. # else
  494. # define _GLIBCXX_INCLUDE_AS_CXX0X
  495. # define _GLIBCXX_BEGIN_NAMESPACE_TR1
  496. # define _GLIBCXX_END_NAMESPACE_TR1
  497. # define _GLIBCXX_TR1
  498. # include <tr1_impl/cmath>
  499. # undef _GLIBCXX_TR1
  500. # undef _GLIBCXX_END_NAMESPACE_TR1
  501. # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
  502. # undef _GLIBCXX_INCLUDE_AS_CXX0X
  503. # endif
  504. #endif
  505. #endif