# HG changeset patch
# User Bob Ippolito <bob@redivi.com>
# Date 1155242007 0
#      Thu Aug 10 20:33:27 2006 +0000
# Node ID 2078acfdf5bdb4dbab1092c7e658969a490d88eb
# Parent  ba010af8a92eb563d53e19a7b7d2d9a3a91dffc3
encoder

git-svn-id: http://simplejson.googlecode.com/svn/trunk@30 a4795897-2c25-0410-b006-0d3caba88fa1

diff --git a/simplejson/encoder.py b/simplejson/encoder.py
--- a/simplejson/encoder.py
+++ b/simplejson/encoder.py
@@ -20,27 +20,27 @@
     ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
 
 def floatstr(o, allow_nan=True):
-    s = str(o)
-    # If the first non-sign is a digit then it's not a special value
-    if (o < 0.0 and s[1].isdigit()) or s[0].isdigit():
-        return s
-    elif not allow_nan:
+    # 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.
+
+    # assume this produces an infinity on all machines (probably not guaranteed)
+    INFINITY = 1e66666
+
+    if o != o:
+        text = 'NaN'
+    elif o == INFINITY:
+        text = 'Infinity'
+    elif o == -INFINITY:
+        text = '-Infinity'
+    else:
+        return str(o)
+
+    if not allow_nan:
         raise ValueError("Out of range float values are not JSON compliant: %r"
             % (o,))
-    # These are the string representations on the platforms I've tried
-    if s == 'nan':
-        return 'NaN'
-    if s == 'inf':
-        return 'Infinity'
-    if s == '-inf':
-        return '-Infinity'
-    # NaN should either be inequal to itself, or equal to everything
-    if o != o or o == 0.0:
-        return 'NaN'
-    # Last ditch effort, assume inf
-    if o < 0:
-        return '-Infinity'
-    return 'Infinity'
+
+    return text
+
 
 def encode_basestring(s):
     """