Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that float // float returns a float #102823

Closed
mdickinson opened this issue Mar 19, 2023 · 3 comments
Closed

Document that float // float returns a float #102823

mdickinson opened this issue Mar 19, 2023 · 3 comments
Labels
docs Documentation in the Doc dir

Comments

@mdickinson
Copy link
Member

mdickinson commented Mar 19, 2023

PEP 238 states clearly that the result of x // y is a float, when x and y both have type float.

For floating point inputs, the result is a float. For example:

3.5//2.0 == 1.0

And indeed this is the behaviour that's been in place since Python 2.2. However, we seem to be missing a clear statement of this in the official docs. It would be good to fix that.

Motivated by the thread https://discuss.python.org/t/make-float-floordiv-and-rfloordiv-return-an-int/24959, where it's been asserted that this behaviour is an 'implementation detail'.

Linked PRs

@rhettinger
Copy link
Contributor

rhettinger commented Mar 19, 2023

+1

Note that Decimal(n) // Decimal(d) returns a Decimal, so it is consistent about returning the input type.

While you're at it, perhaps take a look at the docstring in numbers.py because it could be taken to mean that the built-in floor function is called and would return an int. I think the second sentence should be expressed in English rather that with Python expressions that aren't actually used. We want to communicate the concept of floor division rather that suggesting that it is literally floor(self/other) which is inaccurate.

    @abstractmethod
    def __floordiv__(self, other):
        """self // other: The floor() of self/other."""
        raise NotImplementedError

@skirpichev
Copy link
Contributor

Also note that Decimal(n) // Decimal(d) returns a float.

IOW, a Decimal.

hugovk added a commit to mdickinson/cpython that referenced this issue Sep 7, 2023
hugovk added a commit that referenced this issue Sep 7, 2023
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 7, 2023
…thonGH-102824)

(cherry picked from commit b72251d)

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 7, 2023
…thonGH-102824)

(cherry picked from commit b72251d)

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
@hugovk
Copy link
Member

hugovk commented Sep 7, 2023

Fixed via #102824 and backports to 3.11, thanks!

@hugovk hugovk closed this as completed Sep 7, 2023
hugovk added a commit that referenced this issue Sep 7, 2023
…H-102824) (#109093)

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Yhg1s pushed a commit that referenced this issue Sep 8, 2023
…H-102824) (#109092)

gh-102823: Document return type of floor division on floats (GH-102824)
(cherry picked from commit b72251d)

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

4 participants