OLD | NEW |
(Empty) | |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 |
| 3 #include <stdlib.h> |
| 4 |
| 5 #include "cctest.h" |
| 6 #include "diy-fp.h" |
| 7 #include "double.h" |
| 8 #include "fast-dtoa.h" |
| 9 #include "gay-precision.h" |
| 10 #include "gay-shortest.h" |
| 11 #include "utils.h" |
| 12 |
| 13 using namespace double_conversion; |
| 14 |
| 15 static const int kBufferSize = 100; |
| 16 |
| 17 |
| 18 // Removes trailing '0' digits. |
| 19 static void TrimRepresentation(Vector<char> representation) { |
| 20 int len = strlen(representation.start()); |
| 21 int i; |
| 22 for (i = len - 1; i >= 0; --i) { |
| 23 if (representation[i] != '0') break; |
| 24 } |
| 25 representation[i + 1] = '\0'; |
| 26 } |
| 27 |
| 28 |
| 29 TEST(FastDtoaShortestVariousDoubles) { |
| 30 char buffer_container[kBufferSize]; |
| 31 Vector<char> buffer(buffer_container, kBufferSize); |
| 32 int length; |
| 33 int point; |
| 34 int status; |
| 35 |
| 36 double min_double = 5e-324; |
| 37 status = FastDtoa(min_double, FAST_DTOA_SHORTEST, 0, |
| 38 buffer, &length, &point); |
| 39 CHECK(status); |
| 40 CHECK_EQ("5", buffer.start()); |
| 41 CHECK_EQ(-323, point); |
| 42 |
| 43 double max_double = 1.7976931348623157e308; |
| 44 status = FastDtoa(max_double, FAST_DTOA_SHORTEST, 0, |
| 45 buffer, &length, &point); |
| 46 CHECK(status); |
| 47 CHECK_EQ("17976931348623157", buffer.start()); |
| 48 CHECK_EQ(309, point); |
| 49 |
| 50 status = FastDtoa(4294967272.0, FAST_DTOA_SHORTEST, 0, |
| 51 buffer, &length, &point); |
| 52 CHECK(status); |
| 53 CHECK_EQ("4294967272", buffer.start()); |
| 54 CHECK_EQ(10, point); |
| 55 |
| 56 status = FastDtoa(4.1855804968213567e298, FAST_DTOA_SHORTEST, 0, |
| 57 buffer, &length, &point); |
| 58 CHECK(status); |
| 59 CHECK_EQ("4185580496821357", buffer.start()); |
| 60 CHECK_EQ(299, point); |
| 61 |
| 62 status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_SHORTEST, 0, |
| 63 buffer, &length, &point); |
| 64 CHECK(status); |
| 65 CHECK_EQ("5562684646268003", buffer.start()); |
| 66 CHECK_EQ(-308, point); |
| 67 |
| 68 status = FastDtoa(2147483648.0, FAST_DTOA_SHORTEST, 0, |
| 69 buffer, &length, &point); |
| 70 CHECK(status); |
| 71 CHECK_EQ("2147483648", buffer.start()); |
| 72 CHECK_EQ(10, point); |
| 73 |
| 74 status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_SHORTEST, 0, |
| 75 buffer, &length, &point); |
| 76 if (status) { // Not all FastDtoa variants manage to compute this number. |
| 77 CHECK_EQ("35844466002796428", buffer.start()); |
| 78 CHECK_EQ(299, point); |
| 79 } |
| 80 |
| 81 uint64_t smallest_normal64 = UINT64_2PART_C(0x00100000, 00000000); |
| 82 double v = Double(smallest_normal64).value(); |
| 83 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point); |
| 84 if (status) { |
| 85 CHECK_EQ("22250738585072014", buffer.start()); |
| 86 CHECK_EQ(-307, point); |
| 87 } |
| 88 |
| 89 uint64_t largest_denormal64 = UINT64_2PART_C(0x000FFFFF, FFFFFFFF); |
| 90 v = Double(largest_denormal64).value(); |
| 91 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point); |
| 92 if (status) { |
| 93 CHECK_EQ("2225073858507201", buffer.start()); |
| 94 CHECK_EQ(-307, point); |
| 95 } |
| 96 } |
| 97 |
| 98 |
| 99 TEST(FastDtoaPrecisionVariousDoubles) { |
| 100 char buffer_container[kBufferSize]; |
| 101 Vector<char> buffer(buffer_container, kBufferSize); |
| 102 int length; |
| 103 int point; |
| 104 int status; |
| 105 |
| 106 status = FastDtoa(1.0, FAST_DTOA_PRECISION, 3, buffer, &length, &point); |
| 107 CHECK(status); |
| 108 CHECK(3 >= length); |
| 109 TrimRepresentation(buffer); |
| 110 CHECK_EQ("1", buffer.start()); |
| 111 CHECK_EQ(1, point); |
| 112 |
| 113 status = FastDtoa(1.5, FAST_DTOA_PRECISION, 10, buffer, &length, &point); |
| 114 if (status) { |
| 115 CHECK(10 >= length); |
| 116 TrimRepresentation(buffer); |
| 117 CHECK_EQ("15", buffer.start()); |
| 118 CHECK_EQ(1, point); |
| 119 } |
| 120 |
| 121 double min_double = 5e-324; |
| 122 status = FastDtoa(min_double, FAST_DTOA_PRECISION, 5, |
| 123 buffer, &length, &point); |
| 124 CHECK(status); |
| 125 CHECK_EQ("49407", buffer.start()); |
| 126 CHECK_EQ(-323, point); |
| 127 |
| 128 double max_double = 1.7976931348623157e308; |
| 129 status = FastDtoa(max_double, FAST_DTOA_PRECISION, 7, |
| 130 buffer, &length, &point); |
| 131 CHECK(status); |
| 132 CHECK_EQ("1797693", buffer.start()); |
| 133 CHECK_EQ(309, point); |
| 134 |
| 135 status = FastDtoa(4294967272.0, FAST_DTOA_PRECISION, 14, |
| 136 buffer, &length, &point); |
| 137 if (status) { |
| 138 CHECK(14 >= length); |
| 139 TrimRepresentation(buffer); |
| 140 CHECK_EQ("4294967272", buffer.start()); |
| 141 CHECK_EQ(10, point); |
| 142 } |
| 143 |
| 144 status = FastDtoa(4.1855804968213567e298, FAST_DTOA_PRECISION, 17, |
| 145 buffer, &length, &point); |
| 146 CHECK(status); |
| 147 CHECK_EQ("41855804968213567", buffer.start()); |
| 148 CHECK_EQ(299, point); |
| 149 |
| 150 status = FastDtoa(5.5626846462680035e-309, FAST_DTOA_PRECISION, 1, |
| 151 buffer, &length, &point); |
| 152 CHECK(status); |
| 153 CHECK_EQ("6", buffer.start()); |
| 154 CHECK_EQ(-308, point); |
| 155 |
| 156 status = FastDtoa(2147483648.0, FAST_DTOA_PRECISION, 5, |
| 157 buffer, &length, &point); |
| 158 CHECK(status); |
| 159 CHECK_EQ("21475", buffer.start()); |
| 160 CHECK_EQ(10, point); |
| 161 |
| 162 status = FastDtoa(3.5844466002796428e+298, FAST_DTOA_PRECISION, 10, |
| 163 buffer, &length, &point); |
| 164 CHECK(status); |
| 165 CHECK(10 >= length); |
| 166 TrimRepresentation(buffer); |
| 167 CHECK_EQ("35844466", buffer.start()); |
| 168 CHECK_EQ(299, point); |
| 169 |
| 170 uint64_t smallest_normal64 = UINT64_2PART_C(0x00100000, 00000000); |
| 171 double v = Double(smallest_normal64).value(); |
| 172 status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point); |
| 173 CHECK(status); |
| 174 CHECK_EQ("22250738585072014", buffer.start()); |
| 175 CHECK_EQ(-307, point); |
| 176 |
| 177 uint64_t largest_denormal64 = UINT64_2PART_C(0x000FFFFF, FFFFFFFF); |
| 178 v = Double(largest_denormal64).value(); |
| 179 status = FastDtoa(v, FAST_DTOA_PRECISION, 17, buffer, &length, &point); |
| 180 CHECK(status); |
| 181 CHECK(20 >= length); |
| 182 TrimRepresentation(buffer); |
| 183 CHECK_EQ("22250738585072009", buffer.start()); |
| 184 CHECK_EQ(-307, point); |
| 185 |
| 186 v = 3.3161339052167390562200598e-237; |
| 187 status = FastDtoa(v, FAST_DTOA_PRECISION, 18, buffer, &length, &point); |
| 188 CHECK(status); |
| 189 CHECK_EQ("331613390521673906", buffer.start()); |
| 190 CHECK_EQ(-236, point); |
| 191 |
| 192 v = 7.9885183916008099497815232e+191; |
| 193 status = FastDtoa(v, FAST_DTOA_PRECISION, 4, buffer, &length, &point); |
| 194 CHECK(status); |
| 195 CHECK_EQ("7989", buffer.start()); |
| 196 CHECK_EQ(192, point); |
| 197 } |
| 198 |
| 199 |
| 200 TEST(FastDtoaGayShortest) { |
| 201 char buffer_container[kBufferSize]; |
| 202 Vector<char> buffer(buffer_container, kBufferSize); |
| 203 bool status; |
| 204 int length; |
| 205 int point; |
| 206 int succeeded = 0; |
| 207 int total = 0; |
| 208 bool needed_max_length = false; |
| 209 |
| 210 Vector<const PrecomputedShortest> precomputed = |
| 211 PrecomputedShortestRepresentations(); |
| 212 for (int i = 0; i < precomputed.length(); ++i) { |
| 213 const PrecomputedShortest current_test = precomputed[i]; |
| 214 total++; |
| 215 double v = current_test.v; |
| 216 status = FastDtoa(v, FAST_DTOA_SHORTEST, 0, buffer, &length, &point); |
| 217 CHECK(kFastDtoaMaximalLength >= length); |
| 218 if (!status) continue; |
| 219 if (length == kFastDtoaMaximalLength) needed_max_length = true; |
| 220 succeeded++; |
| 221 CHECK_EQ(current_test.decimal_point, point); |
| 222 CHECK_EQ(current_test.representation, buffer.start()); |
| 223 } |
| 224 CHECK(succeeded*1.0/total > 0.99); |
| 225 CHECK(needed_max_length); |
| 226 } |
| 227 |
| 228 |
| 229 TEST(FastDtoaGayPrecision) { |
| 230 char buffer_container[kBufferSize]; |
| 231 Vector<char> buffer(buffer_container, kBufferSize); |
| 232 bool status; |
| 233 int length; |
| 234 int point; |
| 235 int succeeded = 0; |
| 236 int total = 0; |
| 237 // Count separately for entries with less than 15 requested digits. |
| 238 int succeeded_15 = 0; |
| 239 int total_15 = 0; |
| 240 |
| 241 Vector<const PrecomputedPrecision> precomputed = |
| 242 PrecomputedPrecisionRepresentations(); |
| 243 for (int i = 0; i < precomputed.length(); ++i) { |
| 244 const PrecomputedPrecision current_test = precomputed[i]; |
| 245 double v = current_test.v; |
| 246 int number_digits = current_test.number_digits; |
| 247 total++; |
| 248 if (number_digits <= 15) total_15++; |
| 249 status = FastDtoa(v, FAST_DTOA_PRECISION, number_digits, |
| 250 buffer, &length, &point); |
| 251 CHECK(number_digits >= length); |
| 252 if (!status) continue; |
| 253 succeeded++; |
| 254 if (number_digits <= 15) succeeded_15++; |
| 255 TrimRepresentation(buffer); |
| 256 CHECK_EQ(current_test.decimal_point, point); |
| 257 CHECK_EQ(current_test.representation, buffer.start()); |
| 258 } |
| 259 // The precomputed numbers contain many entries with many requested |
| 260 // digits. These have a high failure rate and we therefore expect a lower |
| 261 // success rate than for the shortest representation. |
| 262 CHECK(succeeded*1.0/total > 0.85); |
| 263 // However with less than 15 digits almost the algorithm should almost always |
| 264 // succeed. |
| 265 CHECK(succeeded_15*1.0/total_15 > 0.9999); |
| 266 } |
OLD | NEW |