Floating point bug?

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

    #16
    Re: Floating point bug?

    On Feb 15, 3:30 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
    The point is that all numbering systems with a base + precision will
    have (rational) values they can't exactly represent. Q\R is of course
    out of the question by definition....
    This 'Decimal is exact' myth has been appearing often enough that I
    wonder whether it's worth devoting a prominent paragraph to in the
    docs.

    Mark

    Comment

    • Lie

      #17
      How about adding rational fraction to Python?

      Would all these problems with floating points be a rational reason to
      add rational numbers support in Python or Py3k? (pun not intended)

      I agree, there are some numbers that is rationals can't represent
      (like pi, phi, e) but these rounding problems also exist in floating
      points, and rational numbers wouldn't be so easily fooled by something
      like 1 / 3 * 3, and 1/10 (computer) is exactly 0.1 (human). The first
      problem with rational is that to get an infinite precision rational,
      the language would have to have an infinite length integers, which
      Python have given us. The second problem with rationals is to keep
      rationals in its most simple, trivial form. This can be solved with a
      good GCD algorithm, which can also be a nice addition to Python's math
      library.

      Comment

      • Jeff Schwab

        #18
        Re: How about adding rational fraction to Python?

        Lie wrote:
        Would all these problems with floating points be a rational reason to
        add rational numbers support in Python or Py3k? (pun not intended)
        >
        I agree, there are some numbers that is rationals can't represent
        (like pi, phi, e) but these rounding problems also exist in floating
        points, and rational numbers wouldn't be so easily fooled by something
        like 1 / 3 * 3, and 1/10 (computer) is exactly 0.1 (human). The first
        problem with rational is that to get an infinite precision rational,
        the language would have to have an infinite length integers, which
        Python have given us. The second problem with rationals is to keep
        rationals in its most simple, trivial form. This can be solved with a
        good GCD algorithm, which can also be a nice addition to Python's math
        library.
        Python has no numeric type with the semantics of an unboundedly precise rational number. This proposal explains the semantics of such a type, and suggests builtin functions and literals to support such a type. This PEP suggests no literals for rationa...

        Comment

        • Lie

          #19
          Re: How about adding rational fraction to Python?

          On Feb 17, 1:40 am, Jeff Schwab <j...@schwabcen ter.comwrote:
          Lie wrote:
          Would all these problems with floating points be a rational reason to
          add rational numbers support in Python or Py3k? (pun not intended)
          >
          I agree, there are some numbers that is rationals can't represent
          (like pi, phi, e) but these rounding problems also exist in floating
          points, and rational numbers wouldn't be so easily fooled by something
          like 1 / 3 * 3, and 1/10 (computer) is exactly 0.1 (human). The first
          problem with rational is that to get an infinite precision rational,
          the language would have to have an infinite length integers, which
          Python have given us. The second problem with rationals is to keep
          rationals in its most simple, trivial form. This can be solved with a
          good GCD algorithm, which can also be a nice addition to Python's math
          library.
          >
          http://www.python.org/dev/peps/pep-0239/
          Yes, I'm aware of the PEP and actually have been trying for some time
          to reopen the PEP.

          The reason that PEP is rejected is because Decimal is accepted, which
          I think is a completely absurd reason as Decimal doesn't actually
          solve the rounding problems and equality comparison problem. Some
          people have also pointed out that Decimal IS Inexact, while a rational
          number is always exact except if you have an operation with a (binary
          or decimal) floating point involved (this can be easilty resolved by
          making fraction recessive, i.e. an operation that receive a fraction
          and a float should return a float).

          Comment

          • Mark Dickinson

            #20
            Re: How about adding rational fraction to Python?

            On Feb 16, 1:35 pm, Lie <Lie.1...@gmail .comwrote:
            Would all these problems with floating points be a rational reason to
            add rational numbers support in Python or Py3k? (pun not intended)
            It's already in the trunk! Python will have a rational type (called
            Fraction) in Python 2.6 and Python 3.0, thanks largely to the work of
            Jeffrey Yaskin.

            Mark

            Comment

            • Mark Dickinson

              #21
              Re: How about adding rational fraction to Python?

              On Feb 16, 1:35 pm, Lie <Lie.1...@gmail .comwrote:
              Would all these problems with floating points be a rational reason to
              add rational numbers support in Python or Py3k? (pun not intended)
              Forgot to give the link:

              Source code: Lib/fractions.py The fractions module provides support for rational number arithmetic. A Fraction instance can be constructed from a pair of rational numbers, from a single number, or ...


              Mark

              Comment

              • mensanator@aol.com

                #22
                Re: How about adding rational fraction to Python?

                On Feb 16, 12:35�pm, Lie <Lie.1...@gmail .comwrote:
                Would all these problems with floating points be a rational reason to
                add rational numbers support in Python or Py3k? (pun not intended)
                >
                I agree, there are some numbers that is rationals can't represent
                (like pi, phi, e) but these rounding problems also exist in floating
                points, and rational numbers wouldn't be so easily fooled by something
                like 1 / 3 * 3, and 1/10 (computer) is exactly 0.1 (human). The first
                problem with rational is that to get an infinite precision rational,
                the language would have to have an infinite length integers, which
                Python have given us. The second problem with rationals is to keep
                rationals in its most simple, trivial form. This can be solved with a
                good GCD algorithm, which can also be a nice addition to Python's math
                library.
                Have you looked at the gmpy madule? That's what I
                use whenever this comes up. Works very nice to
                eliminate the issues that prevent a float olution
                for the problems I'm working on.

                And some irrationals can be represented by infite
                sequences of rationals that, coupled with gmpy's
                unlimited precision floats, allows any number of
                accurate decimal places to be calculated.

                If you would like to see an example, check out

                Discover the latest breaking news in the U.S. and around the world — politics, weather, entertainment, lifestyle, finance, sports and much more.

                Comment

                • Carl Banks

                  #23
                  Re: How about adding rational fraction to Python?

                  On Feb 16, 3:03 pm, Lie <Lie.1...@gmail .comwrote:
                  Although rationals have its limitations too, it is a much
                  better choice compared to floats/Decimals for most cases.
                  Maybe that's true for your use cases, but it's not true for most cases
                  in general.

                  Rationals are pretty useless for almost any extended calculations,
                  since the denominator tends to grow in size till it's practically
                  unusbale, which means you have to periodically do non-exact reductions
                  to keep things running, and if you do that you might as well be using
                  floating point.

                  Rationals have their occasional special purpose uses, but for most
                  cases they're at best marginally better then floats and more often
                  incomparably worse.


                  Carl Banks

                  Comment

                  • Lie

                    #24
                    Re: How about adding rational fraction to Python?

                    On Feb 17, 4:25 am, Carl Banks <pavlovevide... @gmail.comwrote :
                    On Feb 16, 3:03 pm, Lie <Lie.1...@gmail .comwrote:
                    >
                    Although rationals have its limitations too, it is a much
                    better choice compared to floats/Decimals for most cases.
                    >
                    Maybe that's true for your use cases, but it's not true for most cases
                    in general.
                    OK, that might have been an overstatement, but as I see it, it is
                    safer to handle something in a Fraction compared to floats (which is
                    why I uses fractions whenever possible in non-computer maths).
                    Rationals are pretty useless for almost any extended calculations,
                    since the denominator tends to grow in size till it's practically
                    unusbale, which means you have to periodically do non-exact reductions
                    to keep things running, and if you do that you might as well be using
                    floating point.
                    Rationals aren't that good if the same piece of variable is to be
                    calculated again and again because of its growth, but there are a lot
                    of cases where the data would only require five or six or so
                    operations done on it (and there are thousands or millions of such
                    datas), rationals is the perfect choice for those situations because
                    it is easier to use thanks to the comparison safety. Or in the
                    situations where speed isn't as important and accuracy is required,
                    Fraction may be faster than decimal and more accurate at the same time
                    (someone need to test that though).
                    Rationals have their occasional special purpose uses, but for most
                    cases they're at best marginally better then floats and more often
                    incomparably worse.

                    Comment

                    • Jeff Schwab

                      #25
                      Re: How about adding rational fraction to Python?

                      Carl Banks wrote:
                      On Feb 16, 3:03 pm, Lie <Lie.1...@gmail .comwrote:
                      >Although rationals have its limitations too, it is a much
                      >better choice compared to floats/Decimals for most cases.
                      >
                      Maybe that's true for your use cases, but it's not true for most cases
                      in general.
                      >
                      Rationals are pretty useless for almost any extended calculations,
                      since the denominator tends to grow in size till it's practically
                      unusbale,
                      What do you mean by "practicall y unusable?" I heard similar arguments
                      made against big integers at one point ("Primitive types are usually big
                      enough, why risk performance?") but I fell in love with them when I
                      first saw them in Smalltalk, and I'm glad Python supports them natively.

                      Comment

                      • Carl Banks

                        #26
                        Re: How about adding rational fraction to Python?

                        On Feb 16, 5:51 pm, Jeff Schwab <j...@schwabcen ter.comwrote:
                        Carl Banks wrote:
                        On Feb 16, 3:03 pm, Lie <Lie.1...@gmail .comwrote:
                        Although rationals have its limitations too, it is a much
                        better choice compared to floats/Decimals for most cases.
                        >
                        Maybe that's true for your use cases, but it's not true for most cases
                        in general.
                        >
                        Rationals are pretty useless for almost any extended calculations,
                        since the denominator tends to grow in size till it's practically
                        unusbale,
                        >
                        What do you mean by "practicall y unusable?" I heard similar arguments
                        made against big integers at one point ("Primitive types are usually big
                        enough, why risk performance?") but I fell in love with them when I
                        first saw them in Smalltalk, and I'm glad Python supports them natively.

                        Comment

                        • Dan Bishop

                          #27
                          Re: Floating point bug?

                          On Feb 14, 8:10 pm, Zentrader <zentrad...@gma il.comwrote:
                          That's a misconception. The decimal-module has a different base (10
                          instead of 2), and higher precision. But that doesn't change the fact
                          that it will expose the same rounding-errors as floats do - just for
                          different numbers.
                          >
                          >>import decimal as d
                          >>d = d.Decimal
                          >>d("1") / d("3") * d("3")
                          Decimal("0.9999 999999999999999 999999999")
                          >
                          Surely you jest.
                          He's not joking at all.
                          Your example is exact to 28 digits. Your attempted
                          trick is to use a number that never ends (1/3=0.3333...).
                          It does end in base 3, 6, 9, 12, etc.

                          You have to remember that base-ten wasn't chosen because it has
                          mathematical advantages over other bases, but merely because people
                          counted on their fingers. In light of this fact, why is one-fifth
                          more deserving of an exact representation than one-third is?

                          Comment

                          • Carl Banks

                            #28
                            Re: How about adding rational fraction to Python?

                            On Feb 17, 1:45 pm, Lie <Lie.1...@gmail .comwrote:
                            Any iteration with repeated divisions and additions can thus run the
                            denominators up. This sort of calculation is pretty common (examples:
                            compound interest, numerical integration).
                            >
                            Wrong. Addition and subtraction would only grow the denominator up to
                            a certain limit
                            I said repeated additions and divisions.

                            Anyways, addition and subtraction can increase the denominator a lot
                            if for some reason you are inputing numbers with many different
                            denominators.


                            Carl Banks

                            Comment

                            • Lie

                              #29
                              Re: How about adding rational fraction to Python?

                              On Feb 18, 1:25 pm, Carl Banks <pavlovevide... @gmail.comwrote :
                              On Feb 17, 1:45 pm, Lie <Lie.1...@gmail .comwrote:
                              >
                              Any iteration with repeated divisions and additions can thus run the
                              denominators up.  This sort of calculation is pretty common (examples:
                              compound interest, numerical integration).
                              >
                              Wrong. Addition and subtraction would only grow the denominator up to
                              a certain limit
                              >
                              I said repeated additions and divisions.
                              Repeated Addition and subtraction can't make fractions grow
                              infinitely, only multiplication and division could.
                              Anyways, addition and subtraction can increase the denominator a lot
                              if for some reason you are inputing numbers with many different
                              denominators.
                              Up to a certain limit. After you reached the limit, the fraction would
                              always be simplifyable.

                              If the input numerator and denominator have a defined limit, repeated
                              addition and subtraction to another fraction will also have a defined
                              limit.

                              Comment

                              • casevh

                                #30
                                Re: How about adding rational fraction to Python?

                                >
                                Out of curiosity, of what use is denominator limits?
                                >
                                The problems where I've had to use rationals have
                                never afforded me such luxury, so I don't see what
                                your point is
                                In Donald Knuth's The Art of Computer Programming, he described
                                floating slash arithmetic where the total number of bits by the
                                numerator and denominator was bounded. IIRC, a use case was matrix
                                inversion.

                                casevh

                                Comment

                                Working...