Skip to content

Floating point __str__ #6908

Closed
Closed

Description

@anntzer

Related to (but different from) #6136: float64.__str__ can truncate different floats to the same string, even when printoptions is set to a high enough precision:

In [12]: np.set_printoptions(precision=1000)

In [13]: x = np.float64(0.20000000000000004)

In [14]: x > np.float64(.2)
Out[14]: True

In [15]: str(x)
Out[15]: '0.2' # oops

Note that repr(x) correctly gives '0.20000000000000004'.

Activity

  1. gfyoung commented on Jan 12, 2016

    @gfyoung
    Contributor

    Is it me, or are the print options only respected when we want to print arrays? Here's another example of "bad" behavior with scalars:

    >>> import numpy as np
    >>> x = np.float64(0.12345678901234567890123456789)
    >>> m = np.array([x])
    >>>
    >>> str(x) # default precision is 8 decimal places
    '0.123456789012' # oops
    >>> str(m) # printing an array containing 'x' respects the precision
    '[ 0.12345679]'
    
  2. sam09 commented on Jan 16, 2016

    @sam09

    Is anyone working on this issue?
    If not i would like to give it a go

  3. gfyoung commented on Jan 16, 2016

    @gfyoung
    Contributor

    I don't know of anyone who is. Generally, IIUC the etiquette is that unless someone is really talking about it or has already opened a PR to fix the issue, it is completely up for grabs by anyone. So go for it! Good luck!

  4. anntzer commented on Jan 16, 2016

    @anntzer
    ContributorAuthor

    Please consider fixing #6136 at the same time :)

  5. sam09 commented on Jan 20, 2016

    @sam09

    It seems the printoptions are set in numpy/core/arrayprint.py. It has functions defined for printing an array but not single numpy values. Is this behavior intended? I mean is printoptions set to work only for arrays? Here is link to an answered question on stackoverflow. Please tell me if I am missing something

  6. gfyoung commented on Jan 20, 2016

    @gfyoung
    Contributor

    Judging from the labeling by @rgommers , I suspect not. I think you may very well have to add a good amount of new code in order to enforce printing for numbers.

  7. sam09 commented on Jan 25, 2016

    @sam09

    I think the float values are also printed by array2string function defined in numpy/core/arrayprint.py.
    This is where I think it happens

  8. gfyoung commented on Jan 25, 2016

    @gfyoung
    Contributor

    You still have to cast a as an array object. It's just one without a shape, so the function still does not apply to pure literals like a = 5 for example.

  9. NeilGirdhar commented on May 18, 2016

    @NeilGirdhar
    Contributor

    I just ran into this issue. printoptions should be respected by all three:

    In [21]: np.set_printoptions(precision=3)
    
    In [22]: print(np.ones(()) * (0.123456789123))
    0.123456789123
    
    In [23]: print(np.float32(0.123456789123))
    0.123457
    
    In [24]: print(np.ones((1,)) * (0.123456789123))
    [ 0.123]
    
  10. 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