how to convert an integer to a float?

Y

yinglcs

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
 
J

jeff

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
 
F

Farshid Lashkari

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
 
F

Farshid Lashkari

Farshid said:
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
 
M

Matimus

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
 
B

Bjoern Schliessmann

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
 
B

Ben Finney

Please don't top-post; instead, reply below the lines you're
responding to, and trim any irrelevant lines from the original.

Subscriber123 said:
How did the new division ever get approved?!

By being introduced as a PEP, which is now numbered PEP 238.

said:
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.
 
D

Dennis Lee Bieber

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?
Being cynical: the same way you converted the arguments to
integer... use float()
def compareValue(n1, n2):
i1 = int(n1)
i2 = int(n2)
But WHY are you converting to integer in the first place.
dx = abs(i2 - i1)/min(i2, i1)

Note that, by converting to integer, you run the risk of a division
by zero

n1 = 0.2
n2 = 0.3

i1 <- 0
i2 <- 0

min(0, 0)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
A

Andrew Koenig

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.
 
A

Antoon Pardon

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)

I prefer to multiply by 1.0 That has the advantage it continues to work
if your numbers happen to be complex.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top