473,606 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to convert an integer to a float?

Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
i1)' always return 0, can you please tell me how can i convert it from
an integer to float?
def compareValue(n1 , n2):
i1 = int(n1)
i2 = int(n2)

dx = abs(i2 - i1)/min(i2, i1)
print dx
return dx < 0.05

Feb 28 '07 #1
10 107654
On Feb 27, 7:05 pm, "ying...@gmail. com" <ying...@gmail. comwrote:
Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
i1)' always return 0, can you please tell me how can i convert it from
an integer to float?

def compareValue(n1 , n2):
i1 = int(n1)
i2 = int(n2)

dx = abs(i2 - i1)/min(i2, i1)
print dx
return dx < 0.05
x = x + 0.0

Feb 28 '07 #2
yi*****@gmail.c om wrote:
Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
i1)' always return 0, can you please tell me how can i convert it from
an integer to float?
When two integers are involved in a division, the result will also be a
division. If one of the operands is a float, then the result will be a
float instead. So try casting one of the values to a float before
performing the division:

dx = float(abs(i2 - i1))/min(i2, i1)

-Farshid
Feb 28 '07 #3
Farshid Lashkari wrote:
When two integers are involved in a division, the result will also be a
division.
My bad, I meant the result will also be an *integer*

-Farshid
Feb 28 '07 #4
yinglcs, you can use float() or the new division:
>>1 / 2
0
>>1 / float(2)
0.5
>>from __future__ import division
1 / 2
0.5

Bye,
bearophile

Feb 28 '07 #5
On Feb 27, 4:05 pm, "ying...@gmail. com" <ying...@gmail. comwrote:
Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
i1)' always return 0, can you please tell me how can i convert it from
an integer to float?

def compareValue(n1 , n2):
i1 = int(n1)
i2 = int(n2)

dx = abs(i2 - i1)/min(i2, i1)
print dx
return dx < 0.05
dx = float(abs(i2 -i1))/min(i2, i1)

Or you could just put "from __future__ import division" at the top of
your file.

see http://www.python.org/dev/peps/pep-0238/ for details.

-Matt

Feb 28 '07 #6
yi*****@gmail.c om wrote:
def compareValue(n1 , n2):
i1 = int(n1)
i2 = int(n2)

dx = abs(i2 - i1)/min(i2, i1)
print dx
return dx < 0.05
You could also prepend

from __future__ import division

Regards,
Björn

--
BOFH excuse #237:

Plate voltage too low on demodulator tube

Feb 28 '07 #7
On 2007-02-28, jeff <je************ ****@gmail.comw rote:
On Feb 27, 7:05 pm, "ying...@gmail. com" <ying...@gmail. comwrote:
>Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
i1)' always return 0, can you please tell me how can i convert it from
an integer to float?

def compareValue(n1 , n2):
i1 = int(n1)
i2 = int(n2)

dx = abs(i2 - i1)/min(i2, i1)
print dx
return dx < 0.05

x = x + 0.0
How, um, perlesque.

I rather think that this is a bit more pythonic:

x = float(x)

--
Grant Edwards grante Yow! I'm a fuschia bowling
at ball somewhere in Brittany
visi.com
Feb 28 '07 #8
Please don't top-post; instead, reply below the lines you're
responding to, and trim any irrelevant lines from the original.

Subscriber123 <su***********@ gmail.comwrites :
How did the new division ever get approved?!
By being introduced as a PEP, which is now numbered PEP 238.

<URL:http://www.python.org/dev/peps/pep-0238/>
That's not pythonic! What if you then need to divide two integers
and find an element in a list or dict?
Then your code is dependent on ambiguous behaviour which has changed
in newer Python versions.

As described in the above document, the '//' operator will
unambiguously request floor division, even in older versions of
Python.
I know that at the moment it is not implemented unless imported from
__future__, but I expect that it eventually might be.
Please read PEP 238 to see the implementation plan.
That would be a problem with backwards compatibility.
The older Python versions aren't going away. Anyone who wants their
old code to work with new versions of Python has a responsibility to
see what parts of their code need to be updated.

--
\ "My roommate got a pet elephant. Then it got lost. It's in the |
`\ apartment somewhere." -- Steven Wright |
_o__) |
Ben Finney

Feb 28 '07 #9
<yi*****@gmail. comwrote in message
news:11******** **************@ k78g2000cwa.goo glegroups.com.. .
Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2,
i1)' always return 0, can you please tell me how can i convert it from
an integer to float?
I don't think that's what you really want to do.

What you really want is for dx to be a float rather than being truncated to
an integer. Division is going to behave that way in the future, so if you
want it to behave that way now, you can write

from __future__ import division

at the beginning of your program.

If for some reason you don't want to rely on using a version of Python that
knows about this future behavior, you might consider

dx = float(abs(i2 - i1))/min(i2, i1)

as an alternative.
Mar 5 '07 #10

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

Similar topics

2
13693
by: respect | last post by:
i want to convert real float number to 32-bit IEEE binary format in C++ ? plz help me in finding the codes as fast as u can ] Example for input -6.5, the output should be. 1 10000001 101000000000000000000000 0 1 8 9 31 S = 1 E = 129 F = 1.101
7
13869
by: henrytcy | last post by:
Hi, How can I convert integer, for example 12113, to char array but without specify an array size in C programming? Thanks!
2
15193
karthickbabu
by: karthickbabu | last post by:
Hi In my application i want to convert integer to decimal. I get a input and using convert function to convert into decimal. But it shows as it self. My code like as below, Is any wrong in my code dim input as long // I declare as long dim decimaloutput as decimal decimaloutput = Convert.ToDecimal(input) label1.text = decimaloutput.ToString()
4
2253
by: mthread | last post by:
Hi, I would like to know the method to convert integer to string. Thanx in advance.
4
2514
by: wangers16 | last post by:
Is it possible to get JavaScript to check the end of the a integer/float variable for a certain number e.g. 5, if so how do you do it? Thanks in advance
6
10601
by: ngajay | last post by:
Please tell me... how to convert integer to hex in little endian format ? example : i'm input int = 9995 then the output in Array String is
2
2431
by: pantherxin | last post by:
Writing program to calculate cir., area, etc. of circle. Radius is input as integer; how to convert integer to proceed with calculations?
7
4212
by: Udara chathuranga | last post by:
How can I convert binary float number into a decimal float number ?
3
2060
by: Daniel DePasqua | last post by:
I do not understand the concept of inputing a number and having the program convert it to an integer, float and string. How would you write a program that does that
0
8045
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8467
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
8320
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
6803
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...
0
5470
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
3952
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...
0
4011
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1315
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.