1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mcvmath.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 #include <tools/solar.h>
40 // allowed range 0..14, must be even
43 // scale for ...Big() -Functions
46 #define FIX_P3 (FIX_POST-FIX_P2)
49 #define FIX_P3 FIX_POST
53 #define FIX_ADD (1<<(FIX_POST-1))
59 #define FIX_A2 (1<<(FIX_P2-1))
65 #define FIX_A3 (1<<(FIX_P3-1))
78 friend class ColWheel
;
80 // friend Fix ImpMultBig2( const Fix& a, const Fix& b );
87 Fix( int i
) { x
=(long(i
)<<FIX_POST
); }
88 Fix( short l
) { x
=(long(l
)<<FIX_POST
); }
89 Fix( USHORT l
) { x
=(long(l
)<<FIX_POST
); }
90 Fix( long l
) { x
=(l
<<FIX_POST
); }
91 Fix( long Z
, long N
) { x
=(Z
<<FIX_POST
)/N
; }
93 void SetInternVal( long nVal
) { x
=nVal
; }
94 long GetInternVal() const { return x
; }
96 void operator+= ( const Fix
& a
) { x
+=a
.x
; }
97 void operator-= ( const Fix
& a
) { x
-=a
.x
; }
98 void operator*= ( const Fix
& a
) { x
=(x
*a
.x
+FIX_ADD
)>>FIX_POST
; }
99 void operator/= ( const Fix
& a
) { x
=(x
<<FIX_POST
)/a
.x
; }
100 friend Fix
operator- ( const Fix
& a
);
102 void MultBig( const Fix
& a
)
103 { x
=((((a
.x
+FIX_A2
)>>FIX_P2
)*x
+FIX_A3
)>>FIX_P3
); }
104 void DivBig( const Fix
& a
)
105 { x
=((x
<<FIX_P3
)/a
.x
)<<FIX_P2
; }
107 friend BOOL
operator> ( const Fix
& a
, const Fix
& b
) { return a
.x
> b
.x
; }
108 friend BOOL
operator< ( const Fix
& a
, const Fix
& b
) { return a
.x
< b
.x
; }
110 operator long() const { return (x
+FIX_ADD
) >> FIX_POST
; }
111 operator double() const { return double(x
)/(1<<FIX_POST
); }
113 friend Fix
operator+ ( const Fix
& a
, const Fix
& b
);
114 friend Fix
operator- ( const Fix
& a
, const Fix
& b
);
115 friend Fix
operator* ( const Fix
& a
, const Fix
& b
);
116 friend Fix
operator/ ( const Fix
& a
, const Fix
& b
);
118 friend FixCpx
operator-( const FixCpx
& a
);
127 // friend FixCpx ImpMultBig2( const FixCpx& ra, const FixCpx& rb );
134 FixCpx() : r(), i() {}
135 FixCpx( Fix a
) : r( a
), i() {}
136 FixCpx( Fix a
, Fix b
) : r( a
), i( b
) {}
138 Fix
& GetReal() { return r
; }
139 Fix
& GetImag() { return i
; }
141 void operator*= ( const FixCpx
& ra
);
142 void MultBig( const FixCpx
& ra
, const FixCpx
& rb
);
144 friend FixCpx
operator+ ( const FixCpx
& a
, const FixCpx
& b
);
145 friend FixCpx
operator- ( const FixCpx
& a
, const FixCpx
& b
);
146 friend FixCpx
operator* ( const FixCpx
& a
, const FixCpx
& b
);
147 friend FixCpx
operator/ ( const FixCpx
& a
, const FixCpx
& b
);
148 friend FixCpx
operator- ( const FixCpx
& a
);
151 inline Fix
operator- ( const Fix
& a
)
158 inline Fix
operator+ ( const Fix
& a
, const Fix
& b
)
164 inline Fix
operator- ( const Fix
& a
, const Fix
& b
)
170 inline Fix
operator* ( const Fix
& a
, const Fix
& b
)
172 long l
=(a
.x
*b
.x
+FIX_ADD
)>>FIX_POST
;
176 inline Fix
operator/ ( const Fix
& a
, const Fix
& b
)
178 long l
=(a
.x
<<FIX_POST
)/b
.x
;
182 inline FixCpx
operator- ( const FixCpx
& a
)
191 inline FixCpx
operator+ ( const FixCpx
& a
, const FixCpx
& b
)
193 return FixCpx( a
.r
+b
.r
, a
.i
+b
.i
);
196 inline FixCpx
operator- ( const FixCpx
& a
, const FixCpx
& b
)
198 return FixCpx( a
.r
-b
.r
, a
.i
-b
.i
);
201 inline void FixCpx::operator*= ( const FixCpx
& ra
)
203 Fix rr
= ra
.r
*r
-ra
.i
*i
;
208 inline FixCpx
operator* ( const FixCpx
& a
, const FixCpx
& b
)
210 return FixCpx( a
.r
*b
.r
-a
.i
*b
.i
, a
.r
*b
.i
+a
.i
*b
.r
);
213 inline FixCpx
operator/ ( const FixCpx
& a
, const FixCpx
& b
)
215 return FixCpx( (a
.r
*b
.r
+a
.i
*b
.i
)/(b
.r
*b
.r
+b
.i
*b
.i
),
216 (b
.r
*a
.r
-a
.r
*b
.i
)/(b
.r
*b
.r
+b
.i
*b
.i
) );
219 // -----------------------------------------------------------------------
221 Fix
ImpMultBig2( const Fix
& a
, const Fix
& b
);
222 FixCpx
ImpMultBig2( const FixCpx
& ra
, const FixCpx
& rb
);
224 void ImpCartToPolar( const short x
, const short y
, Fix
& rRad
, USHORT
& rPhi
);
225 void ImpPolarToCart( const Fix
& rR
, const USHORT Phi
, short& rX
, short& rY
);
227 USHORT
ImpSqrt( ULONG nRadi
);
228 USHORT
ImpATan2( const short x
, const short y
);
229 FixCpx
ImpExPI( USHORT nPhi
);
231 #endif // _MCVMATH_HXX