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,
Answer from Philipp on Stack OverflowAny 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.
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"
float --> int conversion
Float and int function
How to convert int to float in python? - Stack Overflow
Python: Convert float to int in numpy array
Videos
Hello, please can someone explain to me when to use the float and int functions?
I mean should I use float when I am strictly dealing with decimal numbers, or when I want to convert whole numbers to decimal numbers?
pythonlearner
To convert an integer to a float in Python you can use the following:
float_version = float(int_version)
The reason you are getting 0 is that Python 2 returns an integer if the mathematical operation (here a division) is between two integers. So while the division of 144 by 314 is 0.45~~~, Python converts this to integer and returns just the 0 by eliminating all numbers after the decimal point.
Alternatively you can convert one of the numbers in any operation to a float since an operation between a float and an integer would return a float. In your case you could write float(144)/314 or 144/float(314). Another, less generic code, is to say 144.0/314. Here 144.0 is a float so it’s the same thing.
Other than John's answer, you could also make one of the variable float, and the result will yield float.
>>> 144 / 314.0
0.4585987261146497
I created this program and was wondering that if someone were to input a decimal (ex: 1.5) for a rating, it would round down to 1 and show the correct number of starts. here's my code.
movie = input("Enter a movie: ")
stars = int(input("Rate between 1 and 5: "))
if stars == 1:
print(movie," *")
elif stars == 2:
print(movie," **")
elif stars == 3:
print(movie," ***")
elif stars == 4:
print(movie," ****")
elif stars == 5:
print(movie," *****")
if stars < 1:
print(movie," *")
elif stars > 5:
print(movie," *****")