-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPTypes.cpp
More file actions
177 lines (148 loc) · 5.87 KB
/
FPTypes.cpp
File metadata and controls
177 lines (148 loc) · 5.87 KB
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
169
170
171
172
173
174
175
176
177
/*
Include <numbers> or simulate it if it is not available
*/
#include <climits>
#if __has_include(<numbers>)
#include <numbers>
#else
#include <type_traits>
#include <limits>
template<class T> concept FloatingPoint = std::is_floating_point_v<T>;
namespace std {
namespace math {
template<typename> inline constexpr bool __always_false = false;
template<typename T> inline constexpr T __invalidTemplate() {
static_assert(__always_false<T>, "This template needs to be specialized."); return T();
}
template <typename T> inline constexpr T pi_v = __invalidTemplate<T>();
template <FloatingPoint T> inline constexpr T pi_v<T> = 3.141592653589793238462643383279502884L;
template double pi_v<double>;
inline constexpr double pi = pi_v<double>;
}
}
#endif
/*
High precision FP type generalizing the internal representation of IEEE754 binary floating point numbers.
*/
template <typename N, typename D, typename E, typename F> class floating_t
{
N m_numerator;
D m_denominator;
E m_exponent;
static_assert(std::numeric_limits<N>::is_integer);
static_assert(std::numeric_limits<D>::is_integer);
static_assert(std::numeric_limits<E>::is_integer);
static_assert(!std::numeric_limits<F>::is_integer);
static_assert(std::numeric_limits<N>::is_signed);
static_assert(!std::numeric_limits<D>::is_signed);
static_assert(std::numeric_limits<E>::is_signed);
static_assert(std::numeric_limits<N>::digits ==
std::numeric_limits<D>::digits - 1);
static constexpr unsigned exponent_length = CHAR_BIT * sizeof(F) -
std::numeric_limits<F>::digits;
static constexpr unsigned mantissa_length = std::numeric_limits<F>::digits - 1;
static_assert(CHAR_BIT * sizeof(D) > mantissa_length);
static_assert(std::numeric_limits<E>::digits >= exponent_length);
static_assert(std::numeric_limits<N>::digits >
std::numeric_limits<F>::digits);
constexpr F getFloatingPointValue() const
{
F val = static_cast<F>(m_numerator) / static_cast<F>(m_denominator);
for (E i = std::numeric_limits<F>::max_exponent; i <= m_exponent; i++)
val *= 2;
return val;
}
public:
constexpr floating_t(const N & num, const D & den, const E & e) :
m_numerator(num), m_denominator(den), m_exponent(e) {}
constexpr floating_t(F value) : m_numerator(0), m_denominator(1),
m_exponent(std::numeric_limits<F>::max_exponent - 1)
{
m_denominator <<= mantissa_length;
bool isNegative = false;
if (value < 0)
{
isNegative = true;
value = -value;
}
if (value < 1)
do
m_exponent--;
while ((value = 2 * value) < 1);
else if (value > 2)
do
m_exponent++;
while ((value = value / 2) > 2);
m_numerator = static_cast<N>(value * static_cast<F>(m_denominator));
if (isNegative)
m_numerator = -m_numerator;
}
constexpr operator F() const
{
return getFloatingPointValue();
}
constexpr bool validate(F value) const
{//validate that the supposed value of the class is indeed equal to value
return value == getFloatingPointValue();
}
/*
Many other lines of code
*/
};
using myfptype = floating_t<signed long long, unsigned long long, short, double>;
constexpr myfptype dp_pi (std::math::pi);
static_assert(dp_pi.validate(std::math::pi));
static_assert(std::math::pi == (double)3.141'592'653'589'793'3L);
static_assert(std::math::pi == (double)3.141'592'653'589'793'0L);
template<> inline constexpr myfptype std::math::pi_v<myfptype>
{3'141'592'653'589'793'238L, 1'000'000'000'000'000'000L, 1};
//The internal precision of hp_pi is 19 decimal digits.
constexpr myfptype hp_pi = std::math::pi_v<myfptype>;
constexpr myfptype almost_pi{ 3'141'592'653'589'793'237L, 1'000'000'000'000'000'000L, 1 };
static_assert(hp_pi.validate(std::math::pi));
static_assert(almost_pi.validate(std::math::pi));
/*
Bignum FP type templated by its precision
*/
template <int PRECISION> class BigNumFPType
{
public:
constexpr BigNumFPType (double value) :m_state(value) {};
constexpr static BigNumFPType getPI() { return BigNumFPType(std::math::pi); }
constexpr static BigNumFPType Pi = getPI();
constexpr double roundToDouble() const { return m_state; }
private:
typedef double InternalState; //could and should be much more sophisticated
InternalState m_state;
};
//Explicit specialization of std::math::pi_v for BigNumFPType
template<int PRECISION> inline constexpr BigNumFPType<PRECISION> std::math::pi_v<BigNumFPType<PRECISION>> = BigNumFPType<PRECISION>::getPI();
constexpr auto v = std::math::pi_v<BigNumFPType<100>>;
static_assert(v.roundToDouble() == std::math::pi);
/*
Bignum FP type with dynamic precision
*/
class DynamicBigNumFPType
{
public:
constexpr DynamicBigNumFPType (double value, int precision) :m_state(value), m_precision(precision) {};
void setPrecision(int precision) { m_precision = precision; }
//notice that getPI() isn't constexpr or static, because pi is no longer deemed as a constant but rather as an algorithm running until a certain precision is achieved
//the real implementation would calculate pi based upon m_precision and construct DynamicBigNumFPType from it
DynamicBigNumFPType getPI() { return DynamicBigNumFPType(std::math::pi, m_precision); }
double roundToDouble() const { return m_state; }
private:
typedef double InternalState; //could and should be much more sophisticated
InternalState m_state;
int m_precision;
};
//Explicit specialization of std::math::pi_v for BigNumFPType
template<> inline DynamicBigNumFPType std::math::pi_v<DynamicBigNumFPType> = DynamicBigNumFPType(std::math::pi, 0).getPI();
int main() {
DynamicBigNumFPType pi{ std::math::pi_v<DynamicBigNumFPType> };
for (int i = 0; i < 100; i++)
{
pi.setPrecision(i);
//use pi in calculations, break if the right result is achieved
}
}