Project 2 - Fixed-Point Number Class
fixed.hpp
Go to the documentation of this file.
1 
26 #ifndef FIXED_HPP_
27 #define FIXED_HPP_
28 
29 #include <istream>
30 #include <ostream>
31 #include <string>
32 
37 class fixed
38 {
39 public:
40  typedef int value_type;
41 
42  static int const places = 4;
43  static value_type const places10 = 10000;
44 
46  fixed() : value_{0} {}
47 
52 
55 
57  fixed(double value)
58  : value_(static_cast<value_type>(value * places10 + (value < 0 ? -0.5 : 0.5)))
59  {}
60 
63  std::string as_string() const;
68  bool read(std::istream& strm);
70  double as_long_double() const { return static_cast<long double>(value()) / places10; }
72  double as_double() const { return static_cast<double>(value()) / places10; }
74  float as_float() const { return static_cast<float>(value()) / places10; }
78  value_type round() const;
79 
81  value_type integer() const { return value() / places10; }
83  value_type fraction() const;
84 
93 
95  void negate();
96 
98  fixed& operator++();
100  fixed operator++(int);
102  fixed& operator--();
104  fixed operator--(int);
105 
107  value_type value() const { return value_; }
108 private:
110  value_type reduce(value_type frac);
111  value_type value_;
112 };
113 
115 std::istream& operator>>(std::istream& strm, fixed& f);
117 std::ostream& operator<<(std::ostream& strm, fixed f);
118 
120 fixed operator+(fixed a, fixed b);
122 fixed operator-(fixed a, fixed b);
124 fixed operator*(fixed a, fixed b);
126 fixed operator/(fixed a, fixed b);
129 
131 bool operator==(fixed a, fixed b);
133 bool operator!=(fixed a, fixed b);
135 bool operator<(fixed a, fixed b);
137 bool operator>(fixed a, fixed b);
139 bool operator<=(fixed a, fixed b);
141 bool operator>=(fixed a, fixed b);
142 
143 #endif
fixed & operator*=(fixed f)
Multiplication assignment operator.
Definition: fixed.cpp:141
Implement a fixed-point number class. Values have places places after the decimal point...
Definition: fixed.hpp:37
fixed operator*(fixed a, fixed b)
Multiply fixed values.
Definition: fixed.cpp:268
std::string as_string() const
Definition: fixed.cpp:120
bool operator>=(fixed a, fixed b)
Compare fixed values for greater-than-or-equal by comparing the underlying values.
Definition: fixed.cpp:305
bool operator<=(fixed a, fixed b)
Compare fixed values for less-than-or-equal by comparing the underlying values.
Definition: fixed.cpp:300
bool operator<(fixed a, fixed b)
Compare fixed values for less-than by comparing the underlying values.
Definition: fixed.cpp:290
value_type value() const
Return the internal value.
Definition: fixed.hpp:107
value_type integer() const
Return the integer part (which is the same as trunc()).
Definition: fixed.hpp:81
double as_double() const
Convert to double.
Definition: fixed.hpp:72
std::ostream & operator<<(std::ostream &strm, fixed f)
Write a fixed value.
Definition: fixed.cpp:250
value_type fraction() const
Return the fractional part, e.g., 3 for 12.03.
Definition: fixed.cpp:31
bool operator!=(fixed a, fixed b)
Compare fixed values for inequality by comparing the underlying values.
Definition: fixed.cpp:285
static int const places
number of decimal places
Definition: fixed.hpp:42
bool operator>(fixed a, fixed b)
Compare fixed values for greater-than by comparing the underlying values.
Definition: fixed.cpp:295
fixed & operator++()
Pre-increment.
Definition: fixed.cpp:158
fixed & operator+=(fixed f)
Addition assignment operator.
Definition: fixed.cpp:129
float as_float() const
Convert to float.
Definition: fixed.hpp:74
fixed & operator/=(fixed f)
Division assignment operator.
Definition: fixed.cpp:147
double as_long_double() const
Convert to long double.
Definition: fixed.hpp:70
fixed & operator--()
Pre-decrement.
Definition: fixed.cpp:171
fixed operator/(fixed a, fixed b)
Divide fixed values.
Definition: fixed.cpp:274
void negate()
Negate this value.
Definition: fixed.cpp:153
fixed & operator-=(fixed f)
Subtraction assignment operator.
Definition: fixed.cpp:135
std::istream & operator>>(std::istream &strm, fixed &f)
Read a fixed value.
Definition: fixed.cpp:243
bool operator==(fixed a, fixed b)
Compare fixed values for equality by comparing the underlying values.
Definition: fixed.cpp:280
static value_type const places10
10places
Definition: fixed.hpp:43
fixed operator-(fixed a, fixed b)
Subtract fixed values.
Definition: fixed.cpp:262
int value_type
Type of the actual value.
Definition: fixed.hpp:40
fixed(double value)
Construct by rounding off a floating point number.
Definition: fixed.hpp:57
fixed operator+(fixed a, fixed b)
Add fixed values.
Definition: fixed.cpp:256
value_type round() const
Definition: fixed.cpp:104
fixed()
Default constructor initializes to zero.
Definition: fixed.hpp:46
bool read(std::istream &strm)
Definition: fixed.cpp:190