In the Python math library, the math. floor and math.trunc. Do the same thing?. And what's the difference? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

In the Python math library, the math. floor and math.trunc. Do the same thing?. And what's the difference?

Import math math.floor (5.9) = 5 math.trunc (5.9) = 5

2nd Mar 2019, 1:22 PM
Denis Omar
Denis Omar - avatar
2 Answers
+ 10
There wouldn't be two different methods if they did the same... trunc() gets rid of all decimal places: trunc(4.123) = 4 floor() rounds down to the next smallest integer: floor(4.123) = 4 The difference gets obvious when you use negative numbers: trunc(-4.123) = -4 floor(-4.123) = -5
2nd Mar 2019, 1:34 PM
Anna
Anna - avatar
+ 3
In a more technical way: trunc() rounds up or down towards zero. floor() rounds down towards negative infinity.
2nd Mar 2019, 1:45 PM
Diego
Diego - avatar