{"action":"created","issue":{"url":"https://api.github.com/repos/cython/cython/issues/2728","repository_url":"https://api.github.com/repos/cython/cython","labels_url":"https://api.github.com/repos/cython/cython/issues/2728/labels{/name}","comments_url":"https://api.github.com/repos/cython/cython/issues/2728/comments","events_url":"https://api.github.com/repos/cython/cython/issues/2728/events","html_url":"https://github.com/cython/cython/issues/2728","id":383320161,"node_id":"MDU6SXNzdWUzODMzMjAxNjE=","number":2728,"title":"With default language_level and Python 3 runtime: surprising size_t division results","user":{"login":"kadler15","id":8216091,"node_id":"MDQ6VXNlcjgyMTYwOTE=","avatar_url":"https://avatars3.githubusercontent.com/u/8216091?v=4","gravatar_id":"","url":"https://api.github.com/users/kadler15","html_url":"https://github.com/kadler15","followers_url":"https://api.github.com/users/kadler15/followers","following_url":"https://api.github.com/users/kadler15/following{/other_user}","gists_url":"https://api.github.com/users/kadler15/gists{/gist_id}","starred_url":"https://api.github.com/users/kadler15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kadler15/subscriptions","organizations_url":"https://api.github.com/users/kadler15/orgs","repos_url":"https://api.github.com/users/kadler15/repos","events_url":"https://api.github.com/users/kadler15/events{/privacy}","received_events_url":"https://api.github.com/users/kadler15/received_events","type":"User","site_admin":false},"labels":[{"id":425556354,"node_id":"MDU6TGFiZWw0MjU1NTYzNTQ=","url":"https://api.github.com/repos/cython/cython/labels/R:%20worksforme","name":"R: worksforme","color":"444444","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-11-21T22:56:45Z","updated_at":"2018-11-23T21:44:01Z","closed_at":"2018-11-23T21:44:01Z","author_association":"NONE","body":"Test program:\r\n\r\n```\r\ndef run():\r\n  test_list = ['a']\r\n  test_list_len = len(test_list)\r\n  div1 = 1/(test_list_len + 1)\r\n  div2 = 1/(len(test_list) + 1)\r\n  print(div1)\r\n  print(div2)\r\n```\r\n\r\nResults of test.run() in Python 3.5.2, it's clear the results of `div1` and `div2` are not the same:\r\n```\r\n0.5\r\n0\r\n```\r\n\r\nHere is the C code generated for each line:\r\n```\r\n  /* \"test.py\":4\r\n *   test_list = ['a']\r\n *   test_list_len = len(test_list)\r\n *   div1 = 1/(test_list_len + 1)             # <<<<<<<<<<<<<<\r\n *   div2 = 1/(len(test_list) + 1)\r\n *   print(div1)\r\n */\r\n  __pyx_t_1 = __Pyx_PyInt_AddObjC(__pyx_v_test_list_len, __pyx_int_1, 1, 0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 4, __pyx_L1_error)\r\n  __Pyx_GOTREF(__pyx_t_1);\r\n  __pyx_t_3 = __Pyx_PyNumber_Divide(__pyx_int_1, __pyx_t_1); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 4, __pyx_L1_error)\r\n  __Pyx_GOTREF(__pyx_t_3);\r\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\r\n  __pyx_v_div1 = __pyx_t_3;\r\n  __pyx_t_3 = 0;\r\n```\r\n\r\n```\r\n  /* \"test.py\":5\r\n *   test_list_len = len(test_list)\r\n *   div1 = 1/(test_list_len + 1)\r\n *   div2 = 1/(len(test_list) + 1)             # <<<<<<<<<<<<<<\r\n *   print(div1)\r\n *   print(div2)\r\n */\r\n  __pyx_t_2 = PyList_GET_SIZE(__pyx_v_test_list); if (unlikely(__pyx_t_2 == ((Py_ssize_t)-1))) __PYX_ERR(0, 5, __pyx_L1_error)\r\n  __pyx_t_4 = (__pyx_t_2 + 1);\r\n  if (unlikely(__pyx_t_4 == 0)) {\r\n    PyErr_SetString(PyExc_ZeroDivisionError, \"integer division or modulo by zero\");\r\n    __PYX_ERR(0, 5, __pyx_L1_error)\r\n  }\r\n  else if (sizeof(Py_ssize_t) == sizeof(long) && (!(((Py_ssize_t)-1) > 0)) && unlikely(__pyx_t_4 == (Py_ssize_t)-1)  && unlikely(UNARY_NEG_WOULD_OVERFL$\r\n    PyErr_SetString(PyExc_OverflowError, \"value too large to perform division\");\r\n    __PYX_ERR(0, 5, __pyx_L1_error)\r\n  }\r\n  __pyx_v_div2 = __Pyx_div_Py_ssize_t(1, __pyx_t_4);\r\n```\r\n\r\nI can see that `__Pyx_PyNumber_Divide` changes based on the detected Python runtime, while `__Pyx_div_Py_ssize_t` is the same in either runtime:\r\n\r\n```\r\nstatic CYTHON_INLINE Py_ssize_t __Pyx_div_Py_ssize_t(Py_ssize_t a, Py_ssize_t b) {\r\n    Py_ssize_t q = a / b;\r\n    Py_ssize_t r = a - q*b;\r\n    q -= ((r != 0) & ((r ^ b) < 0));\r\n    return q;\r\n}\r\n```\r\n\r\nI plan to set `'language_level': 3` in my compiler directives, because this will suit my needs, but I was pretty surprised by the behavior. I expected both lines to use int division or float division, but not a mixture!"},"comment":{"url":"https://api.github.com/repos/cython/cython/issues/comments/441321326","html_url":"https://github.com/cython/cython/issues/2728#issuecomment-441321326","issue_url":"https://api.github.com/repos/cython/cython/issues/2728","id":441321326,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0MTMyMTMyNg==","user":{"login":"scoder","id":491659,"node_id":"MDQ6VXNlcjQ5MTY1OQ==","avatar_url":"https://avatars0.githubusercontent.com/u/491659?v=4","gravatar_id":"","url":"https://api.github.com/users/scoder","html_url":"https://github.com/scoder","followers_url":"https://api.github.com/users/scoder/followers","following_url":"https://api.github.com/users/scoder/following{/other_user}","gists_url":"https://api.github.com/users/scoder/gists{/gist_id}","starred_url":"https://api.github.com/users/scoder/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/scoder/subscriptions","organizations_url":"https://api.github.com/users/scoder/orgs","repos_url":"https://api.github.com/users/scoder/repos","events_url":"https://api.github.com/users/scoder/events{/privacy}","received_events_url":"https://api.github.com/users/scoder/received_events","type":"User","site_admin":false},"created_at":"2018-11-23T21:44:01Z","updated_at":"2018-11-23T21:44:01Z","author_association":"CONTRIBUTOR","body":"When I try it (with the latest master and `language_level=3`), I get a float result for `div2`, as expected. Closing as not a bug."}}
{"actor":{"display_login":"scoder"}}