Commit bf9f4d80 authored by Mark Dickinson's avatar Mark Dickinson

Issue 3188: accept float('infinity') as well as float('inf'). This

makes the float constructor behave in the same way as specified
by various other language standards, including C99, IEEE 754r,
and the IBM Decimal standard.
parent e78fbcce
...@@ -253,24 +253,36 @@ class ReprTestCase(unittest.TestCase): ...@@ -253,24 +253,36 @@ class ReprTestCase(unittest.TestCase):
floats_file.close() floats_file.close()
# Beginning with Python 2.6 float has cross platform compatible # Beginning with Python 2.6 float has cross platform compatible
# ways to create and representate inf and nan # ways to create and represent inf and nan
class InfNanTest(unittest.TestCase): class InfNanTest(unittest.TestCase):
def test_inf_from_str(self): def test_inf_from_str(self):
self.assert_(isinf(float("inf"))) self.assert_(isinf(float("inf")))
self.assert_(isinf(float("+inf"))) self.assert_(isinf(float("+inf")))
self.assert_(isinf(float("-inf"))) self.assert_(isinf(float("-inf")))
self.assert_(isinf(float("infinity")))
self.assert_(isinf(float("+infinity")))
self.assert_(isinf(float("-infinity")))
self.assertEqual(repr(float("inf")), "inf") self.assertEqual(repr(float("inf")), "inf")
self.assertEqual(repr(float("+inf")), "inf") self.assertEqual(repr(float("+inf")), "inf")
self.assertEqual(repr(float("-inf")), "-inf") self.assertEqual(repr(float("-inf")), "-inf")
self.assertEqual(repr(float("infinity")), "inf")
self.assertEqual(repr(float("+infinity")), "inf")
self.assertEqual(repr(float("-infinity")), "-inf")
self.assertEqual(repr(float("INF")), "inf") self.assertEqual(repr(float("INF")), "inf")
self.assertEqual(repr(float("+Inf")), "inf") self.assertEqual(repr(float("+Inf")), "inf")
self.assertEqual(repr(float("-iNF")), "-inf") self.assertEqual(repr(float("-iNF")), "-inf")
self.assertEqual(repr(float("Infinity")), "inf")
self.assertEqual(repr(float("+iNfInItY")), "inf")
self.assertEqual(repr(float("-INFINITY")), "-inf")
self.assertEqual(str(float("inf")), "inf") self.assertEqual(str(float("inf")), "inf")
self.assertEqual(str(float("+inf")), "inf") self.assertEqual(str(float("+inf")), "inf")
self.assertEqual(str(float("-inf")), "-inf") self.assertEqual(str(float("-inf")), "-inf")
self.assertEqual(str(float("infinity")), "inf")
self.assertEqual(str(float("+infinity")), "inf")
self.assertEqual(str(float("-infinity")), "-inf")
self.assertRaises(ValueError, float, "info") self.assertRaises(ValueError, float, "info")
self.assertRaises(ValueError, float, "+info") self.assertRaises(ValueError, float, "+info")
...@@ -278,6 +290,10 @@ class InfNanTest(unittest.TestCase): ...@@ -278,6 +290,10 @@ class InfNanTest(unittest.TestCase):
self.assertRaises(ValueError, float, "in") self.assertRaises(ValueError, float, "in")
self.assertRaises(ValueError, float, "+in") self.assertRaises(ValueError, float, "+in")
self.assertRaises(ValueError, float, "-in") self.assertRaises(ValueError, float, "-in")
self.assertRaises(ValueError, float, "infinit")
self.assertRaises(ValueError, float, "+Infin")
self.assertRaises(ValueError, float, "-INFI")
self.assertRaises(ValueError, float, "infinitys")
def test_inf_as_str(self): def test_inf_as_str(self):
self.assertEqual(repr(1e300 * 1e300), "inf") self.assertEqual(repr(1e300 * 1e300), "inf")
......
...@@ -246,6 +246,9 @@ PyFloat_FromString(PyObject *v, char **pend) ...@@ -246,6 +246,9 @@ PyFloat_FromString(PyObject *v, char **pend)
if (PyOS_strnicmp(p, "inf", 4) == 0) { if (PyOS_strnicmp(p, "inf", 4) == 0) {
Py_RETURN_INF(sign); Py_RETURN_INF(sign);
} }
if (PyOS_strnicmp(p, "infinity", 9) == 0) {
Py_RETURN_INF(sign);
}
#ifdef Py_NAN #ifdef Py_NAN
if(PyOS_strnicmp(p, "nan", 4) == 0) { if(PyOS_strnicmp(p, "nan", 4) == 0) {
Py_RETURN_NAN; Py_RETURN_NAN;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment