-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdekker.hpp
206 lines (177 loc) · 5.45 KB
/
dekker.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// MIT License
//
// Copyright (c) 2024 Robin Lind
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef DEKKER_MATH_H
#define DEKKER_MATH_H
#include "vector.hpp"
#include <cmath>
namespace math {
struct dekker {
double h, l;
dekker() = default;
constexpr dekker(const double& x)
{
constexpr double SCALE = 134217729;
const double p = x * SCALE;
h = x - p + p;
l = x - h;
}
constexpr dekker(const double& h, const double& l) :
h(h), l(l) {}
double d() const
{
return h + l;
}
};
constexpr dekker dekker_addition(const dekker& a, const dekker& b)
{
const double T = a.h + b.h;
double t{};
if (std::abs(a.h) > std::abs(b.h))
t = a.h - T + b.h + a.l + b.l;
else
t = b.h - T + a.h + a.l + b.l;
const auto R = T + t;
return { R, T - R + t };
}
constexpr dekker dekker_subtraction(const dekker& a, const dekker& b)
{
const double T = a.h + b.h;
double t{};
if (std::abs(a.h) > std::abs(b.h))
t = a.h - T - b.h + a.l - b.l;
else
t = -b.h - T + a.h + a.l - b.l;
const auto R = T + t;
return { R, T - R + t };
}
constexpr dekker dekker_split(const double& x)
{
constexpr double SCALE = 134217729;
const double p = x * SCALE;
const double R = x - p + p;
return { R, x - R };
}
constexpr dekker dekker_mul12(const double& a, const double& b)
{
const auto A = dekker_split(a);
const auto B = dekker_split(b);
const auto p = A.h * B.h;
const auto q = A.h * B.l + A.l * B.h;
const auto R = p + q;
return { R, p - R + q + A.l * b };
}
constexpr dekker dekker_multiplication(const dekker& a, const dekker& b)
{
const auto T = dekker_mul12(a.h, b.h);
const auto C = a.h * b.l + a.l * b.h + T.l;
const auto R = T.h + C;
return { R, T.h - R + C };
}
constexpr dekker dekker_division(const dekker& a, const dekker& b)
{
const auto U = a.h / b.h;
const auto T = dekker_mul12(U, b.h);
const auto L = (a.h - T.h - T.l + a.l - U * b.l) / b.h;
const auto R = U + L;
return { R, U - R + L };
}
#define dekker_dekker_arithmetic_op(op, func) \
constexpr auto operator op(const dekker& lhs, const dekker& rhs) \
{ \
return func(lhs, rhs); \
}
#define dekker_scalar_arithmetic_op(op, func) \
template<typename T> \
constexpr auto operator op(const dekker& lhs, const T& rhs) \
{ \
return func(lhs, dekker_split(rhs)); \
}
#define scalar_dekker_arithmetic_op(op, func) \
template<typename T, size_t N> \
constexpr auto operator op(const T& lhs, const dekker& rhs) \
{ \
return func(dekker_split(lhs), rhs); \
}
#define dekker_arithmetic_op(op, func) \
dekker_dekker_arithmetic_op(op, func) \
dekker_scalar_arithmetic_op(op, func) \
scalar_dekker_arithmetic_op(op, func)
#define dekker_every_arithmetic_op(macro) \
macro(+, dekker_addition) \
macro(-, dekker_subtraction) \
macro(*, dekker_multiplication) \
macro(/, dekker_division)
#define dekker_dekker_assignment_op(op, opequals) \
template<typename T, size_t N> \
constexpr auto operator opequals(dekker& lhs, const dekker& rhs) \
{ \
lhs = lhs op rhs; \
}
#define dekker_t_assignment_op(op, opequals) \
template<typename T, size_t N> \
constexpr auto operator opequals(dekker& lhs, const T& rhs) \
{ \
lhs = lhs op rhs; \
}
#define dekker_assignment_op(op, opequals) \
dekker_dekker_assignment_op(op, opequals) \
dekker_t_assignment_op(op, opequals)
#define dekker_every_assignment_op(macro) \
macro(+, +=) \
macro(-, -=) \
macro(*, *=) \
macro(/, /=)
dekker_every_arithmetic_op(dekker_arithmetic_op)
dekker_every_assignment_op(dekker_assignment_op)
#undef dekker_dekker_arithmetic_op
#undef dekker_scalar_arithmetic_op
#undef scalar_dekker_arithmetic_op
#undef dekker_arithmetic_op
#undef dekker_every_arithmetic_op
#undef dekker_dekker_assignment_op
#undef dekker_t_assignment_op
#undef dekker_assignment_op
#undef dekker_every_assignment_op
template<typename T, size_t N>
constexpr auto operator-(const dekker& t)
{
return t * -1.0;
}
template<size_t N>
constexpr auto length(const vector<dekker, N>& a)
{
const auto result = math::dekker_split(sqrt(length_squared(a).d()));
return result;
}
using dekker2 = vector<dekker, 2>;
using dekker3 = vector<dekker, 3>;
using dekker4 = vector<dekker, 4>;
} // namespace math
constexpr auto operator""_dk(long double x)
{
return math::dekker(double(x));
}
constexpr auto operator""_dk(unsigned long long x)
{
return math::dekker(double(x));
}
#endif /* DEKKER_MATH_H */