Skip to content

Commit a25c092

Browse files
committed
Fix %g formatting of floats < 0.0
1 parent 6948ea6 commit a25c092

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_formatting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def test_complex_formatting():
114114
assert format(complex(1, float("Inf")), "") == "(1+infj)"
115115
assert format(MyComplex(3j), "") == "42"
116116
assert format(MyComplex(3j), " <5") == "3j "
117+
assert format(complex(2**53 + 1, 0)) == '(9007199254740992+0j)'
117118

118119

119120
class AnyRepr:

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_complex.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*graalpython.lib-python.3.test.test_complex.ComplexTest.test_conjugate
44
*graalpython.lib-python.3.test.test_complex.ComplexTest.test_divmod
55
*graalpython.lib-python.3.test.test_complex.ComplexTest.test_file
6+
*graalpython.lib-python.3.test.test_complex.ComplexTest.test_format
67
*graalpython.lib-python.3.test.test_complex.ComplexTest.test_getnewargs
78
*graalpython.lib-python.3.test.test_complex.ComplexTest.test_neg
89
*graalpython.lib-python.3.test.test_complex.ComplexTest.test_negated_imaginary_literal

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/formatting/FloatFormatter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public FloatFormatter(PythonCore core, FormattingBuffer result, Spec spec, boole
5555
minFracDigits = 0;
5656
} else if (spec.alternate) {
5757
// Alternate form means do not trim the zero fractional digits.
58+
// This should be equivalent to Py_DTSF_ALT flag in CPython
5859
minFracDigits = -1;
5960
} else if (spec.type == 'r' || spec.type == Spec.NONE) {
6061
// These formats by default show at least one fractional digit.
@@ -649,11 +650,10 @@ private void appendFixed(CharSequence digits, int exp, int precision) {
649650
}
650651
lenWhole = digitCount;
651652
}
652-
653-
if (noTruncate) {
654-
// Extend the fraction as BigDecimal will have economised on zeros.
655-
appendPointAndTrailingZeros(precision - digitCount);
656-
}
653+
}
654+
if (noTruncate) {
655+
// Extend the fraction as BigDecimal will have economised on zeros.
656+
appendPointAndTrailingZeros(lenFraction + precision - digitCount);
657657
}
658658

659659
// Finally, ensure we have all and only the fractional digits we should.

0 commit comments

Comments
 (0)