Floor Division cf. math.floor() - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Floor Division cf. math.floor() (/thread-18543.html) |
Floor Division cf. math.floor() - Devarishi - May-22-2019 Hi, A bit of confusion here: >>> 1 / 0.2 5.0That is perfect, but this floor division thing looks confusing: >>> 1 // 0.2 4.0because math.floor() returns: >>> math.floor(1 / .2) 5Why 1 // 0.2 is 4 and not 5 as there is no decimal (or floating) value returned? Thanks, Dev. RE: Floor Division cf. math.floor() - heiner55 - May-22-2019 See https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex: x // y floored quotient of x and y Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int. The result is always rounded towards minus infinity: 1//2 is 0, (-1)//2 is -1, 1//(-2) is -1, and (-1)//(-2) is 0. RE: Floor Division cf. math.floor() - Devarishi - May-22-2019 Thanks for the help! :) I did Google it but my head started to ache with other related mathematical terminologies such as minus infinity or negative infinity. :) So, when we are developing a business application, which one is more appropriate to be used? A: 1 // 0.2 4.0 B: math.floor(1 / 0.2) 5 Or should we consider any other aspect thereof? RE: Floor Division cf. math.floor() - heiner55 - May-22-2019 For business application use decimal: https://docs.python.org/3.7/library/decimal.html >>> decimal.Decimal('1') / decimal.Decimal('0.2') Decimal('5') >>> >>> decimal.Decimal('1') // decimal.Decimal('0.2') Decimal('5') |