Skip to content

Use the shortest repr for np.float32/float64 (Trac #2051) #2643

Closed

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/2051 on 2012-02-12 by trac user beaujolais, assigned to unknown.

Python 2.7 and 3.1 now always use the shortest decimal representation for numbers that are not exactly representable by a binary float (see [http://bugs.python.org/issue1580]).

This causes some very surprising inconsistencies with NumPy's own repr:

x = 0.1
x
0.1
a = np.array([x])
a
array([ 0.1])
a[0]
0.10000000000000001
float(a[0])
0.1
type(a[0])
<class 'numpy.float64'>

For the sake of consistency I suggest adopting the same repr in NumPy.

Activity

  1. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    trac user beaujolais wrote on 2012-02-12

    Sorry for the poor formatting:

    >>> x = 0.1
    >>> x
    0.1
    >>> a = np.array([x])
    >>> a
    array([ 0.1])
    >>> a[0]
    0.10000000000000001
    >>> float(a[0])
    0.1
    >>> type(a[0])
    <class 'numpy.float64'>
    
  2. charris commented on Feb 21, 2014

    @charris
    Member

    Deciding on the needed precision is tricky. Perhaps we should take a look at how Python does it. Not done in 1.9-devel.

  3. 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions