@@ -75,6 +75,16 @@ namespace sw {
75
75
return ostr << v.v ;
76
76
}
77
77
78
+
79
+ std::string centered (const std::string& label, unsigned columnWidth) {
80
+ unsigned length = static_cast <unsigned >(label.length ());
81
+ if (columnWidth < length) return label;
82
+
83
+ unsigned padding = columnWidth - length;
84
+ unsigned leftPadding = (padding >> 1 );
85
+ unsigned rightPadding = padding - leftPadding;
86
+ return std::string (leftPadding, ' ' ) + label + std::string (rightPadding, ' ' );
87
+ }
78
88
}
79
89
}
80
90
@@ -96,13 +106,75 @@ try {
96
106
// dd = high + lo
97
107
// = 1*2^0 + 1*2^-53
98
108
// = 1.0e00 + 1.0elog10(2^-53)
99
- ReportValue (std::pow (2.0 , 0.0 ), " 2^0" );
100
- ReportValue (std::pow (2.0 , -53.0 ), " 2^-53" );
101
- std::cout << std::log10 (std::pow (2.0 , -53.0 )) << ' \n ' ;
102
- double exponent = std::ceil (std::log10 (std::pow (2.0 , -53.0 )));
109
+ double high{ std::pow (2.0 , 0.0 ) };
110
+ ReportValue (high, " 2^0" );
111
+ double low{ std::pow (2.0 , -53.0 ) };
112
+ ReportValue (low, " 2^-53" );
113
+ std::cout << std::log10 (low) << ' \n ' ;
114
+ double exponent = -std::ceil (std::abs (std::log10 (low)));
103
115
std::cout << " exponent : " << exponent << ' \n ' ;
116
+
117
+ // now let's walk that bit down to the ULP
118
+ double x0{ 1.0 };
119
+ double x1{ 0.0 };
120
+ double x2{ 0.0 };
121
+ double x3{ 0.0 };
122
+ int precisionForRange = 16 ;
123
+ std::cout << std::setprecision (precisionForRange);
124
+ x0 = 1.0 ;
125
+ qd a (x0, x1, x2, x3);
126
+ std::cout << centered (" quad-double" , precisionForRange + 6 ) << " : " ;
127
+ std::cout << centered (" binary form of x0" , 68 ) << " : " ;
128
+ std::cout << centered (" real value of x0" , 15 ) << ' \n ' ;
129
+ std::cout << a << " : " << to_binary (x0) << " : " << x0 << ' \n ' ;
130
+ for (int i = 1 ; i < 53 ; ++i) {
131
+ x0 = 1.0 + (std::pow (2.0 , - double (i)));
132
+ qd a (x0, x1, x2, x3);
133
+ std::cout << a << " : " << to_binary (x0) << " : " << std::setprecision (7 ) << x0 << std::setprecision (precisionForRange) << ' \n ' ;
134
+ }
135
+ // x0 is 1.0 + eps() at this point
136
+ // std::cout << to_binary(x0) << '\n';
137
+ std::cout << to_binary (qd (x0, x1, x2, x3)) << ' \n ' ;
138
+ x0 = 1.0 ;
139
+ precisionForRange = 32 ;
140
+ std::cout << std::setprecision (precisionForRange);
141
+ std::cout << centered (" quad-double" , precisionForRange + 6 ) << " : " ;
142
+ std::cout << centered (" binary form of x1" , 68 ) << " : " ;
143
+ std::cout << centered (" real value of x1" , 15 ) << ' \n ' ;
144
+ for (int i = 0 ; i < 54 ; ++i) {
145
+ x1 = (std::pow (2.0 , -53.0 - double (i)));
146
+ qd a (x0, x1, x2, x3);
147
+ std::cout << a << " : " << to_binary (x1) << " : " << std::setprecision (7 ) << x1 << std::setprecision (precisionForRange) << ' \n ' ;
148
+ }
149
+ std::cout << to_binary (qd (x0, x1, x2, x3)) << ' \n ' ;
150
+ x1 = 0.0 ;
151
+ precisionForRange = 48 ;
152
+ std::cout << std::setprecision (precisionForRange);
153
+ std::cout << centered (" quad-double" , precisionForRange + 6 ) << " : " ;
154
+ std::cout << centered (" binary form of x2" , 68 ) << " : " ;
155
+ std::cout << centered (" real value of x2" , 15 ) << ' \n ' ;
156
+ for (int i = 0 ; i < 54 ; ++i) {
157
+ x2 = (std::pow (2.0 , -106.0 - double (i)));
158
+ qd a (x0, x1, x2, x3);
159
+ std::cout << a << " : " << to_binary (x2) << " : " << std::setprecision (7 ) << x2 << std::setprecision (precisionForRange) << ' \n ' ;
160
+ }
161
+ std::cout << to_binary (qd (x0, x1, x2, x3)) << ' \n ' ;
162
+ x2 = 0.0 ;
163
+ precisionForRange = 64 ;
164
+ std::cout << std::setprecision (precisionForRange);
165
+ std::cout << centered (" quad-double" , precisionForRange + 6 ) << " : " ;
166
+ std::cout << centered (" binary form of x3" , 68 ) << " : " ;
167
+ std::cout << centered (" real value of x3" , 15 ) << ' \n ' ;
168
+ for (int i = 0 ; i < 54 ; ++i) {
169
+ x3 = (std::pow (2.0 , -159.0 - double (i)));
170
+ qd a (x0, x1, x2, x3);
171
+ std::cout << a << " : " << to_binary (x3) << " : " << std::setprecision (7 ) << x3 << std::setprecision (precisionForRange) << ' \n ' ;
172
+ }
173
+ std::cout << to_binary (qd (x0, x1, x2, x3)) << ' \n ' ;
174
+ std::cout << std::setprecision (defaultPrecision);
104
175
}
105
176
177
+ return 0 ;
106
178
{
107
179
// what is the difference between ostream fmt scientific/fixed
108
180
0 commit comments