Python Forum
Floor Division cf. math.floor()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Floor Division cf. math.floor()
#1
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.
Reply
#2
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.
Reply
#3
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?
Reply
#4
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Why was floor division used here? tmv 2 115 Jan-31-2026, 02:00 AM
Last Post: Pedroski55
  Division Mallard 9 891 Dec-17-2025, 03:47 PM
Last Post: Mallard
  division error nathanael 2 777 Dec-12-2025, 11:55 PM
Last Post: deanhystad
  Get numpy ceil and floor value for nearest two decimals klllmmm 4 4,896 Jun-07-2023, 07:35 AM
Last Post: paul18fr
  Division questions Dionysis 5 3,605 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Floor approximation problem in algorithm gradlon93 3 2,330 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  Division by zero and value of argument Lawu 5 10,682 Jul-01-2022, 02:28 PM
Last Post: Lawu
  math.log versus math.log10 stevendaprano 10 5,813 May-23-2022, 08:59 PM
Last Post: jefsummers
  Floor division problem with plotting x-axis tick labels Mark17 5 4,772 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Division calcuation with answers to 1decimal place. sik 3 3,557 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020