Bizarre floating-point output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick Maclaren

    #1

    Bizarre floating-point output


    x = (1.234567890125 , 1.2345678901255 )
    print x
    print x[0], x[1]
    >>(1.2345678901 249999, 1.2345678901254 999)
    >>1.234567890 12 1.23456789013
    Is there a rational reason, or is that simply an artifact of the way
    that the code has evolved? It is clearly not a bug :-)


    Regards,
    Nick Maclaren.
  • Richard Brodie

    #2
    Re: Bizarre floating-point output


    "Nick Maclaren" <nmm1@cus.cam.a c.ukwrote in message
    news:enthjb$p0l $1@gemini.csx.c am.ac.uk...
    >
    x = (1.234567890125 , 1.2345678901255 )
    print x
    print x[0], x[1]
    >
    >>>(1.234567890 1249999, 1.2345678901254 999)
    >>>1.2345678901 2 1.23456789013
    >
    Is there a rational reason, or is that simply an artifact of the way
    that the code has evolved? It is clearly not a bug :-)
    print x[0] gives the same result as printing str(x[0]),
    the value of x formatted as a string (rounded to a
    sensible number of places).

    x[0] at the command prompt gives the same result as
    printing repr(x), the representation of the text value as
    a string.

    When you do print on a tuple it doesn't recursively
    call str(), so you get the repr representations .

    You can get similar results with anything where the
    str() and repr() values are different.
    e.g. x = ( u'a', u'b')


    Comment

    • Nick Maclaren

      #3
      Re: Bizarre floating-point output


      In article <entj5d$o4$1@so uth.jnrs.ja.net >,
      "Richard Brodie" <R.Brodie@rl.ac .ukwrites:
      |>
      |When you do print on a tuple it doesn't recursively
      |call str(), so you get the repr representations .

      Ah! That explains it. I would call that reason intermediate
      between rational and an artifact of the way the code has evolved!


      Regards,
      Nick Maclaren.

      Comment

      • Bjoern Schliessmann

        #4
        Re: Bizarre floating-point output

        Nick Maclaren wrote:
        Ah! That explains it. I would call that reason intermediate
        between rational and an artifact of the way the code has evolved!
        Which code has evolved? Those precision problems are inherent
        problems of the way floats are stored in memory.

        Regards,


        Björn

        --
        BOFH excuse #292:

        We ran out of dial tone and we're and waiting for the phone company
        to deliver another bottle.

        Comment

        • Nick Maclaren

          #5
          Re: Bizarre floating-point output


          In article <50f5snF1fb5uqU 2@mid.individua l.net>,
          Bjoern Schliessmann <usenet-mail-0306.20.chr0n0s s@spamgourmet.c omwrites:
          |Nick Maclaren wrote:
          |>
          | Ah! That explains it. I would call that reason intermediate
          | between rational and an artifact of the way the code has evolved!
          |>
          |Which code has evolved? Those precision problems are inherent
          |problems of the way floats are stored in memory.

          The use of different precisions for the two cases is not, however,
          and it is that I was and am referring to.


          Regards,
          Nick Maclaren.

          Comment

          • Fredrik Lundh

            #6
            Re: Bizarre floating-point output

            Nick Maclaren wrote:
            The use of different precisions for the two cases is not, however,
            and it is that I was and am referring to.
            that's by design, of course. maybe you should look "repr" up in the
            documentation ?

            </F>

            Comment

            • Nick Maclaren

              #7
              Re: Bizarre floating-point output


              In article <mailman.2425.1 168272626.32031 .python-list@python.org >, Fredrik Lundh <fredrik@python ware.comwrites:
              |Nick Maclaren wrote:
              |>
              | The use of different precisions for the two cases is not, however,
              | and it is that I was and am referring to.
              |>
              |that's by design, of course. maybe you should look "repr" up in the
              |documentation ?

              I think that you should. Where does it say that tuple's __str__ is
              the same as its __repr__?

              The obvious interpretation of the documentation is that a sequence
              type's __str__ would call __str__ on each sub-object, and its __repr__
              would call __repr__.


              Regards,
              Nick Maclaren.

              Comment

              • Bjoern Schliessmann

                #8
                Re: Bizarre floating-point output

                Nick Maclaren wrote:
                I think that you should.
                Big words.
                Where does it say that tuple's __str__ is the same as its
                __repr__?
                Where does it say that a tuple's __str__ does not call its contents'
                __repr__?
                The obvious interpretation of the documentation is that a sequence
                type's __str__ would call __str__ on each sub-object,
                Where do you read that? BTW, that makes absolutely no sense to me.
                Also, lists of Strings would quickly get messed up when displaying
                them using __str__.

                Regards,


                Björn

                --
                BOFH excuse #359:

                YOU HAVE AN I/O ERROR -Incompetent Operator error

                Comment

                • Bjoern Schliessmann

                  #9
                  Re: Bizarre floating-point output

                  Nick Maclaren wrote:
                  The use of different precisions for the two cases is not, however,
                  and it is that I was and am referring to.
                  You mistake "precision" with "display".

                  Regards,


                  Björn

                  --
                  BOFH excuse #12:

                  dry joints on cable plug

                  Comment

                  • Ziga Seilnacht

                    #10
                    Re: Bizarre floating-point output

                    Nick Maclaren wrote:
                    I think that you should. Where does it say that tuple's __str__ is
                    the same as its __repr__?
                    >
                    The obvious interpretation of the documentation is that a sequence
                    type's __str__ would call __str__ on each sub-object, and its __repr__
                    would call __repr__.
                    How would you distinguish ['3', '2', '1'] from [3, 2, 1] in that case?

                    Ziga

                    Comment

                    • Nick Maclaren

                      #11
                      Re: Bizarre floating-point output


                      In article <1168277798.302 236.184180@s34g 2000cwa.googleg roups.com>,
                      "Ziga Seilnacht" <ziga.seilnacht @gmail.comwrite s:
                      |>
                      | I think that you should. Where does it say that tuple's __str__ is
                      | the same as its __repr__?
                      |
                      | The obvious interpretation of the documentation is that a sequence
                      | type's __str__ would call __str__ on each sub-object, and its __repr__
                      | would call __repr__.
                      |>
                      |How would you distinguish ['3', '2', '1'] from [3, 2, 1] in that case?

                      Well, it's not felt necessary to distinguish those at top level, so
                      why should it be when they are in a sequence?

                      print "3", 3
                      3 3

                      But this whole thing is getting ridiculous. The current implementation
                      is a bizarre interpretation of the specification, but clearly not an
                      incorrect one. It isn't important enough to get involved in a religious
                      war over - I was merely puzzled as to the odd behaviour, because I have
                      to teach it, and it is the sort of thing that can confuse naive users.


                      Regards,
                      Nick Maclaren.

                      Comment

                      • Nick Maclaren

                        #12
                        Re: Bizarre floating-point output


                        In article <50fdf9F1fki7dU 3@mid.individua l.net>,
                        Bjoern Schliessmann <usenet-mail-0306.20.chr0n0s s@spamgourmet.c omwrites:
                        |>
                        | The use of different precisions for the two cases is not, however,
                        | and it is that I was and am referring to.
                        |>
                        |You mistake "precision" with "display".

                        Not at all. "Precision" has been used to indicate the number of digits
                        after the decimal point for at least 60 years, probably 100; in 40 years
                        of IT and using dozens of programming languages, I have never seen
                        "display" used for that purpose.


                        Regards,
                        Nick Maclaren.

                        Comment

                        • Ziga Seilnacht

                          #13
                          Re: Bizarre floating-point output

                          Nick Maclaren wrote:
                          Well, it's not felt necessary to distinguish those at top level, so
                          why should it be when they are in a sequence?
                          Well, this probably wasn't the best example, see the links below
                          for a better one.
                          But this whole thing is getting ridiculous. The current implementation
                          is a bizarre interpretation of the specification, but clearly not an
                          incorrect one. It isn't important enough to get involved in a religious
                          war over - I was merely puzzled as to the odd behaviour, because I have
                          to teach it, and it is the sort of thing that can confuse naive users.
                          There was a recent bug report identical to your complaints, which
                          was closed as invalid. The rationale for closing it was that things
                          like:

                          print ("a, bc", "de f,", "gh), i")

                          would be extremely confusing if the current behaviour was changed. See

                          for details.

                          Ziga

                          Comment

                          • Nick Maclaren

                            #14
                            Re: Bizarre floating-point output


                            In article <1168280213.382 178.228020@11g2 000cwr.googlegr oups.com>,
                            "Ziga Seilnacht" <ziga.seilnacht @gmail.comwrite s:
                            |>
                            |There was a recent bug report identical to your complaints, which
                            |was closed as invalid. The rationale for closing it was that things
                            |like:
                            |>
                            |print ("a, bc", "de f,", "gh), i")
                            |>
                            |would be extremely confusing if the current behaviour was changed. See
                            |http://www.python.org/sf/1534769
                            |for details.

                            Well, I wasn't complaining - merely querying.

                            If this approach is taken, it would be better to document it, so that
                            authors of derived classes follow the convention.


                            Regards,
                            Nick Maclaren.

                            Comment

                            • Bjoern Schliessmann

                              #15
                              Re: Bizarre floating-point output

                              Nick Maclaren wrote:
                              Not at all. "Precision" has been used to indicate the number of
                              digits after the decimal point for at least 60 years,
                              Not only, remember: Computer memories can't think in powers of ten.
                              probably 100; in 40 years of IT and using dozens of programming
                              languages, I have never seen "display" used for that purpose.
                              Yes, but since the representation in computers is based on powers of
                              two, a certain precision in the dual system, i. e. a fixed amount
                              of dual places, doesn't correspond with a fixed amount of decimal
                              places. Thus the rounding while displaying -- just to make it look
                              prettier. The very minimal additional error is silently accepted.

                              Regards,


                              Björn

                              --
                              BOFH excuse #199:

                              the curls in your keyboard cord are losing electricity.

                              Comment

                              Working...