When programming in Python, often we need to change the data type of a value. For example, a floating point number can be converted into an integer with the function int:
temperature = float(input("Please type in a temperature: "))
print("The temperature is", temperature)
print("...and rounded down it is", int(temperature))
Please type in a temperature: **5.15**
The temperature is 5.15
...and rounded down it is 5
Notice the function always rounds down, and not according to the rounding rules in mathematics. This is an example of a floor function.
sorry if i sound nitpicking, but this is in fact incorrect. int in python is taking the integer part, and this means it rounds towards zero, not always down.
>>> int(-0.7)
0
relevant docstring states:
Convert a number or string to an integer, or return 0 if no arguments
are given. If x is a number, return x.int(). For floating point
numbers, this truncates towards zero.
floor is a different function, that actually "rounds down":
>>> from math import floor
>>> floor(-0.3)
-1
>>> floor(-0.7)
-1
converting float/real data to integer is actually complicated and can be daunting sometimes, especially when it comes to "round" functions alluded to here (reference: https://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html). it is especially important not to leave wrong impressions in a introductory level class.
https://github.com/rage/programming-23/blob/main/data/part-2/1-programming_terminology.md?plain=1#L454 :
sorry if i sound nitpicking, but this is in fact incorrect.
int
in python is taking the integer part, and this means it rounds towards zero, not always down.relevant docstring states:
floor
is a different function, that actually "rounds down":converting float/real data to integer is actually complicated and can be daunting sometimes, especially when it comes to "round" functions alluded to here (reference: https://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html). it is especially important not to leave wrong impressions in a introductory level class.