File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -140,7 +140,9 @@ struct floating_decimal_128 generic_binary_to_decimal(
140
140
// Step 2: Determine the interval of legal decimal representations.
141
141
const uint128_t mv = 4 * m2 ;
142
142
// Implicit bool -> int conversion. True is 1, false is 0.
143
- const uint32_t mmShift = (ieeeMantissa != 0 ) || (ieeeExponent == 0 );
143
+ const uint32_t mmShift =
144
+ (ieeeMantissa != (explicitLeadingBit ? ONE << (mantissaBits - 1 ) : 0 ))
145
+ || (ieeeExponent == 0 );
144
146
145
147
// Step 3: Convert to a decimal power base using 128-bit arithmetic.
146
148
uint128_t vr , vp , vm ;
Original file line number Diff line number Diff line change @@ -342,3 +342,19 @@ TEST(Generic128Test, long_double_to_fd128) {
342
342
ASSERT_L2S (" 9.409340012568248E18" , 9 .409340012568248E18L);
343
343
ASSERT_L2S (" 1.2345678E0" , 1 .2345678L );
344
344
}
345
+
346
+ TEST (Generic128Test, regression_test_long_double) {
347
+ // The binary 80-bit representation of this number has a mantissa that is a
348
+ // one followed by zeros. This is a special case, because the next lower
349
+ // number is closer to X than the next higher number, so it is possible that
350
+ // we need to print more digits.
351
+ // Before this test was added, the code incorrectly checked for an all-zeroes
352
+ // mantissa in this case - the 80-bit format has an *explicit* leading one,
353
+ // and the code did not take that into account.
354
+ long double l = 1 .10169395793497080013e-4927L ;
355
+ ASSERT_L2S (" 1.10169395793497080013E-4927" , l);
356
+ // Also check that the next higher and next lower number have *different*
357
+ // decimal representations.
358
+ ASSERT_L2S (" 1.1016939579349708003E-4927" , nextafterl (l, INFINITY));
359
+ ASSERT_L2S (" 1.1016939579349708001E-4927" , nextafterl (l, -INFINITY));
360
+ }
You can’t perform that action at this time.
0 commit comments