Skip to content

Commit fb2cf0b

Browse files
committed
refining numeric_limits for quad-double
1 parent 4379a2b commit fb2cf0b

File tree

4 files changed

+28
-30
lines changed

4 files changed

+28
-30
lines changed

include/universal/common/number_traits_reports.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void numberTraits(std::ostream& ostr) {
2121
ostr << "min " << setw(ColumnWidth) << numeric_limits<Scalar>::min() << '\n';
2222
ostr << "max " << setw(ColumnWidth) << numeric_limits<Scalar>::max() << '\n';
2323
ostr << "lowest " << setw(ColumnWidth) << numeric_limits<Scalar>::lowest() << '\n';
24-
ostr << "epsilon (1+1ULP-1) " << setw(ColumnWidth) << numeric_limits<Scalar>::epsilon() << '\n';
24+
ostr << "epsilon ==ulp(1.0) " << setw(ColumnWidth) << numeric_limits<Scalar>::epsilon() << '\n';
2525
ostr << "round_error " << setw(ColumnWidth) << numeric_limits<Scalar>::round_error() << '\n';
2626
ostr << "smallest value " << setw(ColumnWidth) << numeric_limits<Scalar>::denorm_min() << '\n';
2727
ostr << "infinity " << setw(ColumnWidth) << numeric_limits<Scalar>::infinity() << '\n';

include/universal/number/qd/manipulators.hpp

+4-14
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,13 @@ namespace sw { namespace universal {
1919
}
2020

2121
// generate a binary, color-coded representation of the quad-double
22-
inline std::string color_print(qd const& r, bool nibbleMarker = false) {
23-
//constexpr unsigned es = 11;
24-
//constexpr unsigned fbits = 106;
22+
inline std::string color_print(const qd& r, bool nibbleMarker = false) {
2523
std::stringstream s;
26-
27-
/*
28-
Color red(ColorCode::FG_RED);
29-
Color yellow(ColorCode::FG_YELLOW);
30-
Color blue(ColorCode::FG_BLUE);
31-
Color magenta(ColorCode::FG_MAGENTA);
32-
Color cyan(ColorCode::FG_CYAN);
33-
Color white(ColorCode::FG_WHITE);
34-
Color def(ColorCode::FG_DEFAULT);
35-
*/
3624
for (int i = 0; i < 4; ++i) {
25+
std::string label = "x[" + std::to_string(i) + "]";
26+
s << std::setw(20) << label << " : ";
3727
s << color_print(r[i], nibbleMarker);
38-
if (i < 3) s << ", ";
28+
if (i < 3) s << '\n';
3929
}
4030
return s.str();
4131
}

include/universal/number/qd/numeric_limits.hpp

+5-11
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,32 @@ class numeric_limits< sw::universal::qd > {
1515
using QuadDouble = sw::universal::qd;
1616
static constexpr bool is_specialized = true;
1717
static constexpr QuadDouble min() { // return minimum value
18-
// return QuadDouble(sw::universal::SpecificValue::minpos);
1918
return QuadDouble(radix * (numeric_limits< double >::min() / numeric_limits< double >::epsilon()));
2019
}
2120
static constexpr QuadDouble max() { // return maximum value
22-
//return QuadDouble(sw::universal::SpecificValue::maxpos);
23-
return QuadDouble(numeric_limits< double >::max());
21+
return QuadDouble(sw::universal::SpecificValue::maxpos);
2422
}
2523
static constexpr QuadDouble lowest() { // return most negative value
26-
//return QuadDouble(sw::universal::SpecificValue::maxneg);
27-
return (-(max)());
24+
return QuadDouble(sw::universal::SpecificValue::maxneg);
2825
}
2926
static constexpr QuadDouble epsilon() { // return smallest effective increment from 1.0
30-
return numeric_limits< double >::epsilon() * numeric_limits< double >::epsilon() / radix;
27+
constexpr double epsilon{ std::numeric_limits< double >::epsilon() };
28+
return (((epsilon * epsilon) * epsilon) * epsilon) * 0.5; // / QuadDouble(radix);
3129
}
3230
static constexpr QuadDouble round_error() { // return largest rounding error
3331
return QuadDouble(1.0 / radix);
3432
}
3533
static constexpr QuadDouble denorm_min() { // return minimum denormalized value
36-
// return QuadDouble(sw::universal::SpecificValue::minpos);
37-
return 0.0;
34+
return QuadDouble(std::numeric_limits<double>::denorm_min());
3835
}
3936
static constexpr QuadDouble infinity() { // return positive infinity
4037
return QuadDouble(sw::universal::SpecificValue::infpos);
41-
//return numeric_limits< double >::infinity();
4238
}
4339
static constexpr QuadDouble quiet_NaN() { // return non-signaling NaN
4440
return QuadDouble(sw::universal::SpecificValue::qnan);
45-
//return numeric_limits< double >::quiet_NaN();
4641
}
4742
static constexpr QuadDouble signaling_NaN() { // return signaling NaN
4843
return QuadDouble(sw::universal::SpecificValue::snan);
49-
//return numeric_limits< double >::signaling_NaN();
5044
}
5145

5246
static constexpr int digits = 2 * std::numeric_limits<double>::digits;

include/universal/number/qd/qd_impl.hpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ qd operator+(const qd&, const qd&);
3434
qd operator-(const qd&, const qd&);
3535
qd operator*(const qd&, const qd&);
3636
qd operator/(const qd&, const qd&);
37+
std::ostream& operator<<(std::ostream&, const qd&);
3738
qd pown(const qd&, int);
39+
qd frexp(const qd&, int*);
3840

3941
// qd is an unevaluated quadruple of IEEE-754 doubles that provides a (1,11,212) floating-point triple
4042
class qd {
@@ -300,14 +302,15 @@ class qd {
300302

301303
// create specific number system values of interest
302304
constexpr qd& maxpos() noexcept {
303-
x[0] = 0.0;
305+
x[0] = std::numeric_limits<double>::max();
304306
x[1] = 0.0;
305307
x[2] = 0.0;
306308
x[3] = 0.0;
307309
return *this;
308310
}
311+
// smallest positive normal number
309312
constexpr qd& minpos() noexcept {
310-
x[0] = 0.0;
313+
x[0] = std::numeric_limits<double>::min();
311314
x[1] = 0.0;
312315
x[2] = 0.0;
313316
x[3] = 0.0;
@@ -320,15 +323,16 @@ class qd {
320323
x[3] = 0.0;
321324
return *this;
322325
}
326+
// smallest negative normal number
323327
constexpr qd& minneg() noexcept {
324-
x[0] = 0.0;
328+
x[0] = -std::numeric_limits<double>::min();
325329
x[1] = 0.0;
326330
x[2] = 0.0;
327331
x[3] = 0.0;
328332
return *this;
329333
}
330334
constexpr qd& maxneg() noexcept {
331-
x[0] = 0.0;
335+
x[0] = std::numeric_limits<double>::lowest();
332336
x[1] = 0.0;
333337
x[2] = 0.0;
334338
x[3] = 0.0;
@@ -1105,6 +1109,16 @@ inline std::string to_quad(const qd& v, int precision = 17) {
11051109
return s.str();
11061110
}
11071111

1112+
inline std::string to_triple(const qd& v, int precision = 17) {
1113+
std::stringstream s;
1114+
bool isneg = v.isneg();
1115+
int scale = v.scale();
1116+
int exponent;
1117+
qd fraction = frexp(v, &exponent);
1118+
s << '(' << (isneg ? '1' : '0') << ", " << scale << ", " << std::setprecision(precision) << fraction << ')';
1119+
return s.str();
1120+
}
1121+
11081122
inline std::string to_binary(const qd& number, bool bNibbleMarker = false) {
11091123
std::stringstream s;
11101124
constexpr int nrLimbs = 4;

0 commit comments

Comments
 (0)