diff --git a/src/python2js.c b/src/python2js.c index 633f99c8a..e003e34c0 100644 --- a/src/python2js.c +++ b/src/python2js.c @@ -134,7 +134,7 @@ _python2js(PyObject* x, PyObject* map) // Since Javascript doesn't support > 32-bit ints, use floats // when the Python int gets too large. This will lose precision, // but is less problematic than truncation. - if ((unsigned long)x_long > 0x7fffffff) { + if (labs(x_long) > 0x7fffffff) { PyObject* py_float = PyNumber_Float(x); if (py_float == NULL) { return HW_ERROR; diff --git a/test/test_python.py b/test/test_python.py index 279384ab8..83ef16f2c 100644 --- a/test/test_python.py +++ b/test/test_python.py @@ -63,6 +63,8 @@ def test_python2js_long_ints(selenium): assert selenium.run('2**31') == 2**31 assert selenium.run('2**30 - 1 + 2**30') == (2**30 - 1 + 2**30) assert selenium.run('2**32 / 2**4') == (2**32 / 2**4) + assert selenium.run('-2**30') == -2**30 + assert selenium.run('-2**31') == -2**31 def test_pythonexc2js(selenium):