Skip to content

Floating point repr #6136

Closed
Closed

Description

@anntzer
In [1]: np.array(.1)[()] == np.array(.1).item()
Out[1]: True

In [2]: np.array(.1)[()] # a np.float64
Out[2]: 0.10000000000000001

In [3]: np.array(.1).item() # a python (64-bit) float
Out[3]: 0.1

Python floats use the shortest repr that give the same value when eval'd since https://bugs.python.org/issue1580 was accepted. It would be nice if numpy did the same.

I realize it would require a nontrivial patch :)

Activity

  1. njsmith commented on Aug 1, 2015

    @njsmith
    Member

    Yeah, this bugs me a bit too :-). The complication though is that when
    printing arrays, it's pretty important that the different elements take up
    the same number of columns of screen space, so any solution to this would
    need some way to address that. Maybe print the shortest equivalent
    representation and then pad to spaces if it's shorter than the allotted
    space, or only do this for scalars, or something like that.
    On Jul 31, 2015 10:51, "Antony Lee" [email protected] wrote:

    In [1]: np.array(.1)[()] == np.array(.1).item()
    Out[1]: True

    In [2]: np.array(.1)[()] # a np.float64
    Out[2]: 0.10000000000000001

    In [3]: np.array(.1).item() # a python (64-bit) float
    Out[3]: 0.1

    Python floats use the shortest repr that give the same value when eval'd
    since https://bugs.python.org/issue1580 was accepted. It would be nice if
    numpy did the same.

    I realize it would require a nontrivial patch :)

    —
    Reply to this email directly or view it on GitHub
    #6136.

  2. anntzer commented on Aug 1, 2015

    @anntzer
    ContributorAuthor

    It's already space-padded:

    In [1]: np.array([1, 1.1])
    Out[1]: array([ 1. ,  1.1])
    
  3. aarchiba commented on Aug 29, 2015

    @aarchiba
    Contributor

    Array printing passes through separate code from scalar repr. In fact, repr on arrays intentionally does not produce an invertible representation of the array (it prints according to printoptions). But this could be fixed for np.float64 without too much difficulty. np.float32 could be converted to double, which will sometimes produce more digits than necessary for inversion. np.longdouble can't go through python floats, so it would require implementing the algorithm to get a tidy representation; probably not worth it. The code would go in numpy/core/src/multiarray/scalartypes.c.src in the templated function @name@type_@kind@; the repr version would just convert to a python float and use repr.

  4. NeilGirdhar commented on May 18, 2016

    @NeilGirdhar
    Contributor

    I think it would be better to write the same kind of type-precision respecting print that Python has rather than trying to go through Python's float-printing code. It would be nice for float32 to print shorter numbers automatically.

  5. ahaldane commented on Nov 5, 2017

    @ahaldane
    Member

    Closed by #9941

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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