@@ -41,6 +41,48 @@ namespace sw {
41
41
std::cout << to_pair (v) << ' \n ' ;
42
42
}
43
43
44
+ // specialize ReportValue for double-double (dd)
45
+ void ReportValue (const dd& a, const std::string& label = " " , unsigned labelWidth = 20 , unsigned precision = 32 ) {
46
+ auto defaultPrecision = std::cout.precision ();
47
+ std::cout << std::setprecision (precision);
48
+ std::cout << std::setw (labelWidth) << label << " : " << a << ' \n ' ;
49
+ std::cout << std::setprecision (defaultPrecision);
50
+ }
51
+
52
+
53
+ void SettingBits () {
54
+ std::cout << " +---------- Setting float bits ---------+\n " ;
55
+ {
56
+ float v{ 0 .0f };
57
+ setbit (v, 31 );
58
+ ReportValue (v);
59
+ setbit (v, 23 ); // set min normal
60
+ ReportValue (v);
61
+ setbit (v, 23 , false ); setbit (v, 0 ); // set smallest denorm
62
+ ReportValue (v);
63
+ }
64
+ std::cout << " +---------- Setting double bits ---------+\n " ;
65
+ {
66
+ double v{ 0.0 };
67
+ setbit (v, 63 );
68
+ ReportValue (v);
69
+ setbit (v, 52 ); // set min normal
70
+ ReportValue (v);
71
+ setbit (v, 52 , false ); setbit (v, 0 ); // set smallest denorm
72
+ ReportValue (v);
73
+ }
74
+ std::cout << " +---------- Setting double-double bits ---------+\n " ;
75
+ {
76
+ dd v{ 0.0 };
77
+ v.setbit (127 );
78
+ ReportValue (v);
79
+ v.setbit (116 ); // set min normal
80
+ ReportValue (v);
81
+ v.setbit (116 , false ); v.setbit (64 ); // set smallest denorm
82
+ ReportValue (v);
83
+ }
84
+ }
85
+
44
86
void adjust (dd const & a) {
45
87
dd r = abs (a);
46
88
dd ten (10.0 );
@@ -89,7 +131,35 @@ try {
89
131
90
132
auto defaultPrecision = std::cout.precision ();
91
133
92
- std::cout << " + ---------- - unevaluated pairs------------ +\n " ;
134
+ std::cout << " +---------- ULP assessments ---------+\n " ;
135
+ {
136
+ double zero{ 0.0 };
137
+ double next = std::nextafter (zero, +INFINITY);
138
+ ReportValue (next, " nextafter 0.0" );
139
+ double one{ 1.0 };
140
+ next = std::nextafter (one, +INFINITY);
141
+ ReportValue (next, " nextafter 1.0" );
142
+
143
+
144
+ // ULP at 1.0 is 2^-106
145
+ double ulpAtOne = std::pow (2.0 , -106 );
146
+
147
+ dd a{ 1.0 };
148
+ a += ulpAtOne;
149
+ ReportValue (a, " 1.0 + eps" );
150
+
151
+ a = 1.0 ;
152
+ dd ddUlpAtOne = ulp (a);
153
+ ReportValue (ddUlpAtOne, " ulp(1.0)" );
154
+ a += ulp (a);
155
+ ReportValue (a, " 1.0 + ulp(1.0)" );
156
+
157
+ dd eps = std::numeric_limits<dd>::epsilon ();
158
+ ReportValue (eps, " epsilon" );
159
+
160
+ }
161
+
162
+ std::cout << " +---------- unevaluated pairs ------------ +\n " ;
93
163
{
94
164
// what is the value that adds a delta one below the least significant fraction bit of the high double?
95
165
// dd = high + lo
@@ -113,51 +183,19 @@ try {
113
183
std::cout << std::setprecision (defaultPrecision);
114
184
}
115
185
116
- return 0 ;
117
-
118
- std::cout << " Smallest normal number progressions\n " ;
186
+ std::cout << " +---------- Smallest normal number progressions ---------+\n " ;
119
187
{
120
188
constexpr double smallestNormal = std::numeric_limits<double >::min ();
121
189
dd a (smallestNormal);
122
190
for (int i = 0 ; i < 10 ; ++i) {
123
191
ReportValue (a);
124
192
a *= 2.0 ;
125
193
}
126
-
127
194
}
128
195
129
- std::cout << " Setting float bits\n " ;
130
- {
131
- float v{0 .0f };
132
- setbit (v,31 );
133
- ReportValue (v);
134
- setbit (v,23 ); // set min normal
135
- ReportValue (v);
136
- setbit (v,23 ,false ); setbit (v,0 ); // set smallest denorm
137
- ReportValue (v);
138
- }
139
- std::cout << " Setting double bits\n " ;
140
- {
141
- double v{0.0 };
142
- setbit (v,63 );
143
- ReportValue (v);
144
- setbit (v,52 ); // set min normal
145
- ReportValue (v);
146
- setbit (v,52 ,false ); setbit (v,0 ); // set smallest denorm
147
- ReportValue (v);
148
- }
149
- std::cout << " Setting double-double bits\n " ;
150
- {
151
- dd v{0.0 };
152
- v.setbit (127 );
153
- ReportValue (v);
154
- v.setbit (116 ); // set min normal
155
- ReportValue (v);
156
- v.setbit (116 ,false ); v.setbit (64 ); // set smallest denorm
157
- ReportValue (v);
158
- }
159
196
160
- std::cout << " subnormal exponent adjustment\n " ;
197
+
198
+ std::cout << " +---------- subnormal exponent adjustment ---------+\n " ;
161
199
{
162
200
constexpr double smallestNormal = std::numeric_limits<double >::min ();
163
201
dd a{ smallestNormal };
0 commit comments