-
Notifications
You must be signed in to change notification settings - Fork 3
/
FloatingBitset.hpp
168 lines (145 loc) · 4.89 KB
/
FloatingBitset.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef FLOATINGBITSET_HPP
#define FLOATINGBITSET_HPP
#include <ostream>
#include <bitset>
#include "SignedBitset.hpp"
#include "Exceptions.hpp"
namespace std {
/**
* \brief **FloatingBitset** : Encapsulate bitset to provide extended functionallity with signs and operators and floating point math.
*
* \tparam bit_width
* This template argument specifies the width of the in and output connections.
*/
template <size_t bit_width>
class FloatingBitset : public SignedBitset<bit_width> {
private:
/**
* \brief Extra bitset variable containing a fractionised floating point number.
*
* From left to right, each bit corresponds with the fraction: 1 / (current bit)
*
* MSB = 1 / 2, ..., LSB = 1 / (2 << bit_width)
*
* e.g.: 0.75 = 0.5 + 0.25 ==> (1100) in 4 bits
*/
std::bitset<bit_width> digits;
public:
/**
* Default constructor
*/
FloatingBitset() : std::bitset<bit_width>() {}
/**
* \brief Constructor with default value for number before comma.
*/
FloatingBitset(unsigned long long __val) : SignedBitset<bit_width>(__val), digits(0) {}
/**
* \brief Constructor with default value.
* Value before comma is the given double floored.
* Value after comma is calculated by subtracting fractions (1 / factor) from the remaining number.
*/
FloatingBitset(double __val) : SignedBitset<bit_width>((unsigned long long) __val) {
__val -= (unsigned long long) __val;
float q = 0.0;
for (int i = bit_width, factor = 2; --i >= 0; factor <<= 1) {
q = (1.0F / factor);
if (__val >= q) {
__val -= q;
digits.set(i);
}
}
}
/**
* Default destructor
*/
~FloatingBitset() {}
/** \brief Return the value of the bitset cast to float (with proper sign and digits behind comma).
*/
float to_float(void) const {
return (float) this->to_double();
}
/** \brief Return the value of the bitset cast to double (with proper sign and digits behind comma).
* Calculated by adding the fractions given in `this->digits`.
*/
double to_double(void) const {
double result = 0.0F;
for (int i = bit_width, factor = 2; --i >= 0; factor <<= 1) {
result += this->digits.test(i) ? 1.0F / factor : 0.0F;
}
return (double)this->to_llong() + result;
}
/** \brief Return the digits behind the comma of the FloatingBitset.
*/
const std::bitset<bit_width>& getDigits(void) const {
return this->digits;
}
/** \brief Operator* : Multiply given FloatingBitset with this one.
*
* \param __rhs
* The bitset to multiply with this bitset.
*
* \return FloatingBitset<bit_width>
* A new SignedBitset with the multiplication.
*/
FloatingBitset<bit_width> operator*(const FloatingBitset<bit_width>& __rhs) const {
return FloatingBitset<bit_width>(this->to_double() * __rhs.to_double());
}
/** \brief Operator*= : Multiply given FloatingBitset with this one.
*
* \param __rhs
* The bitset to multiply with this bitset.
*
* \return FloatingBitset<bit_width>
* This bitset.
*/
FloatingBitset<bit_width>& operator*=(const FloatingBitset<bit_width>& __rhs) {
return *this = *this * __rhs;
}
/** \brief Operator/ : Divide this FloatingBitset by the given one (floating point deivision).
*
* \param __rhs
* The bitset to divide this bitset by.
*
* \return FloatingBitset<bit_width>
* A new FloatingBitset with the division.
*
* \exception Exceptions::DivideByZeroException
* Throws exception if second bitset is zero (undefined behaviour).
*/
FloatingBitset<bit_width> operator/(const FloatingBitset<bit_width>& __rhs) const {
#ifdef THROW_EXCEPTIONS
if (__rhs.none())
throw Exceptions::DivideByZeroException("FloatingBitset");
#endif
return FloatingBitset<bit_width>(this->to_double() / __rhs.to_double());
}
/** \brief Operator/= : Divide this FloatingBitset by the given one (floating point deivision).
*
* \param __rhs
* The bitset to divide this bitset by.
*
* \return FloatingBitset<bit_width>
* This bitset.
*
* \exception Exceptions::DivideByZeroException
* Throws exception if second bitset is zero (undefined behaviour).
*/
FloatingBitset<bit_width>& operator/=(const FloatingBitset<bit_width>& __rhs) {
return *this = *this / __rhs;
}
/** \brief Add the floating point value to a given stream (`os << SignedBitset`).
*/
friend std::ostream& operator<<(std::ostream &os, FloatingBitset &s) {
os.precision(10);
os << s.to_double();
//os << s.to_string() << " ==> " << s.to_double();
return os;
}
/** \brief Add the floating point value to a given stream (`os << SignedBitset`).
*/
friend std::ostream& operator<<(std::ostream &os, FloatingBitset *s) {
return os << *s;
}
};
}
#endif // FLOATINGBITSET_HPP