Dividing integers...Convert to float first?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • redefined.horizons@gmail.com

    #1

    Dividing integers...Convert to float first?

    I'm still pretty new to Python. I'm writing a function that accepts
    thre integers as arguments. I need to divide the first integer by te
    second integer, and get a float as a result. I don't want the caller of
    the function to have to pass floats instead of integers. How do I
    convert the arguments passed to the function into floats before I do
    the division? Is this necessary, or is their a better way?

    Thanks,

    Scott Huey

  • Thomas Ploch

    #2
    Re: Dividing integers...Conv ert to float first?

    redefined.horiz ons@gmail.com schrieb:
    I'm still pretty new to Python. I'm writing a function that accepts
    thre integers as arguments. I need to divide the first integer by te
    second integer, and get a float as a result. I don't want the caller of
    the function to have to pass floats instead of integers. How do I
    convert the arguments passed to the function into floats before I do
    the division? Is this necessary, or is their a better way?
    >
    Thanks,
    >
    Scott Huey
    >
    Yes, it is necessary. If you divide two integers, the result will be an
    integer.
    >>1/2
    0

    You need the function float() -float because a division between
    integers and floats will have floats as their results
    >>float(1)/2
    0.5

    Comment

    • Jonathan Smith

      #3
      Re: Dividing integers...Conv ert to float first?

      Thomas Ploch wrote:
      redefined.horiz ons@gmail.com schrieb:
      >I'm still pretty new to Python. I'm writing a function that accepts
      >thre integers as arguments. I need to divide the first integer by te
      >second integer, and get a float as a result. I don't want the caller of
      >the function to have to pass floats instead of integers. How do I
      >convert the arguments passed to the function into floats before I do
      >the division? Is this necessary, or is their a better way?
      >>
      >Thanks,
      >>
      >Scott Huey
      >>
      >
      Yes, it is necessary. If you divide two integers, the result will be an
      integer.
      >
      >>1/2
      0
      >
      You need the function float() -float because a division between
      integers and floats will have floats as their results
      >
      >>float(1)/2
      0.5
      >>from __future__ import division
      >>1/2
      0.5

      -smithj

      Comment

      • Grant Edwards

        #4
        Re: Dividing integers...Conv ert to float first?

        On 2007-01-05, Jonathan Smith <smithj@rpath.c omwrote:
        >from __future__ import division
        >1/2
        0.5
        $ python
        Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
        [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
        Type "help", "copyright" , "credits" or "license" for more
        information.
        >>from __future__ import LotteryNumbers
        File "<stdin>", line 1
        SyntaxError: future feature LotteryNumbers is not defined
        >>>
        Damn.

        I guess it's back to work then.

        --
        Grant Edwards grante Yow! It's a lot of fun
        at being alive... I wonder if
        visi.com my bed is made?!?

        Comment

        • Thomas Ploch

          #5
          Re: Dividing integers...Conv ert to float first?

          Jonathan Smith schrieb:
          Thomas Ploch wrote:
          >redefined.horiz ons@gmail.com schrieb:
          >>I'm still pretty new to Python. I'm writing a function that accepts
          >>thre integers as arguments. I need to divide the first integer by te
          >>second integer, and get a float as a result. I don't want the caller of
          >>the function to have to pass floats instead of integers. How do I
          >>convert the arguments passed to the function into floats before I do
          >>the division? Is this necessary, or is their a better way?
          >>>
          >>Thanks,
          >>>
          >>Scott Huey
          >>>
          >>
          >Yes, it is necessary. If you divide two integers, the result will be an
          >integer.
          >>
          > >>1/2
          > 0
          >>
          >You need the function float() -float because a division between
          >integers and floats will have floats as their results
          >>
          > >>float(1)/2
          > 0.5
          >
          >
          >>>from __future__ import division
          >>>1/2
          0.5
          >
          -smithj
          >
          aahh, I have been tought so many things about python that are actually
          so old, that I am starting to feel embarrassed.

          That brings me to the point, that learning a language X at university
          always brings you to a point where you know (almost) everything, but in
          reality know nothing because course material is too old...

          Thomas

          Comment

          • Thomas Ploch

            #6
            Re: Dividing integers...Conv ert to float first?

            Grant Edwards schrieb:
            On 2007-01-05, Jonathan Smith <smithj@rpath.c omwrote:
            >
            >>>>from __future__ import division
            >>>>1/2
            >0.5
            >
            $ python
            Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
            [GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
            Type "help", "copyright" , "credits" or "license" for more
            information.
            >>from __future__ import LotteryNumbers
            File "<stdin>", line 1
            SyntaxError: future feature LotteryNumbers is not defined
            >>>
            >
            Damn.
            >
            I guess it's back to work then.
            >
            You are working as an oracle?

            :-)

            Thomas

            Comment

            • Simon Brunning

              #7
              Re: Dividing integers...Conv ert to float first?

              On 1/5/07, Grant Edwards <grante@visi.co mwrote:
              >>from __future__ import LotteryNumbers
              File "<stdin>", line 1
              SyntaxError: future feature LotteryNumbers is not defined
              >
              Damn.
              >
              I guess it's back to work then.
              Remember the PEP 8 module name standards.
              >>from __future__ import lottery_numbers
              [1, 16, 19, 20, 21, 39]

              --
              Cheers,
              Simon B
              simon@brunningo nline.net

              Comment

              • Paul McGuire

                #8
                Re: Dividing integers...Conv ert to float first?

                "Simon Brunning" <simon@brunning online.netwrote in message
                news:mailman.23 39.1168019925.3 2031.python-list@python.org ...
                On 1/5/07, Grant Edwards <grante@visi.co mwrote:
                > >>from __future__ import LotteryNumbers
                > File "<stdin>", line 1
                > SyntaxError: future feature LotteryNumbers is not defined
                >>
                >Damn.
                >>
                >I guess it's back to work then.
                >
                Remember the PEP 8 module name standards.
                >
                >>>from __future__ import lottery_numbers
                [1, 16, 19, 20, 21, 39]
                >
                --
                Cheers,
                Simon B
                simon@brunningo nline.net

                The computer printed it out, it must be correct! We can all become
                millionaires!

                (Now if we only knew which drawing in the future, and from which lottery...)

                -- Paul


                Comment

                • Beliavsky

                  #9
                  Re: Dividing integers...Conv ert to float first?


                  Thomas Ploch wrote:
                  Jonathan Smith schrieb:
                  Thomas Ploch wrote:
                  redefined.horiz ons@gmail.com schrieb:
                  >I'm still pretty new to Python. I'm writing a function that accepts
                  >thre integers as arguments. I need to divide the first integer by te
                  >second integer, and get a float as a result. I don't want the caller of
                  >the function to have to pass floats instead of integers. How do I
                  >convert the arguments passed to the function into floats before I do
                  >the division? Is this necessary, or is their a better way?
                  >>
                  >Thanks,
                  >>
                  >Scott Huey
                  >>
                  >
                  Yes, it is necessary. If you divide two integers, the result will be an
                  integer.
                  >
                  >>1/2
                  0
                  >
                  You need the function float() -float because a division between
                  integers and floats will have floats as their results
                  >
                  >>float(1)/2
                  0.5
                  >>from __future__ import division
                  >>1/2
                  0.5

                  -smithj
                  >
                  aahh, I have been tought so many things about python that are actually
                  so old, that I am starting to feel embarrassed.
                  >
                  That brings me to the point, that learning a language X at university
                  always brings you to a point where you know (almost) everything, but in
                  reality know nothing because course material is too old...
                  If you learned C or Fortran 10 years ago, the constructs you learned
                  still have the same meaning, even though new features have been added
                  in C99 or Fortran 95. Mr. van Rossum appears to value backwards
                  compatibility less than the C or Fortran standards committees do,
                  although I am sure he is introducing incompatibiliti es only after
                  serious consideration. If the C or Fortran committees tried to change
                  the meaning of int/int, they would be shot.

                  If you want to be confident that your code will run, unchanged, 10
                  years from now on the hardware and OS that will then be common, Python
                  2.x is not the language to use, unfortunately. From what I have read,
                  Python 3 will break things more fundamental than int/int.

                  Comment

                  • Dan Bishop

                    #10
                    Re: Dividing integers...Conv ert to float first?

                    On Jan 5, 11:47 am, Thomas Ploch <Thomas.Pl...@g mx.netwrote:
                    Jonathan Smith schrieb:
                    >
                    Thomas Ploch wrote:
                    redefined.horiz ...@gmail.com schrieb:
                    >I'm still pretty new to Python. I'm writing a function that accepts
                    >thre integers as arguments. I need to divide the first integer by te
                    >second integer, and get a float as a result. I don't want the caller of
                    >the function to have to pass floats instead of integers. How do I
                    >convert the arguments passed to the function into floats before I do
                    >the division? Is this necessary, or is their a better way?
                    ....
                    >>from __future__ import division
                    >>1/2
                    0.5
                    >
                    -smithjaahh, I have been tought so many things about python that are actually
                    so old, that I am starting to feel embarrassed.
                    Don't feel embarrassed. "from __future__ import division" was added to
                    Python only five years ago, so the tutorial writers haven't had enough
                    time to mention it yet.

                    Just remember that it's a good idea to use "from __future__ import
                    division" (or better, "from __future__ import division as _division")
                    in every module, and if you really want integer division, use the //
                    operator instead of /. This will ensure that your code will continue
                    to work correctly in Python 3.0, and that you won't be bitten by subtle
                    bugs like

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

                    Comment

                    • Thomas Ploch

                      #11
                      Re: Dividing integers...Conv ert to float first?

                      Beliavsky schrieb:
                      If the C or Fortran committees tried to change
                      the meaning of int/int, they would be shot.
                      Or hanged...
                      If you want to be confident that your code will run, unchanged, 10
                      years from now on the hardware and OS that will then be common, Python
                      2.x is not the language to use, unfortunately. From what I have read,
                      Python 3 will break things more fundamental than int/int.
                      >
                      Yes, but until then we have to use python 2.x. And I think that python
                      2.x will be around quite a while after python 3000 has been released if
                      it breaks so much.

                      Thomas

                      Comment

                      • rzed

                        #12
                        Re: Dividing integers...Conv ert to float first?

                        "Simon Brunning" <simon@brunning online.netwrote in
                        news:mailman.23 39.1168019925.3 2031.python-list@python.org :
                        On 1/5/07, Grant Edwards <grante@visi.co mwrote:
                        > >>from __future__ import LotteryNumbers
                        > File "<stdin>", line 1
                        > SyntaxError: future feature LotteryNumbers is not defined
                        >>
                        >Damn.
                        >>
                        >I guess it's back to work then.
                        >
                        Remember the PEP 8 module name standards.
                        >
                        >>>from __future__ import lottery_numbers
                        [1, 16, 19, 20, 21, 39]
                        >
                        My Python version is so old that I only get three numbers. I guess
                        I'll have to upgrade.

                        --
                        rzed

                        Comment

                        Working...