1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QMATH_H
43#define QMATH_H
44
45#include <math.h>
46
47#include <QtCore/qglobal.h>
48
49#ifdef Q_OS_SYMBIAN
50# include <e32math.h>
51#endif
52
53QT_BEGIN_HEADER
54
55QT_BEGIN_NAMESPACE
56
57QT_MODULE(Core)
58
59#define QT_SINE_TABLE_SIZE 256
60
61extern Q_CORE_EXPORT const qreal qt_sine_table[QT_SINE_TABLE_SIZE];
62
63inline int qCeil(qreal v)
64{
65#ifdef QT_USE_MATH_H_FLOATS
66 if (sizeof(qreal) == sizeof(float))
67 return int(ceilf(float(v)));
68 else
69#endif
70 return int(ceil(v));
71}
72
73inline int qFloor(qreal v)
74{
75#ifdef QT_USE_MATH_H_FLOATS
76 if (sizeof(qreal) == sizeof(float))
77 return int(floorf(float(v)));
78 else
79#endif
80 return int(floor(v));
81}
82
83inline qreal qFabs(qreal v)
84{
85#ifdef QT_USE_MATH_H_FLOATS
86 if(sizeof(qreal) == sizeof(float))
87 return fabsf(float(v));
88 else
89#endif
90 return fabs(v);
91}
92
93inline qreal qSin(qreal v)
94{
95#ifdef Q_OS_SYMBIAN
96 TReal sin_v;
97 Math::Sin(sin_v, static_cast<TReal>(v));
98 return static_cast<qreal>(sin_v);
99#else
100# ifdef QT_USE_MATH_H_FLOATS
101 if (sizeof(qreal) == sizeof(float))
102 return sinf(float(v));
103 else
104# endif
105 return sin(v);
106#endif
107}
108
109inline qreal qCos(qreal v)
110{
111#ifdef Q_OS_SYMBIAN
112 TReal cos_v;
113 Math::Cos(cos_v, static_cast<TReal>(v));
114 return static_cast<qreal>(cos_v);
115#else
116# ifdef QT_USE_MATH_H_FLOATS
117 if (sizeof(qreal) == sizeof(float))
118 return cosf(float(v));
119 else
120# endif
121 return cos(v);
122#endif
123}
124
125inline qreal qTan(qreal v)
126{
127#ifdef Q_OS_SYMBIAN
128 TReal tan_v;
129 Math::Tan(tan_v, static_cast<TReal>(v));
130 return static_cast<qreal>(tan_v);
131#else
132# ifdef QT_USE_MATH_H_FLOATS
133 if (sizeof(qreal) == sizeof(float))
134 return tanf(float(v));
135 else
136# endif
137 return tan(v);
138#endif
139}
140
141inline qreal qAcos(qreal v)
142{
143#ifdef Q_OS_SYMBIAN
144 TReal acos_v;
145 Math::ACos(acos_v, static_cast<TReal>(v));
146 return static_cast<qreal>(acos_v);
147#else
148# ifdef QT_USE_MATH_H_FLOATS
149 if (sizeof(qreal) == sizeof(float))
150 return acosf(float(v));
151 else
152# endif
153 return acos(v);
154#endif
155}
156
157inline qreal qAsin(qreal v)
158{
159#ifdef Q_OS_SYMBIAN
160 TReal asin_v;
161 Math::ASin(asin_v, static_cast<TReal>(v));
162 return static_cast<qreal>(asin_v);
163#else
164# ifdef QT_USE_MATH_H_FLOATS
165 if (sizeof(qreal) == sizeof(float))
166 return asinf(float(v));
167 else
168# endif
169 return asin(v);
170#endif
171}
172
173inline qreal qAtan(qreal v)
174{
175#ifdef Q_OS_SYMBIAN
176 TReal atan_v;
177 Math::ATan(atan_v, static_cast<TReal>(v));
178 return static_cast<qreal>(atan_v);
179#else
180# ifdef QT_USE_MATH_H_FLOATS
181 if(sizeof(qreal) == sizeof(float))
182 return atanf(float(v));
183 else
184# endif
185 return atan(v);
186#endif
187}
188
189inline qreal qAtan2(qreal x, qreal y)
190{
191#ifdef Q_OS_SYMBIAN
192 TReal atan2_v;
193 Math::ATan(atan2_v, static_cast<TReal>(x), static_cast<TReal>(y));
194 return static_cast<qreal>(atan2_v);
195#else
196# ifdef QT_USE_MATH_H_FLOATS
197 if(sizeof(qreal) == sizeof(float))
198 return atan2f(float(x), float(y));
199 else
200# endif
201 return atan2(x, y);
202#endif
203}
204
205inline qreal qSqrt(qreal v)
206{
207#ifdef Q_OS_SYMBIAN
208 TReal sqrt_v;
209 Math::Sqrt(sqrt_v, static_cast<TReal>(v));
210 return static_cast<qreal>(sqrt_v);
211#else
212# ifdef QT_USE_MATH_H_FLOATS
213 if (sizeof(qreal) == sizeof(float))
214 return sqrtf(float(v));
215 else
216# endif
217 return sqrt(v);
218#endif
219}
220
221inline qreal qLn(qreal v)
222{
223#ifdef QT_USE_MATH_H_FLOATS
224 if (sizeof(qreal) == sizeof(float))
225 return logf(float(v));
226 else
227#endif
228 return log(v);
229}
230
231inline qreal qExp(qreal v)
232{
233#ifdef Q_OS_SYMBIAN
234 TReal exp_v;
235 Math::Exp(exp_v, static_cast<TReal>(v));
236 return static_cast<qreal>(exp_v);
237#else
238 // only one signature
239 // exists, exp(double)
240 return exp(v);
241#endif
242}
243
244inline qreal qPow(qreal x, qreal y)
245{
246#ifdef Q_OS_SYMBIAN
247 TReal pow_v;
248 Math::Pow(pow_v, static_cast<TReal>(x), static_cast<TReal>(y));
249 return static_cast<qreal>(pow_v);
250#else
251# ifdef QT_USE_MATH_H_FLOATS
252 if (sizeof(qreal) == sizeof(float))
253 return powf(float(x), float(y));
254 else
255# endif
256 return pow(x, y);
257#endif
258}
259
260#ifndef M_PI
261#define M_PI (3.14159265358979323846)
262#endif
263
264inline qreal qFastSin(qreal x)
265{
266 int si = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
267 qreal d = x - si * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
268 int ci = si + QT_SINE_TABLE_SIZE / 4;
269 si &= QT_SINE_TABLE_SIZE - 1;
270 ci &= QT_SINE_TABLE_SIZE - 1;
271 return qt_sine_table[si] + (qt_sine_table[ci] - 0.5 * qt_sine_table[si] * d) * d;
272}
273
274inline qreal qFastCos(qreal x)
275{
276 int ci = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
277 qreal d = x - ci * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
278 int si = ci + QT_SINE_TABLE_SIZE / 4;
279 si &= QT_SINE_TABLE_SIZE - 1;
280 ci &= QT_SINE_TABLE_SIZE - 1;
281 return qt_sine_table[si] - (qt_sine_table[ci] + 0.5 * qt_sine_table[si] * d) * d;
282}
283
284QT_END_NAMESPACE
285
286QT_END_HEADER
287
288#endif // QMATH_H
289