473,395 Members | 1,497 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,395 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 4575
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.