[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Convert int to float

Guido van Brakel

3/15/2008 9:43:00 PM

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
--
7 Answers

Grant Edwards

3/15/2008 9:45:00 PM

0

On 2008-03-15, Guido van Brakel <guidovb1@invalid> wrote:
> 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

sturlamolden

3/15/2008 9:46:00 PM

0

On 15 Mar, 22:43, Guido van Brakel <guidovb1@invalid> 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



Guido van Brakel

3/15/2008 9:53:00 PM

0

Grant Edwards wrote:
> On 2008-03-15, Guido van Brakel <guidovb1@invalid> wrote:
>> 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
--

sturlamolden

3/15/2008 10:12:00 PM

0

On 15 Mar, 22:43, Guido van Brakel <guidovb1@invalid> wrote:

> > 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] )




Dan Bishop

3/15/2008 10:49:00 PM

0

On Mar 15, 4:43 pm, Guido van Brakel <guidovb1@invalid> wrote:
> 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.

Lie Ryan

3/16/2008 8:54:00 AM

0

On Mar 16, 4:43 am, Guido van Brakel <guidovb1@invalid> wrote:
> 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)").

Bryan Olson

3/17/2008 1:30:00 AM

0

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