Skip to content

Commit 7071ff2

Browse files
pgbovinemmmicedcoffee
authored andcommitted
added support for inf, -inf, nan, and prettier printing of floats
1 parent 974af62 commit 7071ff2

4 files changed

Lines changed: 127 additions & 7 deletions

File tree

‎v3/js/pytutor.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2183,6 +2183,10 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
21832183

21842184
d3DomElement.append('<span class="stringObj">' + literalStr + '</span>');
21852185
}
2186+
else if (typ == "object") {
2187+
assert(obj[0] == 'SPECIAL_FLOAT');
2188+
d3DomElement.append('<span class="numberObj">' + obj[1] + '</span>');
2189+
}
21862190
else {
21872191
assert(false);
21882192
}
@@ -3104,6 +3108,12 @@ function structurallyEquivalent(obj1, obj2) {
31043108

31053109
function isPrimitiveType(obj) {
31063110
var typ = typeof obj;
3111+
3112+
// kludge: 'SPECIAL_FLOAT' objects count as primitives
3113+
if (typ == "object" && obj[0] == 'SPECIAL_FLOAT') {
3114+
return true;
3115+
}
3116+
31073117
return ((obj == null) || (typ != "object"));
31083118
}
31093119

‎v3/pg_encoder.py‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@
3535
# Format:
3636
# Primitives:
3737
# * None, int, long, float, str, bool - unchanged
38-
# (json.dumps encodes these fine verbatim)
38+
# (json.dumps encodes these fine verbatim, except for inf, -inf, and nan)
39+
#
40+
# exceptions: float('inf') -> ['SPECIAL_FLOAT', 'Infinity']
41+
# float('-inf') -> ['SPECIAL_FLOAT', '-Infinity']
42+
# float('nan') -> ['SPECIAL_FLOAT', 'NaN']
43+
# x == int(x) -> ['SPECIAL_FLOAT', '%.1f' % x]
44+
# (this way, 3.0 prints as '3.0' and not as 3, which looks like an int)
3945
#
4046
# If render_heap_primitives is True, then primitive values are rendered
4147
# on the heap as ['HEAP_PRIMITIVE', <type name>, <value>]
4248
#
49+
# (for SPECIAL_FLOAT values, <value> is a list like ['SPECIAL_FLOAT', 'Infinity'])
50+
#
4351
# Compound objects:
4452
# * list - ['LIST', elt1, elt2, elt3, ..., eltN]
4553
# * tuple - ['TUPLE', elt1, elt2, elt3, ..., eltN]
@@ -62,11 +70,13 @@
6270

6371
import re, types
6472
import sys
73+
import math
6574
typeRE = re.compile("<type '(.*)'>")
6675
classRE = re.compile("<class '(.*)'>")
6776

6877
import inspect
6978

79+
# TODO: maybe use the 'six' library to smooth over Py2 and Py3 incompatibilities?
7080
is_python3 = (sys.version_info[0] == 3)
7181
if is_python3:
7282
long = None # Avoid NameError when evaluating "long"
@@ -98,6 +108,26 @@ def get_name(obj):
98108

99109
PRIMITIVE_TYPES = (int, long, float, str, bool, type(None))
100110

111+
def encode_primitive(dat):
112+
if type(dat) is float:
113+
if math.isinf(dat):
114+
if dat > 0:
115+
return ['SPECIAL_FLOAT', 'Infinity']
116+
else:
117+
return ['SPECIAL_FLOAT', '-Infinity']
118+
elif math.isnan(dat):
119+
return ['SPECIAL_FLOAT', 'NaN']
120+
else:
121+
# render floats like 3.0 as '3.0' and not as 3
122+
if dat == int(dat):
123+
return ['SPECIAL_FLOAT', '%.1f' % dat]
124+
else:
125+
return round(dat, FLOAT_PRECISION)
126+
else:
127+
# return all other primitives verbatim
128+
return dat
129+
130+
101131
# Note that this might BLOAT MEMORY CONSUMPTION since we're holding on
102132
# to every reference ever created by the program without ever releasing
103133
# anything!
@@ -136,11 +166,7 @@ def encode(self, dat, get_parent):
136166
"""Encode a data value DAT using the GET_PARENT function for parent ids."""
137167
# primitive type
138168
if not self.render_heap_primitives and type(dat) in PRIMITIVE_TYPES:
139-
if type(dat) is float:
140-
return round(dat, FLOAT_PRECISION)
141-
else:
142-
return dat
143-
169+
return encode_primitive(dat)
144170
# compound type - return an object reference and update encoded_heap_objects
145171
else:
146172
my_id = id(dat)
@@ -229,7 +255,7 @@ def encode(self, dat, get_parent):
229255
new_obj.extend(['module', dat.__name__])
230256
elif typ in PRIMITIVE_TYPES:
231257
assert self.render_heap_primitives
232-
new_obj.extend(['HEAP_PRIMITIVE', type(dat).__name__, dat])
258+
new_obj.extend(['HEAP_PRIMITIVE', type(dat).__name__, encode_primitive(dat)])
233259
else:
234260
typeStr = str(typ)
235261
m = typeRE.match(typeStr)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"code": "x = float('inf')\ny = float('-inf')\nz = float('nan')\n",
3+
"trace": [
4+
{
5+
"ordered_globals": [],
6+
"stdout": "",
7+
"func_name": "<module>",
8+
"stack_to_render": [],
9+
"globals": {},
10+
"heap": {},
11+
"line": 1,
12+
"event": "step_line"
13+
},
14+
{
15+
"ordered_globals": [
16+
"x"
17+
],
18+
"stdout": "",
19+
"func_name": "<module>",
20+
"stack_to_render": [],
21+
"globals": {
22+
"x": [
23+
"SPECIAL_FLOAT",
24+
"Infinity"
25+
]
26+
},
27+
"heap": {},
28+
"line": 2,
29+
"event": "step_line"
30+
},
31+
{
32+
"ordered_globals": [
33+
"x",
34+
"y"
35+
],
36+
"stdout": "",
37+
"func_name": "<module>",
38+
"stack_to_render": [],
39+
"globals": {
40+
"y": [
41+
"SPECIAL_FLOAT",
42+
"-Infinity"
43+
],
44+
"x": [
45+
"SPECIAL_FLOAT",
46+
"Infinity"
47+
]
48+
},
49+
"heap": {},
50+
"line": 3,
51+
"event": "step_line"
52+
},
53+
{
54+
"ordered_globals": [
55+
"x",
56+
"y",
57+
"z"
58+
],
59+
"stdout": "",
60+
"func_name": "<module>",
61+
"stack_to_render": [],
62+
"globals": {
63+
"y": [
64+
"SPECIAL_FLOAT",
65+
"-Infinity"
66+
],
67+
"x": [
68+
"SPECIAL_FLOAT",
69+
"Infinity"
70+
],
71+
"z": [
72+
"SPECIAL_FLOAT",
73+
"NaN"
74+
]
75+
},
76+
"heap": {},
77+
"line": 3,
78+
"event": "return"
79+
}
80+
]
81+
}

‎v3/tests/backend-tests/inf-nan.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x = float('inf')
2+
y = float('-inf')
3+
z = float('nan')

0 commit comments

Comments
 (0)