In python 2, the result of a division of two integers is always a integer. To force a float division, you need to use at least one float in the division:
ratio = float(inputLines)/outputLines
Be careful not to do ratio = float(inputLines/outputLines): although that results in a float, it's one obtained after doing the integer division, so the result will be "wrong" (as in "not what you expect")
In python 3, the integer division behavior was changed, and a division of two integers results in a float. You can also use this functionality in python 2.7, by putting from __future__ import division in the begging of your files.
The reason ratio = 1.0 does not work is that types (in python) are a property of values, not variables - in other words, variables don't have types.
a= 1.0 # "a" is a float
a= 1 # "a" is now a integer
Answer from loopbackbee on Stack OverflowIn python 2, the result of a division of two integers is always a integer. To force a float division, you need to use at least one float in the division:
ratio = float(inputLines)/outputLines
Be careful not to do ratio = float(inputLines/outputLines): although that results in a float, it's one obtained after doing the integer division, so the result will be "wrong" (as in "not what you expect")
In python 3, the integer division behavior was changed, and a division of two integers results in a float. You can also use this functionality in python 2.7, by putting from __future__ import division in the begging of your files.
The reason ratio = 1.0 does not work is that types (in python) are a property of values, not variables - in other words, variables don't have types.
a= 1.0 # "a" is a float
a= 1 # "a" is now a integer
Are you using Python 2.x?
Try using
>>> from __future__ import division
>>> ratio = 2/5
0.4
to get a float from a division.
Initializing with ratio = 1.0 doesn't work because with ratio = inputLines/outputLines you are re-assigning the variable to int, no matter what it was before.
How to double input number in python?
Converting large int value to float and back to int messes up the value?
convert int to double
How do I convert an int directly to a double?
What happens if you try to convert a non-numeric string to an integer in Python?
A ValueError will be raised indicating that the string is not a valid integer.
int('abc') # Raises ValueError: invalid literal for int() with base 10: 'abc'
How do you convert a list of integers to a single string in Python?
You can use the join() method combined with map() to convert each integer to a string and concatenate them.
int_list = [1, 2, 3, 4]
result = ''.join(map(str, int_list))
print(result) # Output: '1234'
Can you convert a set to a list in Python? How?
Yes, you can convert a set to a list using the list() function.
a_set = {1, 2, 3}
a_list = list(a_set)
print(a_list) # Output: [1, 2, 3]
All integers that can be represented by floating point numbers have an exact representation. So you can safely use int on the result. Inexact representations occur only if you are trying to represent a rational number with a denominator that is not a power of two.
That this works is not trivial at all! It's a property of the IEEE floating point representation that int∘floor = ⌊⋅⌋ if the magnitude of the numbers in question is small enough, but different representations are possible where int(floor(2.3)) might be 1.
To quote from Wikipedia,
Any integer with absolute value less than or equal to 224 can be exactly represented in the single precision format, and any integer with absolute value less than or equal to 253 can be exactly represented in the double precision format.
Use int(your non integer number) will nail it.
print int(2.3) # "2"
print int(math.sqrt(5)) # "2"
Say I have a large int value
>>> 7 ** 69 20500514515695490612229010908095867391439626248463723805607
Now let's say I convert it to a float value.
>>> float(7 ** 69) 2.050051451569549e+58
Seems about right. Now if I convert it back to back into an int...
>>> int(float(7 ** 69)) 20500514515695489879650927979797501063745685669370929348608
Now it's a very different number. Why did that happen?
I am sorry if it's a stupid question but this has me very intrigued.
There's no double() function in Processing like there is for float() or int(). When I try to use Number.doubleValue() from directly from java.lang, I get an error message saying it "expects no parameters." When I try to convert an int using the Double class, it simply outputs the value as 100.0 instead what it actually should be.
I'm actually trying to make double versions of both width and height
please help