Skip to content

Rounding rational numbers: default behaviour is not up-to-date with Python #35473

@vneiger

Description

@vneiger

Is there an existing issue for this?

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.

Problem Description

The documentation for RationalField's round method says

        Return the nearest integer to ``self``, rounding away from 0 by
        default, for consistency with the builtin Python round.

Yet the builtin Python round currently (since Python3 I think) rounds to nearest, with halfway case rounding to even. The first example in the documentation is (9/2).round() which yields 5, and consequently in Sage round(9/2) yields 5 as well; whereas in Python the same round(9/2) yields 4.

Proposed Solution

A simple solution may be to change the default to even, which is the current Python convention and is already implemented in this method round for rational field.

Alternatives Considered

Or, seemingly less ideal: one could keep the current behaviour and modify the documentation to highlight that this diverges from Python's builtin behaviour.

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions