tbx  0.7.6
fixed16.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2012-2013 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef TBX_FIXED16_H_
26 #define TBX_FIXED16_H_
27 
28 #include <ostream>
29 #include <istream>
30 
31 namespace tbx
32 {
39  class Fixed16
40  {
41  private:
42  int _bits;
43 
44  public:
45  Fixed16() {};
46  Fixed16(const Fixed16 &other) {_bits = other._bits;}
47  Fixed16(int value) {_bits = value<<16;}
48  Fixed16(double value) {_bits = int(value * 65536.0);}
49 
50  operator int() const {return _bits>>16;}
51  operator double() const {return double(_bits)/65536.0;}
52 
56  int bits() const {return _bits;}
57 
58  Fixed16 &operator=(const Fixed16 &other) {_bits = other._bits; return *this;}
59 
60  bool operator<(const Fixed16 &other) {return _bits < other._bits;}
61  bool operator>(const Fixed16 &other) {return _bits > other._bits;}
62  bool operator<=(const Fixed16 &other) {return _bits <= other._bits;}
63  bool operator>=(const Fixed16 &other) {return _bits >= other._bits;}
64 
65  Fixed16 &operator++() {_bits += 65536;return *this;}
66  Fixed16 &operator--() {_bits -= 65536;return *this;}
67  Fixed16 operator++(int) {Fixed16 temp(*this); _bits+= 65536; return temp;}
68  Fixed16 operator--(int) {Fixed16 temp(*this); _bits-= 65536; return temp;}
69 
70  Fixed16 &operator+=(const Fixed16 &other) {_bits += other._bits; return *this;}
71  Fixed16 &operator-=(const Fixed16 &other) {_bits -= other._bits; return *this;}
72  Fixed16 &operator*=(const Fixed16 &other) {_bits = (int)(((long long)_bits * (long long)other._bits) >> 16); return *this;}
73  Fixed16 &operator/=(const Fixed16 &other) {_bits = (int)((((long long)_bits) << 16) / other._bits); return *this;}
74 
75  Fixed16 operator-() {Fixed16 temp;temp._bits = -_bits; return temp;}
76 
77  // Integer on RHS operators
78  Fixed16 &operator=(int value) {_bits = value << 16; return *this;}
79 
80  bool operator==(int value) const {return ((_bits & 0xFFFF) == 0) && ((_bits>>16) == value);}
81  bool operator!=(int value) const {return !operator==(value);}
82  bool operator<(int value) const {return (_bits>>16) < value;}
83  bool operator>(int value) const {return ((_bits>>16) > value) || (((_bits>>16) == value) && ((_bits & 0xFFFF) != 0));}
84  bool operator<=(int value) const {return !operator>(value);}
85  bool operator>=(int value) const {return !operator<(value);}
86 
87  Fixed16 &operator+=(int value) {_bits += value<<16; return *this;}
88  Fixed16 &operator-=(int value) {_bits -= value<<16; return *this;}
89  Fixed16 &operator*=(int value) {_bits = (int)(((long long)_bits * (long long)value)); return *this;}
90  Fixed16 &operator/=(int value) {_bits /= value; return *this;}
91 
92  // Double on RHS operators
93  Fixed16 &operator=(double value) {_bits = int(value * 65536.0); return *this;}
94 
95  bool operator==(double value) const {return double(_bits) / 65536.0 == value;}
96  bool operator!=(double value) const {return !operator==(value);}
97  bool operator<(double value) const {return double(_bits) / 65536.0 < value;}
98  bool operator>(double value) const {return double(_bits) / 65536.0 > value;}
99  bool operator<=(double value) const {return double(_bits) / 65536.0 <= value;}
100  bool operator>=(double value) const {return double(_bits) / 65536.0 >= value;}
101 
102  Fixed16 &operator+=(double value) {_bits += int(value * 65536.0); return *this;}
103  Fixed16 &operator-=(double value) {_bits -= int(value * 65536.0); return *this;}
104  Fixed16 &operator*=(double value) {_bits = int(double(_bits) * value); return *this;}
105  Fixed16 &operator/=(double value) {_bits = int(double(_bits) / value); return *this;}
106  };
107 
108  inline bool operator==(const Fixed16 &lhs, const Fixed16 &rhs) {return lhs.bits() == rhs.bits();}
109  inline bool operator!=(const Fixed16 &lhs, const Fixed16 &rhs) {return lhs.bits() != rhs.bits();}
110 
111  // Integer on LHS operator
112  inline bool operator==(int lhs, const Fixed16 &rhs) {return rhs == lhs;}
113  inline bool operator!=(int lhs, const Fixed16 &rhs) {return rhs != lhs;}
114  inline bool operator<(int lhs, const Fixed16 &rhs) {return rhs >= lhs;}
115  inline bool operator>(int lhs, const Fixed16 &rhs) {return rhs <= lhs;}
116  inline bool operator<=(int lhs, const Fixed16 &rhs) {return rhs > lhs;}
117  inline bool operator>=(int lhs, const Fixed16 &rhs) {return rhs < lhs;}
118 
119  inline Fixed16 &operator+=(int &lhs, const Fixed16 &rhs) {Fixed16 temp(rhs); return temp+=lhs;}
120  inline Fixed16 &operator-=(int &lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp+=rhs;}
121  inline Fixed16 &operator*=(int &lhs, const Fixed16 &rhs) {Fixed16 temp(rhs); return temp*=lhs;}
122  inline Fixed16 &operator/=(int &lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp/=rhs;}
123 
124  inline Fixed16 operator+(const Fixed16 &lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp+=rhs;}
125  inline Fixed16 operator-(const Fixed16 &lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp-=rhs;}
126  inline Fixed16 operator*(const Fixed16 &lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp*=rhs;}
127  inline Fixed16 operator/(const Fixed16 &lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp/=rhs;}
128 
129  inline Fixed16 operator+(int lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp+=rhs;}
130  inline Fixed16 operator+(const Fixed16 &lhs, int rhs) {Fixed16 temp(lhs); return temp+=rhs;}
131  inline Fixed16 operator-(int lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp-=rhs;}
132  inline Fixed16 operator-(const Fixed16 &lhs, int rhs) {Fixed16 temp(lhs); return temp-=rhs;}
133  inline Fixed16 operator*(int lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp*=rhs;}
134  inline Fixed16 operator*(const Fixed16 &lhs, int rhs) {Fixed16 temp(lhs); return temp*=rhs;}
135  inline Fixed16 operator/(int lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp/=rhs;}
136  inline Fixed16 operator/(const Fixed16 &lhs, int rhs) {Fixed16 temp(lhs); return temp/=rhs;}
137 
138  // double on LHS operator
139  inline bool operator==(double lhs, const Fixed16 &rhs) {return rhs == lhs;}
140  inline bool operator!=(double lhs, const Fixed16 &rhs) {return rhs != lhs;}
141  inline bool operator<(double lhs, const Fixed16 &rhs) {return rhs >= lhs;}
142  inline bool operator>(double lhs, const Fixed16 &rhs) {return rhs <= lhs;}
143  inline bool operator<=(double lhs, const Fixed16 &rhs) {return rhs > lhs;}
144  inline bool operator>=(double lhs, const Fixed16 &rhs) {return rhs < lhs;}
145 
146  inline double &operator+=(double &lhs, const Fixed16 &rhs) {lhs += (double)rhs; return lhs;}
147  inline double &operator-=(double &lhs, const Fixed16 &rhs) {lhs -= (double)rhs; return lhs;}
148  inline double &operator*=(double &lhs, const Fixed16 &rhs) {lhs *= (double)rhs; return lhs;}
149  inline double &operator/=(double &lhs, const Fixed16 &rhs) {lhs /= (double)rhs; return lhs;}
150 
151  inline Fixed16 operator+(double lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp+=rhs;}
152  inline Fixed16 operator+(const Fixed16 &lhs, double rhs) {Fixed16 temp(lhs); return temp+=rhs;}
153  inline Fixed16 operator-(double lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp-=rhs;}
154  inline Fixed16 operator-(const Fixed16 &lhs, double rhs) {Fixed16 temp(lhs); return temp-=rhs;}
155  inline Fixed16 operator*(double lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp*=rhs;}
156  inline Fixed16 operator*(const Fixed16 &lhs, double rhs) {Fixed16 temp(lhs); return temp*=rhs;}
157  inline Fixed16 operator/(double lhs, const Fixed16 &rhs) {Fixed16 temp(lhs); return temp/=rhs;}
158  inline Fixed16 operator/(const Fixed16 &lhs, double rhs) {Fixed16 temp(lhs); return temp/=rhs;}
159 
160  inline std::ostream& operator<<(std::ostream &os, const Fixed16 &num) {os << double(num); return os;}
161  inline std::istream& operator>>(std::istream &is, Fixed16 &num) {double val; is >> val; num = val; return is;}
162 };
163 
164 #endif
165 
tbx
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:35
tbx::Fixed16
Class to represent a fixed point number with 16bits before and after the point.
Definition: fixed16.h:40
tbx::Fixed16::bits
int bits() const
Get bits used to represent this number.
Definition: fixed16.h:56