Convert int to float

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guido van Brakel

    Convert int to float

    Hello

    I have this now:
    def gem(a):
    g = sum(a) / len(a)
    return g
    >
    print gem([1,2,3,4])
    print gem([1,10,100,1000])
    print gem([1,-2,3,-4,5])

    It now gives a int, but i would like to see floats. How can integrate
    that into the function?

    Regards,

    --
    Guido van Brakel
    Life is like a box of chocolates, you never know what you're gonna get
    --
  • Grant Edwards

    #2
    Re: Convert int to float

    On 2008-03-15, Guido van Brakel <guidovb1@inval idwrote:
    Hello
    >
    I have this now:
    >
    >def gem(a):
    > g = sum(a) / len(a)
    g = float(sum(a)) / len(a)
    > return g
    It now gives a int, but i would like to see floats. How can integrate
    that into the function?
    See above.
    Life is like a box of chocolates, you never know what you're gonna get
    sometimes it's a crunchy frog...

    --
    Grant

    Comment

    • sturlamolden

      #3
      Re: Convert int to float

      On 15 Mar, 22:43, Guido van Brakel <guidovb1@inval idwrote:
      def gem(a):
      g = sum(a) / len(a)
      return g
      It now gives a int, but i would like to see floats. How can integrate
      that into the function?
      You get an int because you are doing integer division. Cast one int to
      float.

      def gem(a):
      g = sum(a) / float(len(a))
      return g



      Comment

      • Guido van Brakel

        #4
        Re: Convert int to float

        Grant Edwards wrote:
        On 2008-03-15, Guido van Brakel <guidovb1@inval idwrote:
        >Hello
        >>
        >I have this now:
        >>
        >>def gem(a):
        >> g = sum(a) / len(a)
        >
        g = float(sum(a)) / len(a)
        >
        >> return g
        Hi,

        Thank you very much,sometimes it is so amazing simple.

        Regards

        --
        Guido van Brakel
        Life is like a box of chocolates, you never know what you're gonna get
        --

        Comment

        • sturlamolden

          #5
          Re: Convert int to float

          On 15 Mar, 22:43, Guido van Brakel <guidovb1@inval idwrote:
          def gem(a):
          g = sum(a) / len(a)
          return g
          >
          print gem([1,2,3,4])
          print gem([1,10,100,1000])
          print gem([1,-2,3,-4,5])

          gem( map(float,[1,2,3,4]) )

          gem( float(i) for i in [1,2,3,4] )




          Comment

          • Dan Bishop

            #6
            Re: Convert int to float

            On Mar 15, 4:43 pm, Guido van Brakel <guidovb1@inval idwrote:
            Hello
            >
            I have this now:
            >
            def gem(a):
            g = sum(a) / len(a)
            return g
            >
            print gem([1,2,3,4])
            print gem([1,10,100,1000])
            print gem([1,-2,3,-4,5])
            >
            It now gives a int, but i would like to see floats. How can integrate
            that into the function?
            If you add "from __future__ import division" at the top of the file,
            division will work properly.

            Comment

            • Lie

              #7
              Re: Convert int to float

              On Mar 16, 4:43 am, Guido van Brakel <guidovb1@inval idwrote:
              Hello
              >
              I have this now:
              >
              def gem(a):
                  g = sum(a) / len(a)
                  return g
              >
              print gem([1,2,3,4])
              print gem([1,10,100,1000])
              print gem([1,-2,3,-4,5])
              >
              It now gives a int, but i would like to see floats. How can integrate
              that into the function?
              >
              Regards,
              >
              --
              Guido van Brakel
              Life is like a box of chocolates, you never know what you're gonna get
              --
              Python 2's division operator's default behavior is to do integer
              division whenever all of its operands are integers/long and do float
              division if any of them are float/decimal, in Python 3, this is going
              to be changed so that division would always be float division and
              while integer division would have its own operator "//".

              You can change the default behavior of Python 2 by importing division
              behavior from __future__ module (from __future__ import division), or
              you could convert one of the operands to float ("float(a) / b" or "a /
              float(b)").

              Comment

              • Bryan Olson

                #8
                Re: Convert int to float

                sturlamolden wrote:
                Guido van Brakel wrote:
                >
                >>def gem(a):
                >> g = sum(a) / len(a)
                >> return g
                >
                >It now gives a int, but i would like to see floats. How can integrate
                >that into the function?
                >
                You get an int because you are doing integer division. Cast one int to
                float.
                >
                def gem(a):
                g = sum(a) / float(len(a))
                return g
                An alternative is to multiply by 1.0.

                def gem(a):
                g = 1.0 * sum(a) / len(a)
                return g

                The gem function is well-defined on sequences of complex numbers,
                in which case the float() method will raise a TypeError, while
                the 1.0* method will return the complex result. It may not be
                what van Brakel wants here, but it's an alternative to keep in mind.

                And I find it easier to type.

                --
                --Bryan

                Comment

                Working...