[issue39530] Documentation about comparisons between numeric types is misleading

New submission from Mark Dickinson <dickinsm@gmail.com>: The documentation[1] for comparisons between mixed types says:
[...] when a binary arithmetic operator has operands of different numeric types, the operand with the "narrower" type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. Comparisons between numbers of mixed type use the same rule.
That "use the same rule" part of the last sentence is misleading: it suggests that (for example) when an int is compared with a float, the int is first converted to a float, and then the two floats are compared. But that's not what actually happens: instead, the exact values of the int and float are compared. (And it's essential that equality comparisons happen that way, else equality becomes intransitive and dictionaries with numeric keys get very confused as a result.) I suggest dropping the last sentence and adding a new paragraph about comparisons between numbers of mixed type. [1] https://github.com/python/cpython/blob/master/Doc/library/stdtypes.rst#numer... ---------- assignee: docs@python components: Documentation messages: 361234 nosy: docs@python, mark.dickinson priority: normal severity: normal status: open title: Documentation about comparisons between numeric types is misleading versions: Python 3.7, Python 3.8, Python 3.9 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Mark Dickinson <dickinsm@gmail.com> added the comment: Related SO question: https://stackoverflow.com/questions/60005876/how-does-python-compare-int-to-... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Katherine Michel <kthrnmichel@gmail.com> added the comment: I'm at the sprints at PyCascades and was looking for an issue to work on and came across this and wanted to know more about it. As I was doing some research, I found the terms "narrower" and "wider" to seem ambiguous. I'm assuming "narrower" to mean that an integer has a more precise value and "wider" to mean that a float has a less precise/wider range of possible values, but unless I'm missing something, it isn't clear. I think the first paragraph should be revised to make the meaning more explicit. Happy to assist (with some input), if applicable. ---------- nosy: +Katherine Michel _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Mark Dickinson <dickinsm@gmail.com> added the comment: Hi Katherine, My report was really just about the last sentence, but I agree that the "narrower" language is confusing here. If someone says that a type "A" is narrower than type "B", that suggests to my ears that "A" represents a subset of the values of "B" (but conversely, that "B" can represent everything "A" can). That applies to the float/complex pair, or to Python 2's int/long, or to NumPy's float32/float64 or int32/int64, but not really to int/float, since int has many values that can't be represented as a float (and even has larger range than float). I suspect that the wording may have made a bit (but only a bit) more sense in Python 2, where `long` was still in the mix, and `int` was the fixed-width type, which really would have been a subset of `float` on a typical 32-bit machine. So yes, if we can find a way to ditch the "narrower" language altogether but still convey that idea of implicit conversions from int to float, and from float to complex, that would be good. And then the last sentence still needs fixing somehow. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Change by Mark Dickinson <dickinsm@gmail.com>: ---------- keywords: +patch pull_requests: +17980 stage: -> patch review pull_request: https://github.com/python/cpython/pull/18615 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Mark Dickinson <dickinsm@gmail.com> added the comment: There's a PR on GitHub. (GH-18615) Terry: I think you've looked at similar sorts of documentation clarifications in the past. Would you have any interest in reviewing this one? ---------- nosy: +terry.reedy _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- nosy: +miss-islington nosy_count: 4.0 -> 5.0 pull_requests: +18127 pull_request: https://github.com/python/cpython/pull/18737 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +18131 pull_request: https://github.com/python/cpython/pull/18736 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +18132 pull_request: https://github.com/python/cpython/pull/18773 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Change by Mark Dickinson <dickinsm@gmail.com>: ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________

Mark Dickinson <dickinsm@gmail.com> added the comment: Thank you for reviewing! ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue39530> _______________________________________
participants (3)
-
Katherine Michel
-
Mark Dickinson
-
miss-islington