-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRealEstimate.cpp
311 lines (250 loc) · 8.15 KB
/
RealEstimate.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
RealLib, a library for efficient exact real computation
Copyright (C) 2006 Branimir Lambov
This library is licensed under the Apache License, Version 2.0 (the "License");
you may not use this library except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "defs.h"
#include <assert.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include "RealEstimate.h"
#include "DataManager.h"
#include "LongFloat.h"
#include "ErrorEstimate.h"
namespace RealLib {
RealLibException::RealLibException(const char *wht) throw()
{
if (wht) {
strncpy(m_what, wht, 127);
m_what[127] = 0;
} else m_what[0] = 0;
}
// Estimate implementation
Estimate::Estimate(const LongFloat &val, const ErrorEstimate &err)
: m_Value(val), m_Error(err)
{
CorrectZero();
}
Estimate::Estimate(double v)
: m_Value(v), m_Error(ErrorEstimate())
{
CorrectZero();
}
Estimate::Estimate(const char *val)
: m_Value(val), m_Error(RoundingError(m_Value, m_Value.DivisionRoundingError()))
{
CorrectZero();
}
void Estimate::CorrectZero()
{
if (m_Value.Kind() == LongFloat::Zero)
if (m_Error.m_Exp == ErrorEstimate::minusinf)
{
m_Error.m_Exp = MINIMUM_EXPONENT+1;
m_Error.m_Man = 1u<<30;
m_Value = m_Error.AsLongFloat();
m_Error.m_Man = 1u<<31;
}
else
{
m_Value = m_Error.AsLongFloat();
m_Error = m_Error << 1;
}
}
Estimate Estimate::GetError() const
{
return Estimate(m_Error.AsLongFloat());
}
Estimate& Estimate::SetError(const Estimate &err)
{
ErrorEstimate ee(err.m_Value);
m_Error = ee + err.m_Error;
return *this;
}
Estimate& Estimate::AddError(const Estimate &err)
{
m_Error = m_Error + ErrorEstimate(err.m_Value) + err.m_Error;
return *this;
}
i32 Estimate::GetRelativeError() const
{
exp_type ee = m_Error.m_Exp;
exp_type ev = weak_normalize();
return i32_saturated(ev - ee - 1);
}
Estimate Estimate::TruncateNegative(const char *origin) const
{
if (IsNegative()) throw DomainException(origin);
if (IsPositive()) return *this;
// (the theory says
// we can't always give the correct DomainException, so we shouldn't try)
// Get an interval centered at half the upper bound, with the same error.
LongFloat center((m_Value + m_Error.AsLongFloat()) >> 1);
Estimate a(center, ErrorEstimate(center));
assert(!a.IsPositive());
return a;
//return a.SetError(a);
}
// operations
Estimate operator - (const Estimate &arg)
{
return Estimate(-arg.m_Value, arg.m_Error);
}
Estimate recip(const Estimate &arg)
{
if (!arg.IsNonZero()) throw PrecisionException("recip");
LongFloat r(arg.m_Value.recip());
ErrorEstimate e(arg.m_Value, ErrorEstimate::Down);
ErrorEstimate re(RoundingError(r, r.DivisionRoundingError()));
return Estimate(r, (arg.m_Error / (e - arg.m_Error) / e) + re);
// multiplication in denominator would have the wrong rounding mode
}
Estimate operator + (const Estimate &lhs, const Estimate &rhs)
{
LongFloat s(lhs.m_Value + rhs.m_Value);
return Estimate(s, lhs.m_Error + rhs.m_Error + RoundingError(s, s.AdditionRoundingError()));
}
Estimate operator - (const Estimate &lhs, const Estimate &rhs)
{
LongFloat s(lhs.m_Value - rhs.m_Value);
return Estimate(s, lhs.m_Error + rhs.m_Error + RoundingError(s, s.AdditionRoundingError()));
}
Estimate operator * (const Estimate &lhs, const Estimate &rhs)
{
LongFloat r(lhs.m_Value * rhs.m_Value);
ErrorEstimate e(lhs.m_Error * rhs.m_Error + lhs.m_Error * ErrorEstimate(rhs.m_Value) + rhs.m_Error * ErrorEstimate(lhs.m_Value));
return Estimate(r, e + RoundingError(r, r.MultiplicationRoundingError()));
}
Estimate operator / (const Estimate &lhs, const Estimate &rhs)
{
if (!rhs.IsNonZero()) throw PrecisionException("division");
// this also assures e - rhs.m_Error > 0
LongFloat r(lhs.m_Value / rhs.m_Value);
ErrorEstimate e(rhs.m_Value, ErrorEstimate::Down);
ErrorEstimate n(ErrorEstimate(lhs.m_Value) * rhs.m_Error + ErrorEstimate(rhs.m_Value, ErrorEstimate::Up) * lhs.m_Error);
return Estimate(r, n / (e - rhs.m_Error) / e + RoundingError(r, r.DivisionRoundingError()));
// multiplication in denominator would have the wrong rounding mode
}
Estimate Estimate::operator << (i32 howmuch) const
{
LongFloat v(m_Value << howmuch);
return Estimate(v, (m_Error << howmuch) + RoundingError(v, v.AdditionRoundingError()));
}
Estimate operator * (const Estimate &lhs, i32 rhs)
{
LongFloat r(lhs.m_Value * rhs);
return Estimate(r, lhs.m_Error * ErrorEstimate(double(rhs)) + RoundingError(r, r.AdditionRoundingError()));
}
Estimate operator / (const Estimate &lhs, i32 rhs)
{
if (rhs == 0) throw DomainException("division by int");
LongFloat r(lhs.m_Value / rhs);
return Estimate(r, lhs.m_Error / ErrorEstimate(double(rhs)) + RoundingError(r, r.AdditionRoundingError()));
}
bool Estimate::IsPositive() const
{
return !m_Value.IsNegative() && ErrorEstimate(m_Value) > m_Error;
}
bool Estimate::IsNegative() const
{
return m_Value.IsNegative() && ErrorEstimate(m_Value) > m_Error;
}
bool Estimate::IsNonZero() const
{
return ErrorEstimate(m_Value) > m_Error;
}
bool Estimate::weak_IsNegative() const
{
return m_Value.Kind() == LongFloat::Normal && m_Value.IsNegative();
}
bool Estimate::weak_IsPositive() const
{
return m_Value.Kind() == LongFloat::Normal && !m_Value.IsNegative();
}
bool Estimate::weak_lt(const Estimate& rhs) const
{
return m_Value < rhs.m_Value;
}
bool Estimate::weak_eq(const Estimate& rhs) const
{
return m_Value == rhs.m_Value;
}
Estimate Estimate::weak_round() const
{
return Estimate(m_Value.round());
}
/*
char* Estimate::AsDecimal(char *bufptr, u32 buflen)
{
int prec = GetPrecision();
// must at least accomodate exponent
assert (buflen >= 10);
char *buffer = bufptr;
// the format chosen is "[-].<mantissa>e<+/-><exponent>"
// where mantissa has a leading non-zero decimal
if (m_Value.IsNegative()) {
buffer++[0] = '-';
--buflen;
}
// handle special values
switch (m_Value.Kind()) {
case LongFloat::Nan:
strcpy(buffer, "NaN");
return bufptr;
case LongFloat::Infinity:
strcpy(buffer, "Infinity");
return bufptr;
case LongFloat::Zero:
strcpy(buffer, "Zero");
return bufptr;
}
if (m_Error.gePow2(0)) {
strcpy(buffer, "Zero");
return bufptr;
}
// calculate exponent: the least power of 10 that is greater than
// or equal to the value. Using decimal logarithm and its ceiling.
Estimate pwr = ln(abs(*this)) / Constants.ln10.GetEstimate(prec);
// the following operation is safe: the exponent is at most
// 2^(32 * log(10, 2)) which fits in less than the 53 mantissa bits
// of double
int e = int(ceil(pwr.Value().AsDouble()));
// GCC doesn't understand _itoa, use sprintf instead
sprintf(buffer, "%+d", e);
// now we know the length of the exponent. move it to the end of
// the string
int explen = strlen(buffer);
strcpy(buffer + buflen - explen - 1, buffer);
buffer[0] = '.';
// divide the value by the exponent to form decimal mantissa
Estimate div(pow(LongFloat(10), e));
Estimate man = *this / div;
// when the value is power of ten, we can get two possible
// representations of the mantissa: 0.(9) and 1.(0). This
// behavior is not an error as it is consistent with the
// theory. The function used to convert the mantissa will
// not display 1.(0) correctly. Thus we must handle the
// case differently: just pretend it's 0.(9)
if (man.Value().Exponent() > 0) {
for (u32 i=1;i<buflen - explen - 2; ++i)
buffer[i] = '9';
} else
// convert it to decimal using LongFloat's function
man.Value().MantissaAsDecimal(buffer+1, buflen - explen - 2);
char *ptr = buffer + (buflen - explen - 2);
*ptr = 'e';
return bufptr;
}
*/
} // namespace