Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qmath.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \headerfile <QtMath>
6 \inmodule QtCore
7 \title Generic Math Functions
8 \ingroup funclists
9
10 \brief The <QtMath> header file provides various math functions.
11
12 These functions are partly convenience definitions for basic math operations
13 not available in the C or Standard Template Libraries.
14
15 The header also ensures some constants specified in POSIX, but not present
16 in C++ standards (so absent from <math.h> on some platforms), are defined:
17
18 \value M_E The base of the natural logarithms, e = exp(1)
19 \value M_LOG2E The base-two logarithm of e
20 \value M_LOG10E The base-ten logarithm of e
21 \value M_LN2 The natural logarithm of two
22 \value M_LN10 The natural logarithm of ten
23 \value M_PI The ratio of a circle's circumference to diameter, \unicode{0x3C0}
24 \value M_PI_2 Half M_PI, \unicode{0x3C0} / 2
25 \value M_PI_4 Quarter M_PI, \unicode{0x3C0} / 4
26 \value M_1_PI The inverse of M_PI, 1 / \unicode{0x3C0}
27 \value M_2_PI Twice the inverse of M_PI, 2 / \unicode{0x3C0}
28 \value M_2_SQRTPI Two divided by the square root of pi, 2 / \unicode{0x221A}\unicode{0x3C0}
29 \value M_SQRT2 The square root of two, \unicode{0x221A}2
30 \value M_SQRT1_2 The square roof of half, 1 / \unicode{0x221A}2
31*/
32
33/*!
34 \fn template <typename T> int qCeil(T v)
35 Returns the ceiling of the value \a v.
36
37 The ceiling is the smallest integer that is not less than \a v.
38 For example, if \a v is 41.2, then the ceiling is 42.
39
40 \relates <QtMath>
41 \sa qFloor()
42*/
43
44/*!
45 \fn template <typename T> int qFloor(T v)
46 Returns the floor of the value \a v.
47
48 The floor is the largest integer that is not greater than \a v.
49 For example, if \a v is 41.2, then the floor is 41.
50
51 \relates <QtMath>
52 \sa qCeil()
53*/
54
55/*!
56 \fn template <typename T> auto qFabs(T v)
57 Returns the absolute value of \a v.
58
59 \relates <QtMath>
60*/
61
62/*!
63 \fn template <typename T> auto qSin(T v)
64 Returns the sine of the angle \a v in radians.
65
66 \relates <QtMath>
67 \sa qCos(), qTan()
68*/
69
70/*!
71 \fn template <typename T> auto qCos(T v)
72 Returns the cosine of an angle \a v in radians.
73
74 \relates <QtMath>
75 \sa qSin(), qTan()
76*/
77
78/*!
79 \fn template <typename T> auto qTan(T v)
80 Returns the tangent of an angle \a v in radians.
81
82 \relates <QtMath>
83 \sa qSin(), qCos()
84*/
85
86/*!
87 \fn template <typename T> auto qAcos(T v)
88 Returns the arccosine of \a v as an angle in radians.
89 Arccosine is the inverse operation of cosine.
90
91 \relates <QtMath>
92 \sa qAtan(), qAsin(), qCos()
93*/
94
95/*!
96 \fn template <typename T> auto qAsin(T v)
97 Returns the arcsine of \a v as an angle in radians.
98 Arcsine is the inverse operation of sine.
99
100 \relates <QtMath>
101 \sa qSin(), qAtan(), qAcos()
102*/
103
104/*!
105 \fn template <typename T> auto qAtan(T v)
106 Returns the arctangent of \a v as an angle in radians.
107 Arctangent is the inverse operation of tangent.
108
109 \relates <QtMath>
110 \sa qTan(), qAcos(), qAsin()
111*/
112
113/*!
114 \fn template <typename T1, typename T2> auto qAtan2(T1 y, T2 x)
115 Returns the arctangent of a point specified by the coordinates \a y and \a x.
116 This function will return the angle (argument) of that point.
117
118 \relates <QtMath>
119 \sa qAtan(), qHypot()
120*/
121
122/*!
123 \fn template <typename T> auto qSqrt(T v)
124 Returns the square root of \a v.
125 This function returns a NaN if \a v is a negative number.
126
127 \relates <QtMath>
128 \sa qPow(), qHypot()
129*/
130
131/*!
132 \since 6.1
133 \overload
134 \fn template <typename Tx, typename Ty> auto qHypot(Tx x, Ty y)
135 Returns the distance of a point (\a x, \a y) from the origin (0, 0).
136
137 This is qSqrt(x * x + y * y), optimized.
138 In particular, underflow and overflow may be avoided.
139
140 Accepts any mix of numeric types, returning the same
141 floating-point type as std::hypot(). If either parameter is
142 infinite, so is the result; otherwise, if either is a NaN, so is
143 the result.
144
145 \relates <QtMath>
146 \sa qSqrt(), qAtan2()
147*/
148
149/*!
150 \since 6.1
151 \overload
152 \fn template <typename Tx, typename Ty, typename Tz> auto qHypot(Tx x, Ty y, Tz z)
153 Returns the distance of a point (x, y, z) from the origin (0, 0, 0).
154
155 This is qSqrt(x * x + y * y + z * z), optimized where supported.
156 In particular, underflow and overflow may be avoided.
157
158 Accepts any mix of numeric types, returning the same
159 floating-point type as std::hypot(). If any parameter is infinite,
160 so is the result; otherwise, if any is NaN, so is the result.
161
162 \relates <QtMath>
163 \sa qSqrt()
164*/
165
166/*!
167 \since 6.1
168 \fn template<typename F, typename ...Fs> auto qHypot(F first, Fs... rest)
169 Returns the distance from origin in arbitrarily many dimensions
170
171 This is as for the two-argument and three-argument forms, supported by
172 std::hypot(), but with as many numeric parameters as you care to pass to
173 it. Uses \a first and each of the \a rest as coordinates, performing a
174 calculation equivalent to squaring each, summing and returning the square
175 root, save that underflow and overflow are avoided as far as possible.
176
177 \relates <QtMath>
178 \sa qSqrt()
179*/
180
181/*!
182 \fn template <typename T> auto qLn(T v)
183 Returns the natural logarithm of \a v. Natural logarithm uses base e.
184
185 \relates <QtMath>
186 \sa qExp()
187*/
188
189/*!
190 \fn template <typename T> auto qExp(T v)
191 Returns the exponential function of \c e to the power of \a v.
192
193 \relates <QtMath>
194 \sa qLn()
195*/
196
197/*!
198 \fn template <typename T1, typename T2> auto qPow(T1 x, T2 y)
199 Returns the value of \a x raised to the power of \a y.
200 That is, \a x is the base and \a y is the exponent.
201
202 \relates <QtMath>
203 \sa qSqrt()
204*/
205
206/*!
207 \fn float qDegreesToRadians(float degrees)
208 \relates <QtMath>
209 \since 5.1
210
211 This function converts the \a degrees in float to radians.
212
213 Example:
214
215 \snippet code/src_corelib_kernel_qmath.cpp 0
216
217 \sa qRadiansToDegrees()
218*/
219
220/*!
221 \fn double qDegreesToRadians(double degrees)
222 \relates <QtMath>
223 \since 5.1
224
225 This function converts the \a degrees in double to radians.
226
227 Example:
228
229 \snippet code/src_corelib_kernel_qmath.cpp 1
230
231 \sa qRadiansToDegrees()
232*/
233
234/*!
235 \fn long double qDegreesToRadians(long double degrees)
236 \relates <QtMath>
237 \since 6.0
238
239 This function converts the \a degrees in double to radians.
240
241 \sa qRadiansToDegrees()
242*/
243
244/*!
245 \fn template <typename Integral> double qDegreesToRadians(Integral degrees)
246 \relates <QtMath>
247 \since 6.0
248
249 This function converts the \a degrees in double to radians;
250 the angle is casted to a double before the conversion.
251
252 This function participates in overload resolution if and only if
253 \c Integral is an integral type.
254
255 \sa qRadiansToDegrees()
256*/
257
258/*!
259 \fn float qRadiansToDegrees(float radians)
260 \relates <QtMath>
261 \since 5.1
262
263 This function converts the \a radians in float to degrees.
264
265 Example:
266
267 \snippet code/src_corelib_kernel_qmath.cpp 2
268
269 \sa qDegreesToRadians()
270*/
271
272/*!
273 \fn double qRadiansToDegrees(double radians)
274 \relates <QtMath>
275 \since 5.1
276
277 This function converts the \a radians in double to degrees.
278
279 Example:
280
281 \snippet code/src_corelib_kernel_qmath.cpp 3
282
283 \sa qDegreesToRadians()
284*/
285
286/*!
287 \fn long double qRadiansToDegrees(long double radians)
288 \relates <QtMath>
289 \since 6.0
290
291 This function converts the \a radians in double to degrees.
292
293 \sa qDegreesToRadians()
294*/
295
296/*!
297 \fn quint32 qNextPowerOfTwo(quint32 value)
298 \relates <QtMath>
299 \since 5.4
300
301 This function returns the nearest power of two greater than \a value. For 0 it returns 1, and for values larger than or equal to 2^31 the result is undefined.
302*/
303
304/*!
305 \fn quint32 qNextPowerOfTwo(qint32 value)
306 \relates <QtMath>
307 \since 5.4
308 \overload
309
310 This function returns the nearest power of two greater than \a value. For negative values the result is undefined.
311*/
312
313/*!
314 \fn quint64 qNextPowerOfTwo(quint64 value)
315 \relates <QtMath>
316 \since 5.4
317
318 This function returns the nearest power of two greater than \a value. For 0 it returns 1, and for values larger than or equal to 2^63 the result is undefined.
319*/
320
321/*!
322 \fn quint64 qNextPowerOfTwo(qint64 value)
323 \relates <QtMath>
324 \since 5.4
325 \overload
326
327 This function returns the nearest power of two greater than \a value. For negative values the result is undefined.
328*/