Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(868)

Side by Side Diff: runtime/vm/bigint_operations_test.cc

Issue 9540015: Implement correct bigint to double conversion. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/bigint_operations.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/assert.h" 5 #include "platform/assert.h"
6 #include "vm/bigint_operations.h" 6 #include "vm/bigint_operations.h"
7 #include "vm/object.h" 7 #include "vm/object.h"
8 #include "vm/object_store.h" 8 #include "vm/object_store.h"
9 #include "vm/unit_test.h" 9 #include "vm/unit_test.h"
10 10
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 bigint = BigintOperations::NewFromSmi(smi); 132 bigint = BigintOperations::NewFromSmi(smi);
133 dbl = BigintOperations::ToDouble(bigint); 133 dbl = BigintOperations::ToDouble(bigint);
134 EXPECT_EQ(0.0, dbl.value()); 134 EXPECT_EQ(0.0, dbl.value());
135 double zero = dbl.value(); 135 double zero = dbl.value();
136 136
137 smi = Smi::New(-12345678); 137 smi = Smi::New(-12345678);
138 bigint = BigintOperations::NewFromSmi(smi); 138 bigint = BigintOperations::NewFromSmi(smi);
139 dbl = BigintOperations::ToDouble(bigint); 139 dbl = BigintOperations::ToDouble(bigint);
140 EXPECT_EQ(-1.2345678e+7, dbl.value()); 140 EXPECT_EQ(-1.2345678e+7, dbl.value());
141 141
142 bigint = BigintOperations::NewFromCString("1");
143 dbl = BigintOperations::ToDouble(bigint);
144 EXPECT_EQ(1.0, dbl.value());
145
146 bigint = BigintOperations::NewFromCString("123456");
147 dbl = BigintOperations::ToDouble(bigint);
148 EXPECT_EQ(123456.0, dbl.value());
149
150 bigint = BigintOperations::NewFromCString("123456789");
151 dbl = BigintOperations::ToDouble(bigint);
152 EXPECT_EQ(123456789.0, dbl.value());
153
154 bigint = BigintOperations::NewFromCString("12345678901234567");
155 dbl = BigintOperations::ToDouble(bigint);
156 EXPECT_EQ(12345678901234567.0, dbl.value());
157 EXPECT_EQ(12345678901234568.0, dbl.value());
158
142 bigint = BigintOperations::NewFromCString("98765432109876"); 159 bigint = BigintOperations::NewFromCString("98765432109876");
143 dbl = BigintOperations::ToDouble(bigint); 160 dbl = BigintOperations::ToDouble(bigint);
144 EXPECT_EQ(9.8765432109876e+13, dbl.value()); 161 EXPECT_EQ(9.8765432109876e+13, dbl.value());
145 162
163 bigint = BigintOperations::NewFromCString("0x17777777777778");
164 dbl = BigintOperations::ToDouble(bigint);
165 EXPECT_EQ(6605279453476728.0, dbl.value());
166
167 bigint = BigintOperations::NewFromCString("0x37777777777778");
168 dbl = BigintOperations::ToDouble(bigint);
169 EXPECT_EQ(15612478708217720.0, dbl.value());
170
171 bigint = BigintOperations::NewFromCString("0x177777777777781234567");
172 dbl = BigintOperations::ToDouble(bigint);
173 EXPECT_EQ(1.7730912021014563e+24, dbl.value());
174
175 bigint = BigintOperations::NewFromCString("0x177777777777788000000");
176 dbl = BigintOperations::ToDouble(bigint);
177 EXPECT_EQ(1.7730912021014563e+24, dbl.value());
178
179 bigint = BigintOperations::NewFromCString("0x177777777777788000001");
180 dbl = BigintOperations::ToDouble(bigint);
181 EXPECT_EQ(1.7730912021014565e+24, dbl.value());
182
183 bigint = BigintOperations::NewFromCString("0x177777777777798000000");
184 dbl = BigintOperations::ToDouble(bigint);
185 EXPECT_EQ(1.7730912021014568e+24, dbl.value());
186
187 bigint = BigintOperations::NewFromCString("0x177777777777798000001");
188 dbl = BigintOperations::ToDouble(bigint);
189 EXPECT_EQ(1.7730912021014568e+24, dbl.value());
190
191 bigint = BigintOperations::NewFromCString("0x377777777777790000000");
192 dbl = BigintOperations::ToDouble(bigint);
193 EXPECT_EQ(4.1909428413307146e+24, dbl.value());
194
195 bigint = BigintOperations::NewFromCString("0x377777777777790000001");
196 dbl = BigintOperations::ToDouble(bigint);
197 EXPECT_EQ(4.190942841330715e+24, dbl.value());
198
199 bigint = BigintOperations::NewFromCString("0x377777777777730000000");
200 dbl = BigintOperations::ToDouble(bigint);
201 EXPECT_EQ(4.1909428413307135e+24, dbl.value());
202
203 bigint = BigintOperations::NewFromCString("0x377777777777730000001");
204 dbl = BigintOperations::ToDouble(bigint);
205 EXPECT_EQ(4.1909428413307135e+24, dbl.value());
206
146 // Reduced precision. 207 // Reduced precision.
147 bigint = BigintOperations::NewFromCString( 208 bigint = BigintOperations::NewFromCString(
148 "9876543210987654321098765432109876543210"); 209 "9876543210987654321098765432109876543210");
149 dbl = BigintOperations::ToDouble(bigint); 210 dbl = BigintOperations::ToDouble(bigint);
150 // TODO(floitsch): Proper rounding if deemed necessary.
151 EXPECT_EQ(9.8765432109876546e+39, dbl.value()); 211 EXPECT_EQ(9.8765432109876546e+39, dbl.value());
152 212
153 bigint = BigintOperations::NewFromCString( 213 bigint = BigintOperations::NewFromCString(
154 "12345678901234567890123456789012345678901234567890" 214 "12345678901234567890123456789012345678901234567890"
155 "12345678901234567890123456789012345678901234567890" 215 "12345678901234567890123456789012345678901234567890"
156 "12345678901234567890123456789012345678901234567890" 216 "12345678901234567890123456789012345678901234567890"
157 "12345678901234567890123456789012345678901234567890" 217 "12345678901234567890123456789012345678901234567890"
158 "12345678901234567890123456789012345678901234567890" 218 "12345678901234567890123456789012345678901234567890"
159 "12345678901234567890123456789012345678901234567890" 219 "12345678901234567890123456789012345678901234567890"
160 "12345678901234567890123456789012345678901234567890" 220 "12345678901234567890123456789012345678901234567890"
161 "12345678901234567890123456789012345678901234567890" 221 "12345678901234567890123456789012345678901234567890"
162 "12345678901234567890123456789012345678901234567890" 222 "12345678901234567890123456789012345678901234567890"
163 "12345678901234567890123456789012345678901234567890"); 223 "12345678901234567890123456789012345678901234567890");
164 dbl = BigintOperations::ToDouble(bigint); 224 dbl = BigintOperations::ToDouble(bigint);
165 EXPECT_EQ(1.0/zero, dbl.value()); 225 EXPECT_EQ(1.0/zero, dbl.value());
166 226
227 bigint = BigintOperations::NewFromCString(
228 "17976931348623157081452742373170435679807056752584"
229 "49965989174768031572607800285387605895586327668781"
230 "71540458953514382464234321326889464182768467546703"
231 "53751698604991057655128207624549009038932894407586"
232 "85084551339423045832369032229481658085593321233482"
233 "74797826204144723168738177180919299881250404026184"
234 "124858368");
235 dbl = BigintOperations::ToDouble(bigint);
236 EXPECT_EQ(1.7976931348623157e308, dbl.value());
237
238 bigint = BigintOperations::NewFromCString(
239 "17976931348623159077293051907890247336179769789423"
240 "06572734300811577326758055009631327084773224075360"
241 "21120113879871393357658789768814416622492847430639"
242 "47412437776789342486548527630221960124609411945308"
243 "29520850057688381506823424628814739131105408272371"
244 "63350510684586298239947245938479716304835356329624"
245 "224137216");
246 dbl = BigintOperations::ToDouble(bigint);
247 EXPECT_EQ(1.0/zero, dbl.value());
248
249 bigint = BigintOperations::NewFromCString(
250 "17976931348623158079372897140530341507993413271003"
251 "78269361737789804449682927647509466490179775872070"
252 "96330286416692887910946555547851940402630657488671"
253 "50582068190890200070838367627385484581771153176447"
254 "57302700698555713669596228429148198608349364752927"
255 "19074168444365510704342711559699508093042880177904"
256 "174497792");
257 dbl = BigintOperations::ToDouble(bigint);
258 EXPECT_EQ(1.0/zero, dbl.value());
259
260 bigint = BigintOperations::NewFromCString(
261 "17976931348623158079372897140530341507993413271003"
262 "78269361737789804449682927647509466490179775872070"
263 "96330286416692887910946555547851940402630657488671"
264 "50582068190890200070838367627385484581771153176447"
265 "57302700698555713669596228429148198608349364752927"
266 "19074168444365510704342711559699508093042880177904"
267 "174497791");
268 dbl = BigintOperations::ToDouble(bigint);
269 EXPECT_EQ(1.7976931348623157e308, dbl.value());
270
167 bigint = BigintOperations::NewFromCString("100000000000000000000000"); 271 bigint = BigintOperations::NewFromCString("100000000000000000000000");
168 dbl = BigintOperations::ToDouble(bigint); 272 dbl = BigintOperations::ToDouble(bigint);
169 EXPECT_EQ(1e+23, dbl.value()); 273 EXPECT_EQ(1e+23, dbl.value());
170 274
171 // TODO(floitsch): Proper rounding if deemed necessary.
172 #if 0
173 bigint = BigintOperations::NewFromCString("100000000000000000000001"); 275 bigint = BigintOperations::NewFromCString("100000000000000000000001");
174 dbl = BigintOperations::ToDouble(bigint); 276 dbl = BigintOperations::ToDouble(bigint);
175 // EXPECT_EQ(1.0000000000000001e+23, dbl.value()); 277 EXPECT_EQ(1.0000000000000001e+23, dbl.value());
176 EXPECT_EQ(9.9999999999999992e+22, dbl.value());
177 #endif
178 278
179 // Same but shifted 64 bits to the left. 279 // Same but shifted 64 bits to the left.
180 bigint = BigintOperations::NewFromCString( 280 bigint = BigintOperations::NewFromCString(
181 "1844674407370955161600000000000000000000000"); 281 "1844674407370955161600000000000000000000000");
182 dbl = BigintOperations::ToDouble(bigint); 282 dbl = BigintOperations::ToDouble(bigint);
183 EXPECT_EQ(1.844674407370955e+42, dbl.value()); 283 EXPECT_EQ(1.844674407370955e+42, dbl.value());
184 284
185 bigint = BigintOperations::NewFromCString( 285 bigint = BigintOperations::NewFromCString(
186 "1844674407370955161600000000000000000000001"); 286 "1844674407370955161600000000000000000000001");
187 dbl = BigintOperations::ToDouble(bigint); 287 dbl = BigintOperations::ToDouble(bigint);
188 // TODO(floitsch): Proper rounding if deemed necessary. 288 EXPECT_EQ(1.8446744073709553e+42, dbl.value());
189 // EXPECT_EQ(1.8446744073709553e+42, dbl.value());
190 EXPECT_EQ(1.844674407370955e+42, dbl.value());
191 } 289 }
192 290
193 291
194 TEST_CASE(BigintHexStrings) { 292 TEST_CASE(BigintHexStrings) {
195 { 293 {
196 const Bigint& bigint = Bigint::Handle( 294 const Bigint& bigint = Bigint::Handle(
197 BigintOperations::NewFromCString("0x0")); 295 BigintOperations::NewFromCString("0x0"));
198 EXPECT(BigintOperations::FitsIntoSmi(bigint)); 296 EXPECT(BigintOperations::FitsIntoSmi(bigint));
199 const Smi& smi = Smi::Handle(BigintOperations::ToSmi(bigint)); 297 const Smi& smi = Smi::Handle(BigintOperations::ToSmi(bigint));
200 EXPECT_EQ(0, smi.Value()); 298 EXPECT_EQ(0, smi.Value());
(...skipping 1927 matching lines...) Expand 10 before | Expand all | Expand 10 after
2128 "123456789012345678901234567890123456789012345678901234567890123456789012" 2226 "123456789012345678901234567890123456789012345678901234567890123456789012"
2129 "345678901234567890123456789012345678901234567890123456789012345678901234" 2227 "345678901234567890123456789012345678901234567890123456789012345678901234"
2130 "567890123456789012345678901234567890123456789012345678901234567890123456" 2228 "567890123456789012345678901234567890123456789012345678901234567890123456"
2131 "789012345678901234567890123456789012345678901234567890123456789012345678" 2229 "789012345678901234567890123456789012345678901234567890123456789012345678"
2132 "90123456789012345678901234567890", 2230 "90123456789012345678901234567890",
2133 "0x1234567890ABCDEF01234567890ABCDEF01234567890ABCDEF01234567890ABCDEF" 2231 "0x1234567890ABCDEF01234567890ABCDEF01234567890ABCDEF01234567890ABCDEF"
2134 "01234567890ABCDEE"); 2232 "01234567890ABCDEE");
2135 } 2233 }
2136 2234
2137 } // namespace dart 2235 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/bigint_operations.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698