Skip to content

Commit 01fd7c6

Browse files
committed
Ensure numeric data is preserved
While JSON is happy to transfer 5.0 and 55555555555555555555555555 out of Python, it doesn't survive when loaded back into JavaScript. The 5.0 becomes 5, and precision is lost on long numbers. This patch changes the representation 5.0 to ['NUMBER-LITERAL', '5.0']
1 parent 2b87b4a commit 01fd7c6

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

‎v3/js/pytutor.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,10 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
21972197

21982198
d3DomElement.append('<span class="stringObj">' + literalStr + '</span>');
21992199
}
2200+
else if (obj instanceof Array && obj[0] == "NUMBER-LITERAL") {
2201+
// actually transmitted as a string
2202+
d3DomElement.append('<span class="numberObj">' + obj[1] + '</span>');
2203+
}
22002204
else {
22012205
assert(false);
22022206
}
@@ -3118,7 +3122,7 @@ function structurallyEquivalent(obj1, obj2) {
31183122

31193123
function isPrimitiveType(obj) {
31203124
var typ = typeof obj;
3121-
return ((obj == null) || (typ != "object"));
3125+
return ((obj == null) || (typ != "object") || (obj instanceof Array && obj[0] == "NUMBER-LITERAL"));
31223126
}
31233127

31243128
function getRefID(obj) {

‎v3/pg_encoder.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ def encode(self, dat, get_parent):
137137
# primitive type
138138
if not self.render_heap_primitives and type(dat) in PRIMITIVE_TYPES:
139139
if type(dat) is float:
140-
return round(dat, FLOAT_PRECISION)
140+
return ['NUMBER-LITERAL', str(round(dat, FLOAT_PRECISION))]
141141
else:
142-
return dat
142+
return ['NUMBER-LITERAL', str(dat)]
143143

144144
# compound type - return an object reference and update encoded_heap_objects
145145
else:

0 commit comments

Comments
 (0)