qmath.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtCore module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QMATH_H
00043 #define QMATH_H
00044 
00045 #include <math.h>
00046 
00047 #include <QtCore/qglobal.h>
00048 
00049 #ifdef Q_OS_SYMBIAN
00050 #    include <e32math.h>
00051 #endif
00052 
00053 QT_BEGIN_HEADER
00054 
00055 QT_BEGIN_NAMESPACE
00056 
00057 QT_MODULE(Core)
00058 
00059 #define QT_SINE_TABLE_SIZE 256
00060 
00061 extern Q_CORE_EXPORT const qreal qt_sine_table[QT_SINE_TABLE_SIZE];
00062 
00063 inline int qCeil(qreal v)
00064 {
00065 #ifdef QT_USE_MATH_H_FLOATS
00066     if (sizeof(qreal) == sizeof(float))
00067         return int(ceilf(float(v)));
00068     else
00069 #endif
00070         return int(ceil(v));
00071 }
00072 
00073 inline int qFloor(qreal v)
00074 {
00075 #ifdef QT_USE_MATH_H_FLOATS
00076     if (sizeof(qreal) == sizeof(float))
00077         return int(floorf(float(v)));
00078     else
00079 #endif
00080         return int(floor(v));
00081 }
00082 
00083 inline qreal qFabs(qreal v)
00084 {
00085 #ifdef QT_USE_MATH_H_FLOATS
00086     if(sizeof(qreal) == sizeof(float))
00087         return fabsf(float(v));
00088     else
00089 #endif
00090         return fabs(v);
00091 }
00092 
00093 inline qreal qSin(qreal v)
00094 {
00095 #ifdef Q_OS_SYMBIAN
00096     TReal sin_v;
00097     Math::Sin(sin_v, static_cast<TReal>(v));
00098     return static_cast<qreal>(sin_v);
00099 #else
00100 #    ifdef QT_USE_MATH_H_FLOATS
00101         if (sizeof(qreal) == sizeof(float))
00102             return sinf(float(v));
00103         else
00104 #    endif
00105             return sin(v);
00106 #endif
00107 }
00108 
00109 inline qreal qCos(qreal v)
00110 {
00111 #ifdef Q_OS_SYMBIAN
00112     TReal cos_v;
00113     Math::Cos(cos_v, static_cast<TReal>(v));
00114     return static_cast<qreal>(cos_v);
00115 #else
00116 #    ifdef QT_USE_MATH_H_FLOATS
00117         if (sizeof(qreal) == sizeof(float))
00118             return cosf(float(v));
00119         else
00120 #    endif
00121             return cos(v);
00122 #endif
00123 }
00124 
00125 inline qreal qTan(qreal v)
00126 {
00127 #ifdef Q_OS_SYMBIAN
00128     TReal tan_v;
00129     Math::Tan(tan_v, static_cast<TReal>(v));
00130     return static_cast<qreal>(tan_v);
00131 #else
00132 #    ifdef QT_USE_MATH_H_FLOATS
00133         if (sizeof(qreal) == sizeof(float))
00134             return tanf(float(v));
00135         else
00136 #    endif
00137             return tan(v);
00138 #endif
00139 }
00140 
00141 inline qreal qAcos(qreal v)
00142 {
00143 #ifdef Q_OS_SYMBIAN
00144     TReal acos_v;
00145     Math::ACos(acos_v, static_cast<TReal>(v));
00146     return static_cast<qreal>(acos_v);
00147 #else
00148 #    ifdef QT_USE_MATH_H_FLOATS
00149         if (sizeof(qreal) == sizeof(float))
00150             return acosf(float(v));
00151         else
00152 #    endif
00153            return acos(v);
00154 #endif
00155 }
00156 
00157 inline qreal qAsin(qreal v)
00158 {
00159 #ifdef Q_OS_SYMBIAN
00160     TReal asin_v;
00161     Math::ASin(asin_v, static_cast<TReal>(v));
00162     return static_cast<qreal>(asin_v);
00163 #else
00164 #    ifdef QT_USE_MATH_H_FLOATS
00165         if (sizeof(qreal) == sizeof(float))
00166             return asinf(float(v));
00167         else
00168 #    endif
00169             return asin(v);
00170 #endif
00171 }
00172 
00173 inline qreal qAtan(qreal v)
00174 {
00175 #ifdef Q_OS_SYMBIAN
00176     TReal atan_v;
00177     Math::ATan(atan_v, static_cast<TReal>(v));
00178     return static_cast<qreal>(atan_v);
00179 #else
00180 #    ifdef QT_USE_MATH_H_FLOATS
00181         if(sizeof(qreal) == sizeof(float))
00182             return atanf(float(v));
00183         else
00184 #    endif
00185             return atan(v);
00186 #endif
00187 }
00188 
00189 inline qreal qAtan2(qreal x, qreal y)
00190 {
00191 #ifdef Q_OS_SYMBIAN
00192     TReal atan2_v;
00193     Math::ATan(atan2_v, static_cast<TReal>(x), static_cast<TReal>(y));
00194     return static_cast<qreal>(atan2_v);
00195 #else
00196 #    ifdef QT_USE_MATH_H_FLOATS
00197         if(sizeof(qreal) == sizeof(float))
00198             return atan2f(float(x), float(y));
00199         else
00200 #    endif
00201             return atan2(x, y);
00202 #endif
00203 }
00204 
00205 inline qreal qSqrt(qreal v)
00206 {
00207 #ifdef Q_OS_SYMBIAN
00208     TReal sqrt_v;
00209     Math::Sqrt(sqrt_v, static_cast<TReal>(v));
00210     return static_cast<qreal>(sqrt_v);
00211 #else
00212 #    ifdef QT_USE_MATH_H_FLOATS
00213         if (sizeof(qreal) == sizeof(float))
00214             return sqrtf(float(v));
00215         else
00216 #    endif
00217             return sqrt(v);
00218 #endif
00219 }
00220 
00221 inline qreal qLn(qreal v)
00222 {
00223 #ifdef QT_USE_MATH_H_FLOATS
00224     if (sizeof(qreal) == sizeof(float))
00225         return logf(float(v));
00226     else
00227 #endif
00228         return log(v);
00229 }
00230 
00231 inline qreal qExp(qreal v)
00232 {
00233 #ifdef Q_OS_SYMBIAN
00234     TReal exp_v;
00235     Math::Exp(exp_v, static_cast<TReal>(v));
00236     return static_cast<qreal>(exp_v);
00237 #else
00238     // only one signature
00239     // exists, exp(double)
00240     return exp(v);
00241 #endif
00242 }
00243 
00244 inline qreal qPow(qreal x, qreal y)
00245 {
00246 #ifdef Q_OS_SYMBIAN
00247     TReal pow_v;
00248     Math::Pow(pow_v, static_cast<TReal>(x), static_cast<TReal>(y));
00249     return static_cast<qreal>(pow_v);
00250 #else
00251 #    ifdef QT_USE_MATH_H_FLOATS
00252         if (sizeof(qreal) == sizeof(float))
00253             return powf(float(x), float(y));
00254         else
00255 #    endif
00256             return pow(x, y);
00257 #endif
00258 }
00259 
00260 #ifndef M_PI
00261 #define M_PI (3.14159265358979323846)
00262 #endif
00263 
00264 inline qreal qFastSin(qreal x)
00265 {
00266     int si = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
00267     qreal d = x - si * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
00268     int ci = si + QT_SINE_TABLE_SIZE / 4;
00269     si &= QT_SINE_TABLE_SIZE - 1;
00270     ci &= QT_SINE_TABLE_SIZE - 1;
00271     return qt_sine_table[si] + (qt_sine_table[ci] - 0.5 * qt_sine_table[si] * d) * d;
00272 }
00273 
00274 inline qreal qFastCos(qreal x)
00275 {
00276     int ci = int(x * (0.5 * QT_SINE_TABLE_SIZE / M_PI)); // Would be more accurate with qRound, but slower.
00277     qreal d = x - ci * (2.0 * M_PI / QT_SINE_TABLE_SIZE);
00278     int si = ci + QT_SINE_TABLE_SIZE / 4;
00279     si &= QT_SINE_TABLE_SIZE - 1;
00280     ci &= QT_SINE_TABLE_SIZE - 1;
00281     return qt_sine_table[si] - (qt_sine_table[ci] + 0.5 * qt_sine_table[si] * d) * d;
00282 }
00283 
00284 QT_END_NAMESPACE
00285 
00286 QT_END_HEADER
00287 
00288 #endif // QMATH_H