Skip to content

round() returns floating point, not int, for some numpy floats when no second arg #11810

Closed
@tfawcett

Description

@tfawcett

The semantics of round() changed in Python 3:

round(number[, ndigits])
Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

This works incorrectly in the case of np.float64, which returns a float. I believe the __round__ method is calling __rint__, which should return an integer but doesn't.

Reproducing code example:

import numpy as np
In [50]: round(1.0)
Out[50]: 1

In [51]: round(float(1.0))
Out[51]: 1

In [52]: round(np.float(1.0))
Out[52]: 1

In [53]: round(np.float64(1.0))
Out[53]: 1.0

This behavior is the same for float16, float32, and float128.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions