libstdc++: cmath Source File

libstdc++

ext/cmath
Go to the documentation of this file.
1// Math extensions -*- C++ -*-
2
3// Copyright (C) 2013-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
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
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file ext/cmath
26 * This file is a GNU extension to the Standard C++ Library.
27 */
28
29#ifndef _EXT_CMATH
30#define _EXT_CMATH 1
31
32#ifdef _GLIBCXX_SYSHDR
33#pragma GCC system_header
34#endif
35
36#include <bits/requires_hosted.h> // GNU extensions are currently omitted
37
38#if __cplusplus < 201103L
39# include <bits/c++0x_warning.h>
40#else
41
42#include <cmath>
43#include <type_traits>
44
45namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
46{
47_GLIBCXX_BEGIN_NAMESPACE_VERSION
48
49 // A class for math constants.
50 template<typename _RealType>
51 struct __math_constants
52 {
53 static_assert(std::is_floating_point<_RealType>::value,
54 "template argument not a floating point type");
55
56 // Constant @f$ \pi @f$.
57 static constexpr _RealType __pi = 3.1415926535897932384626433832795029L;
58 // Constant @f$ \pi / 2 @f$.
59 static constexpr _RealType __pi_half = 1.5707963267948966192313216916397514L;
60 // Constant @f$ \pi / 3 @f$.
61 static constexpr _RealType __pi_third = 1.0471975511965977461542144610931676L;
62 // Constant @f$ \pi / 4 @f$.
63 static constexpr _RealType __pi_quarter = 0.7853981633974483096156608458198757L;
64 // Constant @f$ \sqrt(\pi / 2) @f$.
65 static constexpr _RealType __root_pi_div_2 = 1.2533141373155002512078826424055226L;
66 // Constant @f$ 1 / \pi @f$.
67 static constexpr _RealType __one_div_pi = 0.3183098861837906715377675267450287L;
68 // Constant @f$ 2 / \pi @f$.
69 static constexpr _RealType __two_div_pi = 0.6366197723675813430755350534900574L;
70 // Constant @f$ 2 / \sqrt(\pi) @f$.
71 static constexpr _RealType __two_div_root_pi = 1.1283791670955125738961589031215452L;
72
73 // Constant Euler's number @f$ e @f$.
74 static constexpr _RealType __e = 2.7182818284590452353602874713526625L;
75 // Constant @f$ 1 / e @f$.
76 static constexpr _RealType __one_div_e = 0.36787944117144232159552377016146087L;
77 // Constant @f$ \log_2(e) @f$.
78 static constexpr _RealType __log2_e = 1.4426950408889634073599246810018921L;
79 // Constant @f$ \log_10(e) @f$.
80 static constexpr _RealType __log10_e = 0.4342944819032518276511289189166051L;
81 // Constant @f$ \ln(2) @f$.
82 static constexpr _RealType __ln_2 = 0.6931471805599453094172321214581766L;
83 // Constant @f$ \ln(3) @f$.
84 static constexpr _RealType __ln_3 = 1.0986122886681096913952452369225257L;
85 // Constant @f$ \ln(10) @f$.
86 static constexpr _RealType __ln_10 = 2.3025850929940456840179914546843642L;
87
88 // Constant Euler-Mascheroni @f$ \gamma_E @f$.
89 static constexpr _RealType __gamma_e = 0.5772156649015328606065120900824024L;
90 // Constant Golden Ratio @f$ \phi @f$.
91 static constexpr _RealType __phi = 1.6180339887498948482045868343656381L;
92
93 // Constant @f$ \sqrt(2) @f$.
94 static constexpr _RealType __root_2 = 1.4142135623730950488016887242096981L;
95 // Constant @f$ \sqrt(3) @f$.
96 static constexpr _RealType __root_3 = 1.7320508075688772935274463415058724L;
97 // Constant @f$ \sqrt(5) @f$.
98 static constexpr _RealType __root_5 = 2.2360679774997896964091736687312762L;
99 // Constant @f$ \sqrt(7) @f$.
100 static constexpr _RealType __root_7 = 2.6457513110645905905016157536392604L;
101 // Constant @f$ 1 / \sqrt(2) @f$.
102 static constexpr _RealType __one_div_root_2 = 0.7071067811865475244008443621048490L;
103 };
104
105 // And the template definitions for the constants.
106 template<typename _RealType>
107 constexpr _RealType __math_constants<_RealType>::__pi;
108 template<typename _RealType>
109 constexpr _RealType __math_constants<_RealType>::__pi_half;
110 template<typename _RealType>
111 constexpr _RealType __math_constants<_RealType>::__pi_third;
112 template<typename _RealType>
113 constexpr _RealType __math_constants<_RealType>::__pi_quarter;
114 template<typename _RealType>
115 constexpr _RealType __math_constants<_RealType>::__root_pi_div_2;
116 template<typename _RealType>
117 constexpr _RealType __math_constants<_RealType>::__one_div_pi;
118 template<typename _RealType>
119 constexpr _RealType __math_constants<_RealType>::__two_div_pi;
120 template<typename _RealType>
121 constexpr _RealType __math_constants<_RealType>::__two_div_root_pi;
122 template<typename _RealType>
123 constexpr _RealType __math_constants<_RealType>::__e;
124 template<typename _RealType>
125 constexpr _RealType __math_constants<_RealType>::__one_div_e;
126 template<typename _RealType>
127 constexpr _RealType __math_constants<_RealType>::__log2_e;
128 template<typename _RealType>
129 constexpr _RealType __math_constants<_RealType>::__log10_e;
130 template<typename _RealType>
131 constexpr _RealType __math_constants<_RealType>::__ln_2;
132 template<typename _RealType>
133 constexpr _RealType __math_constants<_RealType>::__ln_3;
134 template<typename _RealType>
135 constexpr _RealType __math_constants<_RealType>::__ln_10;
136 template<typename _RealType>
137 constexpr _RealType __math_constants<_RealType>::__gamma_e;
138 template<typename _RealType>
139 constexpr _RealType __math_constants<_RealType>::__phi;
140 template<typename _RealType>
141 constexpr _RealType __math_constants<_RealType>::__root_2;
142 template<typename _RealType>
143 constexpr _RealType __math_constants<_RealType>::__root_3;
144 template<typename _RealType>
145 constexpr _RealType __math_constants<_RealType>::__root_5;
146 template<typename _RealType>
147 constexpr _RealType __math_constants<_RealType>::__root_7;
148 template<typename _RealType>
149 constexpr _RealType __math_constants<_RealType>::__one_div_root_2;
150
151_GLIBCXX_END_NAMESPACE_VERSION
152} // namespace __gnu_cxx
153
154#endif // C++11
155
156#endif // _EXT_CMATH
type_traits
c++0x_warning.h
requires_hosted.h
__gnu_cxx
GNU extensions for public use.
cmath