diff Lib/json/encoder.py @ 46719:ef47df25423c legacy-trunk

Replace more float hacks with correct math functions
author Christian Heimes <christian@cheimes.de>
date Wed, 07 May 2008 22:54:17 +0000
parents 4ded35754cb7
children d95e5add3ca4
line wrap: on
line diff
--- a/Lib/json/encoder.py	Wed May 07 02:23:43 2008 +0000
+++ b/Lib/json/encoder.py	Wed May 07 22:54:17 2008 +0000
@@ -2,6 +2,7 @@
 """
 
 import re
+import math
 
 try:
     from _json import encode_basestring_ascii as c_encode_basestring_ascii
@@ -25,20 +26,19 @@
 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)