473,657 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dividing integers...Conv ert 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 15182
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.c omwrote:
>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.c omwrote:
>>>>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.co mwrote:
>>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***@brunningo nline.net
Jan 5 '07 #7
"Simon Brunning" <si***@brunning online.netwrote in message
news:ma******** *************** *************** *@python.org...
On 1/5/07, Grant Edwards <gr****@visi.co mwrote:
> >>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***@brunningo nline.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 incompatibiliti es 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...@g mx.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

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

Similar topics

5
3403
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
1809
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 entered by the user. I know the web script must convert the values into integers, store them as variables and then output them, but I cannot seem to establish the calculation for this. I'm attaching the html I've got, that works when the page is...
4
10554
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
9561
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 to find the single precision floating point value that is closest to but greater than or equal to the integer. I believe that if I simply cast to a float, it may be assigned the next higher or lower representable value, depending on...
6
6994
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')); // PRINTS NOTHING print_r(strlen((int)1)); // PRINTS '1' Now I understand that in PHP, everything scalar is a string and can
4
2560
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
72568
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
16279
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 i can resize to (160,120) scale=2.5 how can i get the newsz?
9
5110
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, how can I convert them to int and print on console using printf. I tried to print with %d, but float, but not printing, any idea?. Thanks.
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.