Floating point bug?

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

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

    Mark Dickinson wrote:
    On Feb 26, 9:00 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
    >Certainly, I'd expect that if x and y are both integers and x is an
    >exact multiple of y, then x/y will be computable and not overflow.
    >But try computing 10**5000 / 10**4000 under future division (that is
    >supposed to give a float).
    >
    And smaller numbers are problematic too:
    >
    >>>from __future__ import division
    >>>10**50/10**40
    10000000000.0
    >>>10**60/10**50
    9999999999.9999 981
    >
    This despite the fact that the quotient *is* exactly representable
    You know what I find weird about this? The result of truncating
    division in this case is not just less accurate than it could be; it's
    actually greater than the result of non-truncating division.
    >>(10**60//10**50) (10**60/10**50)
    True

    Comment

    • Mark Dickinson

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

      On Feb 26, 11:55 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
      So use:  return sum(number_list ) / float(len(numbe r_list))
      That makes it somewhat more explicit what you want.  Otherwise
      But that fails for a list of Decimals...

      Mark

      Comment

      • Paul Rubin

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

        Mark Dickinson <dickinsm@gmail .comwrites:
        So use:  return sum(number_list ) / float(len(numbe r_list))
        That makes it somewhat more explicit what you want.  Otherwise
        >
        But that fails for a list of Decimals...
        Again, that depends on what your application considers to be failure.
        Heck, int/int = float instead of decimal might be a failure.

        FWIW, I just checked Haskell: int/int is not allowed (compile time
        type error). There is an integer division function `div`, like
        Python's //, . that you can use if you want an integer quotient. If
        you want a floating or rational quotient, you have to coerce the
        operands manually. Explicit is better than implicit.

        Comment

        • Paul Rubin

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

          "Gabriel Genellina" <gagsl-py2@yahoo.com.a rwrites:
          They exist since this semantic change was introduced *seven* *years*
          ago, in 2001, so it's not that suddenly the Python world is going to
          be upside down... I can't believe how long this thread is by now...
          I don't think it's a sudden uproar about int/int being float, it's
          just one of the periodic discussions about introducing a rational
          type, like we not that long ago got a decimal type.

          Comment

          • Ross Ridge

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

            Mark Dickinson <dickinsm@gmail .comwrote:
            >True division and floor division are different operations. It doesn't
            >seem ridiculous to use different operators for them.
            I don't have a problem with there being different operators for integer
            and floating-point division. I have a problem with the behviour of the
            slash (/) operator changing.

            Ross Ridge

            --
            l/ // Ross Ridge -- The Great HTMU
            [oo][oo] rridge@csclub.u waterloo.ca
            -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/
            db //

            Comment

            • Paul Rubin

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

              Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrites:
              Oh come on. With a function named "mean" that calculates the sum of a
              list of numbers and then divides by the number of items, what else could
              it be?
              You have a bunch of marbles you want to put into bins. The division
              tells you how many marbles to put into each bin. That would be
              an integer since you cannot cut up individual marbles.
              You can always imagine corner cases where some programmer, somewhere, has
              some bizarre need for a mean() function that truncates when given a list
              of integers but not when given a list of floats. Making that the default
              makes life easy for the 0.1% corner cases and life harder for the 99.9%
              of regular cases, which is far from the Python philosophy.
              I think it's more important that a program never give a wrong answer,
              than save a few keystrokes. So, that polymorphic mean function is
              a bit scary. It might be best to throw an error if the args are
              all integers. There is no definitely correct way to handle it so
              it's better to require explicit directions.

              Comment

              • Paul Rubin

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

                Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrites:
                When it comes to mixed arithmetic, it's just too darn inconvenient to
                forbid automatic conversions. Otherwise you end up either forbidding
                things like 1 + 1.0 on the basis that it isn't clear whether the
                programmer wants an int result or a float result,
                You can parse 1 as either an integer or a floating 1, so 1 + 1.0 can
                be correctly typed as a float. However (for example), len(x) is
                always an int so len(x) + 1.0 would be forbidden.
                or else even more complex rules ("if the left operator is an int,
                and the result of the addition has a zero floating-point part, then
                the result is an int,
                That is ugly and unnecessary.

                Comment

                • Paul Rubin

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

                  Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrites:
                  def pmean(data): # Paul Rubin's mean
                  """Returns the arithmetic mean of data, unless data is all
                  ints, in which case returns the mean rounded to the nearest
                  integer less than the arithmetic mean."""
                  s = sum(data)
                  if isinstance(s, int): return s//len(data)
                  else: return s/len(data)
                  Scheme and Common Lisp do automatic conversion and they thought out
                  the semantics rather carefully, and I think both of them return
                  exact rationals in this situation (int/int division). I agree
                  with you that using // as above is pretty weird and it may be
                  preferable to raise TypeError on any use of int/int (require
                  either an explicit conversion, or use of //).

                  Comment

                  • Dan Bishop

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

                    On Feb 26, 11:21 pm, Mark Dickinson <dicki...@gmail .comwrote:
                    On Feb 26, 11:55 pm, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
                    >
                    So use: return sum(number_list ) / float(len(numbe r_list))
                    That makes it somewhat more explicit what you want. Otherwise
                    >
                    But that fails for a list of Decimals...
                    >
                    Mark
                    Or complex. Or for rationals if you aren't expecting a conversion to
                    float.

                    Comment

                    • Steven D'Aprano

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

                      On Wed, 27 Feb 2008 18:43:24 -0800, Paul Rubin wrote:
                      Steven D'Aprano <steve@REMOVE-THIS-cybersource.com .auwrites:
                      >def pmean(data): # Paul Rubin's mean
                      > """Returns the arithmetic mean of data, unless data is all ints, in
                      > which case returns the mean rounded to the nearest integer less
                      > than the arithmetic mean."""
                      > s = sum(data)
                      > if isinstance(s, int): return s//len(data)
                      > else: return s/len(data)
                      >
                      Scheme and Common Lisp do automatic conversion and they thought out the
                      semantics rather carefully, and I think both of them return exact
                      rationals in this situation (int/int division).
                      Potentially better than returning a float, but equally objectionable to
                      all those insisting that int <opint should only return an int.

                      I agree with you that
                      using // as above is pretty weird and it may be preferable to raise
                      TypeError on any use of int/int (require either an explicit conversion,
                      or use of //).

                      Then you have completely misunderstood my objection.

                      It's not that // is weird, but that the semantics that you want by
                      default:

                      "return the mean for arbitrary numeric data, except for all ints, in
                      which case return the mean rounded to the next smaller integer"

                      is weird. Weird or not, if you want those semantics, Python gives you the
                      tools to create it, as above. It's not even very much more work.

                      But the normal semantics:

                      "return the mean for arbitrary numeric data"

                      should be easier, and with Python it is:

                      def mean(data): return sum(data)/len(data)

                      That does the right thing for data, no matter of what it consists of:
                      floats, ints, Decimals, rationals, complex numbers, or a mix of all of
                      the above.

                      You want the pmean() case to be easy, and the mean() case to be hard, and
                      that's what boggles my brain.



                      --
                      Steven

                      Comment

                      • Paul Rubin

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

                        Marc 'BlackJack' Rintsch <bj_666@gmx.net writes:
                        For implementing this in Python you have to carry an "is allowed to be
                        coerced to float" flag with every integer object to decide at run time if
                        it is an error to add it to a float or not.
                        Yeah, I guess it's not workable in a dynamic language. Hmm. Well I
                        could think of some crazy ways to do it.
                        Or you make Python into a statically typed language like Haskell.
                        But then it's not Python anymore IMHO.
                        There are some languages like Boo, that are sort of halfway between
                        Python and Haskell, so maybe that kind of idea could be used in them.

                        Comment

                        • Steven D'Aprano

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

                          On Thu, 28 Feb 2008 00:30:11 -0800, Dennis Lee Bieber wrote:
                          On Thu, 28 Feb 2008 01:25:32 -0000, Steven D'Aprano
                          <steve@REMOVE-THIS-cybersource.com .audeclaimed the following in
                          comp.lang.pytho n:
                          >
                          >When it comes to mixed arithmetic, it's just too darn inconvenient to
                          >forbid automatic conversions. Otherwise you end up either forbidding
                          >things like 1 + 1.0 on the basis that it isn't clear whether the
                          >programmer wants an int result or a float result, or else even more
                          >complex rules ("if the left operator is an int, and the result of the
                          >addition has a zero floating-point part, then the result is an int,
                          >otherwise it's an error, but if the left operator is a float, the
                          >result is always a float"). Or a proliferation of operators, with
                          >integer and floating point versions of everything.
                          >
                          Automatic conversions, okay... but converting a result when all
                          inputs are of one time, NO...
                          What? How does that make any sense?

                          By that logic, we should see this:
                          >>len("a string")
                          '8'
                          >>len([2, 4, 6])
                          [3]
                          >>len({'key': 'value'})
                          {1: None}


                          The only rule needed is very simple: promote simpler types to the
                          more complex type involved in the current expression (with expression
                          defined as "value operator value" -- so (1/2) * 3.0 is INTEGER 1/2,
                          resultant 0 then promoted to float 0.0 to be compatible with 3.0).
                          >
                          Very simple rule, used by very many traditional programming
                          languages.
                          And rightly rejected by many other programming languages, including
                          modern Python, not to mention calculators, real mathematics and common
                          sense.



                          --
                          Steven

                          Comment

                          • Carl Banks

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

                            On Feb 28, 3:30 am, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
                            Automatic conversions, okay... but converting a result when all
                            inputs are of one time, NO...
                            People, this is so cognitive dissonance it's not even funny.

                            There is absolutely nothing obvious about 1/2 returning a number that
                            isn't at least approximately equal to one half. There is nothing self-
                            evident about operations maintaining types.

                            You people can't tell the difference between "obvious" and "learned
                            conventions that came about because in limitations in the hardware at
                            the time". Nobody would have come up with a silly rule like "x op y
                            must always have the same type as x and y" if computer hardware had
                            been up to the task when these languages were created.

                            Very simple rule, used by very many traditional >programming languages.
                            I'd be interested in hearing what languages those are.


                            Carl Banks

                            Comment

                            • Grant Edwards

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

                              On 2008-02-28, Carl Banks <pavlovevidence @gmail.comwrote :
                              >Automatic conversions, okay... but converting a result when
                              >all inputs are of one time, NO...
                              >
                              People, this is so cognitive dissonance it's not even funny.
                              >
                              There is absolutely nothing obvious about 1/2 returning a number that
                              isn't at least approximately equal to one half.
                              I guess obviousness is in the eye of the beholder. To me it's
                              obvious that "1" and "2" are integers, and it's also obvious
                              that 2 goes into 1 zero times.
                              There is nothing self-evident about operations maintaining
                              types.
                              By that logic, there's no reason for 1 + "two" shouldn't
                              convert one operand or the other.
                              You people can't tell the difference between "obvious" and "learned
                              conventions that came about because in limitations in the hardware at
                              the time".
                              It seems to me that the expectation that 1/2 yield 0.5 is just
                              as much a convention as that it yield 0 or a true rational.

                              --
                              Grant Edwards grante Yow! I am covered with
                              at pure vegetable oil and I am
                              visi.com writing a best seller!

                              Comment

                              • Torsten Bronger

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

                                Hallöchen!

                                Grant Edwards writes:
                                [...]
                                >
                                >You people can't tell the difference between "obvious" and
                                >"learned conventions that came about because in limitations in
                                >the hardware at the time".
                                >
                                It seems to me that the expectation that 1/2 yield 0.5 is just as
                                much a convention as that it yield 0 or a true rational.
                                Should be set up a poll? Do you really think that less than 90% of
                                the voters would enter something else than 0.5 in the result edit
                                field?

                                Tschö,
                                Torsten.

                                --
                                Torsten Bronger, aquisgrana, europa vetus
                                Jabber ID: bronger@jabber. org
                                (See http://ime.webhop.org for further contact info.)

                                Comment

                                Working...