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
  Get numpy ceil and floor value for nearest two decimals klllmmm 4 1,198 Jun-07-2023, 07:35 AM
Last Post: paul18fr
  Division questions Dionysis 5 990 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Floor approximation problem in algorithm gradlon93 3 901 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
  Division by zero and value of argument Lawu 5 2,966 Jul-01-2022, 02:28 PM
Last Post: Lawu
  math.log versus math.log10 stevendaprano 10 2,301 May-23-2022, 08:59 PM
Last Post: jefsummers
  Floor division problem with plotting x-axis tick labels Mark17 5 2,047 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Division calcuation with answers to 1decimal place. sik 3 2,091 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,704 Feb-24-2021, 05:09 PM
Last Post: bowlofred
Photo Locate Noise floor level for a spectral data in Python Ranjan_Pal 1 2,990 Dec-19-2020, 10:04 AM
Last Post: Larz60+
  Floor division return value Chirumer 8 3,696 Nov-26-2020, 02:34 PM
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