diff options
author | Christian Heimes <christian@cheimes.de> | 2008-05-07 22:54:17 (GMT) |
---|---|---|
committer | Christian Heimes <christian@cheimes.de> | 2008-05-07 22:54:17 (GMT) |
commit | 33e4a98a3ea14e58a4a1c4a7765b9be33b14a5d9 (patch) | |
tree | 3a59b19c83cef7eed4125d2840e21529d5e9ee5e | |
parent | 342c095b8c14394a470ec5340205043ac553181f (diff) | |
download | cpython-33e4a98a3ea14e58a4a1c4a7765b9be33b14a5d9.zip cpython-33e4a98a3ea14e58a4a1c4a7765b9be33b14a5d9.tar.gz cpython-33e4a98a3ea14e58a4a1c4a7765b9be33b14a5d9.tar.bz2 |
Replace more float hacks with correct math functions
-rw-r--r-- | Lib/json/encoder.py | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index d8d4770..b1dd570 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -2,6 +2,7 @@ """ import re +import math try: from _json import encode_basestring_ascii as c_encode_basestring_ascii @@ -25,20 +26,19 @@ ESCAPE_DCT = { for i in range(0x20): ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i)) -# Assume this produces an infinity on all machines (probably not guaranteed) -INFINITY = float('1e66666') FLOAT_REPR = repr def floatstr(o, allow_nan=True): # Check for specials. Note that this type of test is processor- and/or # platform-specific, so do tests which don't depend on the internals. - if o != o: + if math.isnan(o): text = 'NaN' - elif o == INFINITY: - text = 'Infinity' - elif o == -INFINITY: - text = '-Infinity' + elif math.isinf(o): + if math.copysign(1., o) == 1.: + text = 'Infinity' + else: + text = '-Infinity' else: return FLOAT_REPR(o) |