float / rounding question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • helen.m.flynn@gmail.com

    float / rounding question

    Hi I'm very much a beginner with Python.
    I want to write a function to convert celcius to fahrenheit like this
    one:

    def celciusToFahren heit(tc):
    tf = (9/5)*tc+32
    return tf

    I want the answer correct to one decimal place, so
    celciusToFahren heit(12) would return 53.6.

    Of course the function above returns 53.600000000000 001.

    How do I format it correctly?
  • sabatier

    #2
    Re: float / rounding question

    On Feb 25, 10:44 am, helen.m.fl...@g mail.com wrote:
    Hi I'm very much a beginner with Python.
    I want to write a function to convert celcius to fahrenheit like this
    one:
    >
    def celciusToFahren heit(tc):
    tf = (9/5)*tc+32
    return tf
    >
    I want the answer correct to one decimal place, so
    celciusToFahren heit(12) would return 53.6.
    >
    Of course the function above returns 53.600000000000 001.
    >
    How do I format it correctly?
    By the way, I tried this:

    return '%2.1f' % tf but that returns a string instead of a number.

    Any other suggestions?

    Comment

    • Jorge Godoy

      #3
      Re: float / rounding question

      sabatier wrote:
      On Feb 25, 10:44 am, helen.m.fl...@g mail.com wrote:
      >Hi I'm very much a beginner with Python.
      >I want to write a function to convert celcius to fahrenheit like this
      >one:
      >>
      >def celciusToFahren heit(tc):
      > tf = (9/5)*tc+32
      > return tf
      >>
      >I want the answer correct to one decimal place, so
      >celciusToFahre nheit(12) would return 53.6.
      >>
      >Of course the function above returns 53.600000000000 001.
      >>
      >How do I format it correctly?
      >
      By the way, I tried this:
      >
      return '%2.1f' % tf but that returns a string instead of a number.
      >
      Any other suggestions?
      But you are asking for a string on your format string above.

      And also formatting make no sense in other context since a number is a
      number and 53.600000 and 53.6 are the same number (besides precision).

      You are concerned with how numbers are represented in binary. When
      displaying the value use the format string you shown above and all will
      work.

      Comment

      • Necmettin Begiter

        #4
        Re: float / rounding question

        25 February 2008 Monday 12:44:46 tarihinde helen.m.flynn@g mail.com şunları yazmıştı:
        Hi I'm very much a beginner with Python.
        I want to write a function to convert celcius to fahrenheit like this
        one:

        def celciusToFahren heit(tc):
        tf = (9/5)*tc+32
        return tf

        I want the answer correct to one decimal place, so
        celciusToFahren heit(12) would return 53.6.

        Of course the function above returns 53.600000000000 001.

        How do I format it correctly?
        Use the round(number,di gits) function:

        tf = round((9/5)*tc+32,1)

        Comment

        • Sion Arrowsmith

          #5
          Re: float / rounding question

          Necmettin Begiter <necmettin.begi ter@gmail.comwr ote:
          >25 February 2008 Monday 12:44:46 tarihinde helen.m.flynn@g mail.com =C5=9Fun=
          >lar=C4=B1 yazm=C4=B1=C5=9 Ft=C4=B1:
          >Of course the function above returns 53.600000000000 001.
          >>=20
          >How do I format it correctly?
          >
          >Use the round(number,di gits) function:
          >
          >tf =3D round((9/5)*tc+32,1)
          >>53.6
          53.600000000000 001
          >>round(53.6, 1)
          53.600000000000 001

          --
          \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
          "Frankly I have no feelings towards penguins one way or the other"
          -- Arthur C. Clarke
          her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

          Comment

          • Mel

            #6
            Re: float / rounding question

            helen.m.flynn@g mail.com wrote:
            Hi I'm very much a beginner with Python.
            I want to write a function to convert celcius to fahrenheit like this
            one:
            >
            def celciusToFahren heit(tc):
            tf = (9/5)*tc+32
            return tf
            >
            I want the answer correct to one decimal place, so
            celciusToFahren heit(12) would return 53.6.
            >
            Of course the function above returns 53.600000000000 001.
            >
            How do I format it correctly?
            print celcisuToFahren heit (12)

            will do fine.


            Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32)
            [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
            Type "help", "copyright" , "credits" or "license" for more information.
            >>def celciusToFahren heit(tc):
            .... tf = (float(9)/5)*tc+32
            .... return tf
            ....
            >>celciusToFahr enheit (12)
            53.600000000000 001
            >>print celciusToFahren heit (12)
            53.6


            The straight value display from the interpreter pursues precision to
            the bitter end, doing its formatting with the repr function. print
            uses str formatting for a more expected result.

            Mel.

            Comment

            • Terry Reedy

              #7
              Re: float / rounding question


              <helen.m.flynn@ gmail.comwrote in message
              news:d4d9e9d6-b0e0-4063-a5b2-456bcea5a6ce@z1 7g2000hsg.googl egroups.com...
              | Hi I'm very much a beginner with Python.
              | I want to write a function to convert celcius to fahrenheit like this
              | one:
              |
              | def celciusToFahren heit(tc):
              | tf = (9/5)*tc+32
              | return tf

              Unless you are importing 'integer division' or using 3.0, that should be
              9.0/5.0.

              | I want the answer correct to one decimal place, so
              | celciusToFahren heit(12) would return 53.6.

              As written, running above on 2.x returns 44.

              tjr



              Comment

              • John Machin

                #8
                Re: float / rounding question

                On Feb 26, 7:14 am, "Terry Reedy" <tjre...@udel.e duwrote:
                <helen.m.fl...@ gmail.comwrote in message
                >
                news:d4d9e9d6-b0e0-4063-a5b2-456bcea5a6ce@z1 7g2000hsg.googl egroups.com...
                | Hi I'm very much a beginner with Python.
                | I want to write a function to convert celcius to fahrenheit like this
                | one:
                |
                | def celciusToFahren heit(tc):
                | tf = (9/5)*tc+32
                | return tf
                >
                Unless you are importing 'integer division' or using 3.0, that should be
                9.0/5.0.
                Has the syntax changed? I thought it was:
                from __future__ import division

                The OP may wish to avoid the confusion and the pointless division by
                using:
                tf = 1.8 * tc + 32

                >
                | I want the answer correct to one decimal place, so
                | celciusToFahren heit(12) would return 53.6.
                >
                As written, running above on 2.x returns 44.
                >
                tjr

                Comment

                • Mark Dickinson

                  #9
                  Re: float / rounding question

                  On Mar 7, 5:12 pm, Piet van Oostrum <p...@cs.uu.nlw rote:
                  Python just uses the C library for printing, I presume, and the conversion
                  routines in the C library are rather simplistic. It is, however, possible
                  to do better, so that 53.6 -- although internally represented as something
                  that could be described as 53.600000000000 001 -- will actually be printed
                  as 53.6.
                  There are issues with doing this portably and reliably. See



                  for a recent discussion.

                  Mark

                  Comment

                  • Steven D'Aprano

                    #10
                    Re: float / rounding question

                    On Fri, 07 Mar 2008 23:12:27 +0100, Piet van Oostrum wrote:
                    Sorry to come in so late in this discussion. Although it is correct to
                    say that many real numbers that have an exact decimal representation
                    cannot be exactly represented in binary, that is no excuse to print 53.6
                    as 53.600000000000 001. This is just lousy printing and the fact that
                    this kind of question comes up every week shows that it is confusing to
                    many people.
                    Good. That's a feature, not a bug.

                    Floats *are* confusing and unintuitive. Anyone with pretensions to be a
                    programmer should get over the illusion that floats are reals as soon as
                    possible, before they learn bad habits that have to be unlearned. If that
                    starts with them asking why they get 53.600000000000 001 instead of 53.6,
                    so be it. If they want to be insulated from the harsh reality, they can
                    do this:
                    >>print 53.6
                    53.6



                    --
                    Steven

                    Comment

                    • Mark Dickinson

                      #11
                      Re: float / rounding question

                      On Mar 7, 11:23 pm, Steven D'Aprano <st...@REMOVE-THIS-
                      cybersource.com .auwrote:
                      On Fri, 07 Mar 2008 23:12:27 +0100, Piet van Oostrum wrote:
                      Sorry to come in so late in this discussion. Although it is correct to
                      say that many real numbers that have an exact decimal representation
                      cannot be exactly represented in binary, that is no excuse to print 53.6
                      as 53.600000000000 001. This is just lousy printing and the fact that
                      this kind of question comes up every week shows that it is confusing to
                      many people.
                      >
                      Good. That's a feature, not a bug.
                      Even so, it's not clear that Python's current behaviour couldn't be
                      improved. I have a mild dislike of the lack of consistency in the
                      following, which arises from Python arbitrarily stripping trailing
                      zeros from the result returned by the C library functions:
                      >>10.1
                      10.1
                      >>10.2
                      10.199999999999 999
                      >>10.3
                      10.300000000000 001
                      >>10.4
                      10.4

                      Piet van Oostrum's suggestion gives one nice way of dealing with
                      this inconsistency. Unfortunately it doesn't look easy to
                      implement this in practice: the main difficulty seems to be
                      that to ensure float(repr(x))= =x round-tripping you'd
                      need to write routines to take control of both str -float and
                      float -str conversions, not forgetting that those routines
                      have to be reasonably fast, and work correctly on various
                      non IEEE 754 platforms as well as the usual ones. This means
                      adding, maintaining and testing hundreds of lines of
                      complicated code, where right now a few C library calls suffice.

                      Mark

                      Comment

                      • Mark Dickinson

                        #12
                        Re: float / rounding question

                        On Mar 8, 11:34 am, Mark Dickinson <dicki...@gmail .comwrote:
                        following, which arises from Python arbitrarily stripping trailing
                        zeros from the result returned by the C library functions:
                        Correction: on closer examination it's not Python doing the
                        stripping of trailing zeros; it's the C library.

                        Mark

                        Comment

                        • Roel Schroeven

                          #13
                          Re: float / rounding question

                          Mark Dickinson schreef:
                          On Mar 7, 11:23 pm, Steven D'Aprano <st...@REMOVE-THIS-
                          cybersource.com .auwrote:
                          >On Fri, 07 Mar 2008 23:12:27 +0100, Piet van Oostrum wrote:
                          >>Sorry to come in so late in this discussion. Although it is correct to
                          >>say that many real numbers that have an exact decimal representation
                          >>cannot be exactly represented in binary, that is no excuse to print 53.6
                          >>as 53.600000000000 001. This is just lousy printing and the fact that
                          >>this kind of question comes up every week shows that it is confusing to
                          >>many people.
                          >Good. That's a feature, not a bug.
                          >
                          Even so, it's not clear that Python's current behaviour couldn't be
                          improved. I have a mild dislike of the lack of consistency in the
                          following, which arises from Python arbitrarily stripping trailing
                          zeros from the result returned by the C library functions:
                          >
                          >>>10.1
                          10.1
                          >>>10.2
                          10.199999999999 999
                          >>>10.3
                          10.300000000000 001
                          >>>10.4
                          10.4
                          Actually I don't see what all the fuss is about. If you want a nicely
                          rounded number, use print or str() or the % operator.

                          *Only* when you use repr() or when the interactive interpreter uses it
                          to print the value of the expression, you get something that doesn't
                          look as nice.

                          --
                          The saddest aspect of life right now is that science gathers knowledge
                          faster than society gathers wisdom.
                          -- Isaac Asimov

                          Roel Schroeven

                          Comment

                          • Piet van Oostrum

                            #14
                            Re: float / rounding question

                            >>>>Dennis Lee Bieber <wlfraed@ix.net com.com(DLB) wrote:
                            >DLBOn Fri, 07 Mar 2008 23:12:27 +0100, Piet van Oostrum <piet@cs.uu.n l>
                            >DLBdeclaimed the following in comp.lang.pytho n:
                            >>Sorry to come in so late in this discussion. Although it is correct to say
                            >>that many real numbers that have an exact decimal representation cannot be
                            >>exactly represented in binary, that is no excuse to print 53.6 as
                            >>53.6000000000 00001. This is just lousy printing and the fact that this kind
                            >>of question comes up every week shows that it is confusing to many people.
                            >>>
                            >>>>53.6
                            >DLB53.60000000 0000001
                            >>>>print 53.6
                            >DLB53.6
                            >>>>print str(53.6)
                            >DLB53.6
                            >>>>print repr(53.6)
                            >DLB53.60000000 0000001
                            >>>>>
                            >DLB Looks like "print" already does what you expect.
                            No, what you see is not the behaviour of `print' but of `str'. `print' uses
                            `str' to do the formatting instead of `repr' whereas the interactive prompt
                            uses `str'. `str' is meant to please the human reader, and `repr' is
                            supposed to give you something that you can use as input and get exactly
                            the same value. But `str' does it by just giving you less accuracy, thus
                            sweeping the problem under the carpet:
                            >>str(53.599999 99999)
                            53.6
                            >>53.5999999999 9==53.6
                            False
                            >>repr(53.59999 999999)
                            '53.59999999998 9997'
                            >>53.5999999999 89997==53.59999 999999
                            True
                            >>repr(53.6)
                            '53.60000000000 0001'
                            >>53.6000000000 00001==53.6
                            True

                            --
                            Piet van Oostrum <piet@cs.uu.n l>
                            URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C 4]
                            Private email: piet@vanoostrum .org

                            Comment

                            Working...