summaryrefslogtreecommitdiff
path: root/library/qmath.c
Unidiff
Diffstat (limited to 'library/qmath.c') (more/less context) (ignore whitespace changes)
-rw-r--r--library/qmath.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/library/qmath.c b/library/qmath.c
new file mode 100644
index 0000000..7af8706
--- a/dev/null
+++ b/library/qmath.c
@@ -0,0 +1,157 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include <math.h>
22#include <float.h>
23#include "qmath.h"
24
25#ifdef QT_QWS_CASSIOPEIA
26
27double qFabs( double a )
28{
29 if ( a < 0.0 )
30 return -a;
31 return a;
32}
33
34double qSqrt( double value )
35{
36 const double tol = 0.000005; // relative error tolerance
37 double old_app, new_app;
38 if (value == 0.0)
39 return 0.0;
40 old_app = value; // take value as first approximation
41 new_app = (old_app + value/old_app)/2;
42 while (qFabs((new_app-old_app)/new_app) > tol)
43 {
44 old_app = new_app;
45 new_app = (old_app + value/old_app)/2;
46 }
47
48 return new_app;
49}
50
51const double Q_PI = 3.14159265358979323846; // pi
52const double Q_2PI = 6.28318530717958647693; // 2*pi
53const double Q_PI2 = 1.57079632679489661923; // pi/2
54
55static double qsincos( double a, int calcCos )
56{
57 int sign;
58 double a2;
59 double a3;
60 double a5;
61 double a7;
62 double a9;
63 double a11;
64
65 if ( calcCos ) // calculate cosine
66 a -= Q_PI2;
67 if ( a >= Q_2PI || a <= -Q_2PI ) { // fix range: -2*pi < a < 2*pi
68 int m = (int)(a/Q_2PI);
69 a -= Q_2PI*m;
70 }
71 if ( a < 0.0 ) // 0 <= a < 2*pi
72 a += Q_2PI;
73 sign = a > Q_PI ? -1 : 1;
74 if ( a >= Q_PI )
75 a = Q_2PI - a;
76 if ( a >= Q_PI2 )
77 a = Q_PI - a;
78 if ( calcCos )
79 sign = -sign;
80 a2 = a*a; // here: 0 <= a < pi/4
81 a3 = a2*a; // make taylor sin sum
82 a5 = a3*a2;
83 a7 = a5*a2;
84 a9 = a7*a2;
85 a11 = a9*a2;
86 return (a-a3/6+a5/120-a7/5040+a9/362880-a11/39916800)*sign;
87}
88
89double qSin( double a ) { return qsincos(a,0); }
90double qCos( double a ) { return qsincos(a,1); }
91
92//atan2 returns values from -PI to PI, so we have to do the same
93double qATan2( double y, double x )
94{
95 double r;
96 if ( x != 0.0 ) {
97 double a = qFabs(y/x);
98 if ( a <= 1 )
99 r = a/(1+ 0.28*a*a);
100 else
101 r = Q_PI2 - a/(a*a + 0.28);
102 } else {
103 r = Q_PI2;
104 }
105
106 if ( y >= 0.0 ) {
107 if ( x >= 0.0 )
108 return r;
109 else
110 return Q_PI - r;
111 } else {
112 if ( x >= 0.0 )
113 return 0.0 - r;
114 else
115 return -Q_PI + r;
116 }
117}
118
119double qATan( double a )
120{
121 return qATan2( a, 1.0 );
122}
123
124double qASin( double a )
125{
126 return qATan2( a, qSqrt(1-a*a) );
127}
128
129double qTan( double a )
130{
131 double ca = qCos(a);
132 if ( ca != 0.0 )
133 return qSin( a ) / ca;
134
135 return MAXDOUBLE;
136}
137
138double qFloor( double a )
139{
140 long i = (long) a;
141 return (double) i;
142}
143
144#else
145
146double qSqrt( double value ) { return sqrt( value ); }
147double qSin( double a ) { return sin(a); }
148double qCos( double a ) { return cos(a); }
149double qATan2( double y, double x ) { return atan2(y,x); }
150double qATan( double a ) { return atan(a); }
151double qASin( double a ) { return asin(a); }
152double qTan( double a ) { return tan(a); }
153double qFloor( double a ) { return floor(a); }
154double qFabs( double a ) { return fabs(a); }
155
156#endif
157