473,406 Members | 2,705 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,406 software developers and data experts.

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

Jan 5 '07 #1
11 15155
re****************@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
Jan 5 '07 #2
Thomas Ploch wrote:
re****************@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
Jan 5 '07 #3
On 2007-01-05, Jonathan Smith <sm****@rpath.comwrote:
>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?!?
Jan 5 '07 #4
Jonathan Smith schrieb:
Thomas Ploch wrote:
>re****************@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
Jan 5 '07 #5
Grant Edwards schrieb:
On 2007-01-05, Jonathan Smith <sm****@rpath.comwrote:
>>>>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
Jan 5 '07 #6
On 1/5/07, Grant Edwards <gr****@visi.comwrote:
>>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
si***@brunningonline.net
Jan 5 '07 #7
"Simon Brunning" <si***@brunningonline.netwrote in message
news:ma***************************************@pyt hon.org...
On 1/5/07, Grant Edwards <gr****@visi.comwrote:
> >>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
si***@brunningonline.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
Jan 5 '07 #8

Thomas Ploch wrote:
Jonathan Smith schrieb:
Thomas Ploch wrote:
re****************@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 incompatibilities 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.

Jan 6 '07 #9
On Jan 5, 11:47 am, Thomas Ploch <Thomas.Pl...@gmx.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)

Jan 6 '07 #10
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
Jan 6 '07 #11
"Simon Brunning" <si***@brunningonline.netwrote in
news:ma***************************************@pyt hon.org:
On 1/5/07, Grant Edwards <gr****@visi.comwrote:
> >>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
Feb 4 '07 #12

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

Similar topics

5
by: Jørgen Cederberg | last post by:
Hi, using Python 2.2.1 on Windows and QNX I'm having trouble understandig why int() raises a ValueError exception upon converting strings. >>> int('-10') -10 >>> int('10') 10 >>> int(10.1)
7
by: Krista Bailie | last post by:
I'm sure this is so easy that it will hurt for anyone to read it, but I really need some direction. I'm trying to create a color chart (RGB) that shows steps between two different colors as...
4
by: Timothy Fitz | last post by:
Why are all numberical literals in exponential notation floats? To me it is counter-intuitive for 1e3 to behave so fundamentally different from 1000. Timothy Fitz
8
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need...
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
4
by: dor | last post by:
i have an input file named input.txt where all the data looks like this: (4,10) 20 (5,3) 13 (7,19) 6 .. .. .. the numbers are random. i need to use every number in each line
6
by: Johs | last post by:
I have: int a = 1; int b = 2; double c = a/b; Is it somehow possible to divide these two integers and get the result as a double 0.5? Or do they both have to be declared as doubles?
8
by: royG | last post by:
hi i am trying to resize some images.First i'd read the size as a 2 tuple and then i want to divide it by 2 or 4 or 2.5 etc.. suppose origsz=(400,300) i want to divide the origsize by 2.5 so...
9
by: tvnaidu | last post by:
I am doing floating point calculation inside C function, with debugger I can see float values in single step, if I want to print them on console, I am using printf, but printf not working for flaot,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.