23 template <
int FRAC_BITS=10>
class fixed {
26 static const int int_bits = 31 - FRAC_BITS;
29 fixed(
int in_value,
bool) : value(in_value) { ; }
32 fixed(
const fixed & in_value) : value (in_value.value) { ; }
33 fixed(
int in_value) : value(in_value << FRAC_BITS) { ; }
34 fixed(
double in_value) : value(in_value * (1 << FRAC_BITS)) { ; }
37 int bits()
const {
return value; }
38 int AsInt()
const {
return value >> FRAC_BITS; }
39 double AsDouble()
const {
return ((
double) value) / ((double) (1 << FRAC_BITS)); }
43 fixed &
operator=(
double other) { value = other * (1 << FRAC_BITS);
return *
this; }
49 const int frac_mask = (1 << FRAC_BITS) - 1;
50 value = (value & frac_mask) * (rhs.value >> FRAC_BITS) + (value >> FRAC_BITS) * rhs.value
51 + ((value & frac_mask) * (rhs.value & frac_mask) >> FRAC_BITS);
56 value = (((long) value) << FRAC_BITS) / rhs.value;
66 return fixed(lhs.value + rhs.value,
true);
69 return fixed(lhs.value - rhs.value,
true);
73 const int frac_mask = (1 << FRAC_BITS) - 1;
74 const int new_value = (lhs.value & frac_mask) * (rhs.value >> FRAC_BITS)
75 + (lhs.value >> FRAC_BITS) * rhs.value
76 + ((lhs.value & frac_mask) * (rhs.value & frac_mask) >> FRAC_BITS);
77 return fixed(new_value,
true);
81 const int new_value = (((long) lhs.value) << FRAC_BITS) / rhs.value;
82 return fixed(new_value,
true);
86 {
return lhs.value == rhs.value; }
88 {
return lhs.value != rhs.value; }
90 {
return lhs.value < rhs.value; }
92 {
return lhs.value <= rhs.value; }
94 {
return lhs.value > rhs.value; }
96 {
return lhs.value >= rhs.value; }
104 template <
int FRAC_BITS> std::ostream &
operator<<(std::ostream & os,
109 template <
int FRAC_BITS> std::istream &
operator>>(std::istream & is,
fixed(const fixed &in_value)
Definition: fixed.h:32
~fixed()
Definition: fixed.h:35
friend bool operator<=(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:91
fixed & operator=(const fixed &other)
Definition: fixed.h:41
friend bool operator==(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:85
friend bool operator>=(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:95
friend bool operator>(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:93
friend fixed operator+(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:65
fixed operator--(int)
Definition: fixed.h:63
fixed()
Definition: fixed.h:31
fixed & operator--()
Definition: fixed.h:62
std::istream & operator>>(std::istream &is, emp::Ptr< T > &ptr)
Definition: Ptr.h:808
fixed & operator+=(const fixed &rhs)
Definition: fixed.h:45
fixed(double in_value)
Definition: fixed.h:34
int bits() const
Definition: fixed.h:37
friend fixed operator*(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:71
fixed & operator=(double other)
Definition: fixed.h:43
int AsInt() const
Definition: fixed.h:38
std::ostream & operator<<(std::ostream &out, const emp::Ptr< T > &ptr)
Definition: Ptr.h:800
friend bool operator!=(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:87
fixed & operator-=(const fixed &rhs)
Definition: fixed.h:46
fixed & operator=(int other)
Definition: fixed.h:42
friend fixed operator/(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:79
fixed & operator/=(const fixed &rhs)
Definition: fixed.h:54
fixed & operator*=(const fixed &rhs)
Definition: fixed.h:47
friend bool operator<(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:89
If we are in emscripten, make sure to include the header.
Definition: array.h:37
fixed & operator++()
Definition: fixed.h:60
double AsDouble() const
Definition: fixed.h:39
friend fixed operator-(const fixed &lhs, const fixed &rhs)
Definition: fixed.h:68
fixed operator++(int)
Definition: fixed.h:61
fixed(int in_value)
Definition: fixed.h:33