Project 2 - Fixed-Point Number Class

- Main Page
- Classes
- Files

- File List
- File Members

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
51 fixed(value_type integer, value_type fraction);
52
54 fixed(value_type integer);
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
86 fixed& operator+=(fixed f);
88 fixed& operator-=(fixed f);
90 fixed& operator*=(fixed f);
92 fixed& operator/=(fixed f);
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);
128 fixed operator-(fixed a);
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 & operator*=(fixed f)
Multiplication assignment operator.
Definition: fixed.cpp:141
fixed
Implement a fixed-point number class. Values have places places after the decimal point...
Definition: fixed.hpp:37
operator*
fixed operator*(fixed a, fixed b)
Multiply fixed values.
Definition: fixed.cpp:268
fixed::as_string
std::string as_string() const
Definition: fixed.cpp:120
operator>=
bool operator>=(fixed a, fixed b)
Compare fixed values for greater-than-or-equal by comparing the underlying values.
Definition: fixed.cpp:305
operator<=
bool operator<=(fixed a, fixed b)
Compare fixed values for less-than-or-equal by comparing the underlying values.
Definition: fixed.cpp:300
operator<
bool operator<(fixed a, fixed b)
Compare fixed values for less-than by comparing the underlying values.
Definition: fixed.cpp:290
fixed::value
value_type value() const
Return the internal value.
Definition: fixed.hpp:107
fixed::integer
value_type integer() const
Return the integer part (which is the same as trunc()).
Definition: fixed.hpp:81
fixed::as_double
double as_double() const
Convert to double.
Definition: fixed.hpp:72
operator<<
std::ostream & operator<<(std::ostream &strm, fixed f)
Write a fixed value.
Definition: fixed.cpp:250
fixed::fraction
value_type fraction() const
Return the fractional part, e.g., 3 for 12.03.
Definition: fixed.cpp:31
operator!=
bool operator!=(fixed a, fixed b)
Compare fixed values for inequality by comparing the underlying values.
Definition: fixed.cpp:285
fixed::places
static int const places
number of decimal places
Definition: fixed.hpp:42
operator>
bool operator>(fixed a, fixed b)
Compare fixed values for greater-than by comparing the underlying values.
Definition: fixed.cpp:295
fixed::operator++
fixed & operator++()
Pre-increment.
Definition: fixed.cpp:158
fixed::operator+=
fixed & operator+=(fixed f)
Addition assignment operator.
Definition: fixed.cpp:129
fixed::as_float
float as_float() const
Convert to float.
Definition: fixed.hpp:74
fixed::operator/=
fixed & operator/=(fixed f)
Division assignment operator.
Definition: fixed.cpp:147
fixed::as_long_double
double as_long_double() const
Convert to long double.
Definition: fixed.hpp:70
fixed::operator--
fixed & operator--()
Pre-decrement.
Definition: fixed.cpp:171
operator/
fixed operator/(fixed a, fixed b)
Divide fixed values.
Definition: fixed.cpp:274
fixed::negate
void negate()
Negate this value.
Definition: fixed.cpp:153
fixed::operator-=
fixed & operator-=(fixed f)
Subtraction assignment operator.
Definition: fixed.cpp:135
operator>>
std::istream & operator>>(std::istream &strm, fixed &f)
Read a fixed value.
Definition: fixed.cpp:243
operator==
bool operator==(fixed a, fixed b)
Compare fixed values for equality by comparing the underlying values.
Definition: fixed.cpp:280
fixed::places10
static value_type const places10
10places
Definition: fixed.hpp:43
operator-
fixed operator-(fixed a, fixed b)
Subtract fixed values.
Definition: fixed.cpp:262
fixed::value_type
int value_type
Type of the actual value.
Definition: fixed.hpp:40
fixed::fixed
fixed(double value)
Construct by rounding off a floating point number.
Definition: fixed.hpp:57
operator+
fixed operator+(fixed a, fixed b)
Add fixed values.
Definition: fixed.cpp:256
fixed::round
value_type round() const
Definition: fixed.cpp:104
fixed::fixed
fixed()
Default constructor initializes to zero.
Definition: fixed.hpp:46
fixed::read
bool read(std::istream &strm)
Definition: fixed.cpp:190