25 #ifndef TBX_FIXED16_H_
26 #define TBX_FIXED16_H_
47 Fixed16(
int value) {_bits = value<<16;}
48 Fixed16(
double value) {_bits = int(value * 65536.0);}
50 operator int()
const {
return _bits>>16;}
51 operator double()
const {
return double(_bits)/65536.0;}
56 int bits()
const {
return _bits;}
58 Fixed16 &operator=(
const Fixed16 &other) {_bits = other._bits;
return *
this;}
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;}
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;}
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;}
75 Fixed16 operator-() {Fixed16 temp;temp._bits = -_bits;
return temp;}
78 Fixed16 &operator=(
int value) {_bits = value << 16;
return *
this;}
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);}
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;}
93 Fixed16 &operator=(
double value) {_bits = int(value * 65536.0);
return *
this;}
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;}
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;}
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();}
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;}
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;}
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;}
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;}
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;}
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;}
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;}
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;}