473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
--
Mar 15 '08 #1
7 4571
On 2008-03-15, Guido van Brakel <guidovb1@invalidwrote:
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

Mar 15 '08 #2
On 15 Mar, 22:43, Guido van Brakel <guidovb1@invalidwrote:
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

Mar 15 '08 #3
Grant Edwards wrote:
On 2008-03-15, Guido van Brakel <guidovb1@invalidwrote:
>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
--
Mar 15 '08 #4
On 15 Mar, 22:43, Guido van Brakel <guidovb1@invalidwrote:
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] )


Mar 15 '08 #5
On Mar 15, 4:43 pm, Guido van Brakel <guidovb1@invalidwrote:
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.
Mar 15 '08 #6
Lie
On Mar 16, 4:43*am, Guido van Brakel <guidovb1@invalidwrote:
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)").
Mar 16 '08 #7
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
Mar 17 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Cally | last post by:
Hello, I would like to convert a field from ntext field found in one database table to float field found in another database table. The reason why I want to do this is a long one. I have...
4
by: jaijai_kumar | last post by:
Select Cast('100.1234' as float) give me the result 100.1234 Now when I convert it back to char I want exactly 100.1234 Select Convert(char(100),Cast('100.1234' as float)) Gives me 100.123 (Here...
9
by: GRoll21 | last post by:
I have a program here that asks the number of students surveyed. then it will ask how many movies each student has watched. After thats been collected it does functions to find the average, median,...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
2
by: Chi Tang | last post by:
Hi, I try to convert a string to a float but it alway comes out with extra value. For example, the string input is '12.6' but the output is '12.6000003814697' The following is my code to do...
2
by: Alberto | last post by:
why there isn't a Convert.ToFloat() method in the Convert class? Sometimes I can't work with a float field because there isn't this method and I need it to, for example, convert the input in a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
2
by: trondhuso | last post by:
Hi group, I've found some code that I want to use in a project that I am working on, but the code is for c# .net and not 2003 or 2005 that I have available. In this code the program yells on...
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.