Python Forum

Full Version: Floor Division cf. math.floor()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

A bit of confusion here:

>>> 1 / 0.2
5.0
That is perfect, but this floor division thing looks confusing:

>>> 1 // 0.2
4.0
because math.floor() returns:

>>> math.floor(1 / .2)
5
Why 1 // 0.2 is 4 and not 5 as there is no decimal (or floating) value returned?

Thanks,
Dev.
See https://docs.python.org/3/library/stdtyp...at-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.
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?
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')