floating point rounding

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

    #1

    floating point rounding

    Hi,

    Here is my issue:

    f = 1.5 * 0.01
    f
    >0.014999999999 999999
    '%f' % f
    >>'0.015000'

    But I really want to get 0.02 as a result ... is there a way out ?

    Thanks,

    hg

  • hg

    #2
    Re: floating point rounding

    hg wrote:
    Hi,
    >
    Here is my issue:
    >
    f = 1.5 * 0.01
    f
    >>0.01499999999 9999999
    '%f' % f
    >>>'0.015000'
    >
    >
    But I really want to get 0.02 as a result ... is there a way out ?
    >
    Thanks,
    >
    hg
    round

    Comment

    • John Henry

      #3
      Re: floating point rounding

      On Mar 9, 5:45 am, hg <h...@nospam.or gwrote:
      hg wrote:
      Hi,
      >
      Here is my issue:
      >
      f = 1.5 * 0.01
      f
      >0.014999999999 999999
      '%f' % f
      >>'0.015000'
      >
      But I really want to get 0.02 as a result ... is there a way out ?
      >
      Thanks,
      >
      hg
      >
      round

      Or more precisely:

      round(0.0149999 99999999999,2)

      if that's what you wish to do.

      Comment

      • hg

        #4
        Re: floating point rounding

        John Henry wrote:
        On Mar 9, 5:45 am, hg <h...@nospam.or gwrote:
        >hg wrote:
        Hi,
        >>
        Here is my issue:
        >>
        f = 1.5 * 0.01
        f
        >>0.01499999999 9999999
        '%f' % f
        >>>'0.015000'
        >>
        But I really want to get 0.02 as a result ... is there a way out ?
        >>
        Thanks,
        >>
        hg
        >>
        >round
        >
        >
        Or more precisely:
        >
        round(0.0149999 99999999999,2)
        >
        if that's what you wish to do.
        Indeed.

        hg

        Comment

        • greg

          #5
          Re: floating point rounding

          John Henry wrote:
          Or more precisely:
          >
          round(0.0149999 99999999999,2)
          No, that *won't* solve the problem. Using a slightly
          different example,
          >>x = 1.5 * 0.1
          >>x
          0.1500000000000 0002
          >>round(x, 2)
          0.1499999999999 9999

          The problem is that floats are stored internally in
          binary, not decimal, and numbers like 0.1 and 0.01
          have no exact representation as a binary float.
          Using round() doesn't help, because the result is
          still a binary float, and the result you're after
          still can't be represented.

          The best you can do is to *display* it rounded
          to the number of digits you want using formatting,
          e.g.
          >>"%.2f" % x
          '0.15'

          Alternatively, use the Decimal module, which stores
          numbers as decimal and does arithmetic in ways that
          will match your calculator. It's slower, though.

          --
          Greg

          Comment

          Working...