Skip to content

Commit f100534

Browse files
Tim-Stncw
authored andcommitted
Fix repr(float) when given integral numbers - fixes #103
1 parent af17d7d commit f100534

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

py/float.go

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ func FloatNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
4848
}
4949

5050
func (a Float) M__str__() (Object, error) {
51+
if i := int64(a); Float(i) == a {
52+
return String(fmt.Sprintf("%d.0", i)), nil
53+
}
5154
return String(fmt.Sprintf("%g", a)), nil
5255
}
5356

py/tests/float.py

+20
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,24 @@
1414
assert float(" -1E400") == float("-inf")
1515
assertRaises(ValueError, float, "1 E200")
1616

17+
doc="repr"
18+
assert repr(float("1.0")) == "1.0"
19+
assert repr(float("1.")) == "1.0"
20+
assert repr(float("1.1")) == "1.1"
21+
assert repr(float("1.11")) == "1.11"
22+
assert repr(float("-1.0")) == "-1.0"
23+
assert repr(float("1.00101")) == "1.00101"
24+
assert repr(float("1.00")) == "1.0"
25+
assert repr(float("2.010")) == "2.01"
26+
27+
doc="str"
28+
assert str(float("1.0")) == "1.0"
29+
assert str(float("1.")) == "1.0"
30+
assert str(float("1.1")) == "1.1"
31+
assert str(float("1.11")) == "1.11"
32+
assert str(float("-1.0")) == "-1.0"
33+
assert str(float("1.00101")) == "1.00101"
34+
assert str(float("1.00")) == "1.0"
35+
assert str(float("2.010")) == "2.01"
36+
1737
doc="finished"

py/tests/list.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
assert str([1,2,3]) == "[1, 2, 3]"
1010
assert str([1,[2,3],4]) == "[1, [2, 3], 4]"
1111
assert str(["1",[2.5,17,[]]]) == "['1', [2.5, 17, []]]"
12+
assert str([1, 1.0]) == "[1, 1.0]"
1213

1314
doc="repr"
1415
assert repr([]) == "[]"
1516
assert repr([1,2,3]) == "[1, 2, 3]"
1617
assert repr([1,[2,3],4]) == "[1, [2, 3], 4]"
1718
assert repr(["1",[2.5,17,[]]]) == "['1', [2.5, 17, []]]"
19+
assert repr([1, 1.0]) == "[1, 1.0]"
1820

1921
doc="enumerate"
2022
a = [e for e in enumerate([3,4,5,6,7], 4)]

py/tests/tuple.py

+2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
assert str((1,2,3)) == "(1, 2, 3)"
88
assert str((1,(2,3),4)) == "(1, (2, 3), 4)"
99
assert str(("1",(2.5,17,()))) == "('1', (2.5, 17, ()))"
10+
assert str((1, 1.0)) == "(1, 1.0)"
1011

1112
doc="repr"
1213
assert repr(()) == "()"
1314
assert repr((1,2,3)) == "(1, 2, 3)"
1415
assert repr((1,(2,3),4)) == "(1, (2, 3), 4)"
1516
assert repr(("1",(2.5,17,()))) == "('1', (2.5, 17, ()))"
17+
assert repr((1, 1.0)) == "(1, 1.0)"
1618

1719
doc="mul"
1820
a = (1, 2, 3)

0 commit comments

Comments
 (0)