Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,14 @@ def test_repr(self):
self.assertEqual(v, eval(repr(v)))
floats_file.close()

def test_repr_subnormal_one_digit(self):
self.assertEqual(repr(5e-324), '5e-324')
self.assertEqual(repr(1e-323), '1e-323')
self.assertEqual(repr(1.5e-323), '1.5e-323')
self.assertEqual(repr(5e-323), '5e-323')
self.assertEqual(repr(-5e-324), '-5e-324')
self.assertEqual(repr(2.2250738585072014e-308), '2.2250738585072014e-308')

@unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
"applies only when using short float repr style")
def test_short_repr(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,14 @@ private static int reprDigits(double value, int maxDigits, FormattingBuffer.Stri

// Most of the work is done by Double.
String s = Double.toString(value);
if (value < Double.MIN_NORMAL) {
// Double.toString prints at least two digits, so for a subnormal it can miss a
// one-digit decimal that round-trips.
BigDecimal oneDigit = new BigDecimal(value).round(new MathContext(1, RoundingMode.HALF_EVEN));
if (oneDigit.doubleValue() == value) {
s = oneDigit.unscaledValue() + ".0E" + -oneDigit.scale();
}
}

// Variables for scanning the string
int p = 0, end = s.length(), first = 0, point = end, exp;
Expand Down