Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Division questions
#1
I'm new to Python and also refreshing my math...
I have two questions:
- Simple division (/) gives floats even if the result can be a round integer. Why is that? And how can I get a round integer instead of a .0 float? i.e. get the result 2/2=1 instead of 2/2=1.0 I'm not talking about integer division here, because when there is a decimal part I would like to see it. I.e. show a decimal part when it is not .0 Obviously this is relevant when dividing variables where the result is not know if will have a decimal part or not.
- In integer division (//) with one negative number, why is the result rounded down? i.e. why 10//-3=-4?
Thank you!
Reply
#2
you can cast your results to integer: int(10/2)
Reply
#3
Simple division always creates a float. You can check if the float is an integer and convert back if you would like.

quotient = 10 / 5
if quotient.is_integer():
    quotient = int(quotient)
print(quotient)
Floor division always rounds down. It doesn't round toward zero if that's what you're expecting.
Reply
#4
The documentation says

Quote:The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Division of integers yields a float, while floor division of integers results in an integer; the result is that of mathematical division with the ‘floor’ function applied to the result.

So division ( / ) returns a float because that is what it is supposed to do.

The same goes for your floor division question. The documentation says 10 // -3 == floor(10 / -3).

The documentation for floor says this:

Quote:math.floor(x)
Return the floor of x, the largest integer less than or equal to x. If x is not a float, delegates to x.__floor__, which should return an Integral value.

10 / -3 = -3.3333.... floor(-3.3333...) = -4
swassilis likes this post
Reply
#5
you can use floor division with double slashes '//' which rounds down
search fro floor division here: https://docs.python.org/3/reference/expressions.html
Reply
#6
(Feb-13-2023, 11:13 PM)bowlofred Wrote: Simple division always creates a float. You can check if the float is an integer and convert back if you would like.

quotient = 10 / 5
if quotient.is_integer():
    quotient = int(quotient)
print(quotient)
That's a nice way to do it!
I'm still wondering though, why 4/2 couldn't just give an integer? Why a de facto float result was considered the best choice? After all, multiplication gives inetegers when it can... Why not division too?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Division by zero and value of argument Lawu 5 3,151 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Floor division problem with plotting x-axis tick labels Mark17 5 2,118 Apr-03-2022, 01:48 PM
Last Post: Mark17
  Division calcuation with answers to 1decimal place. sik 3 2,138 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  Floor division return value Chirumer 8 3,797 Nov-26-2020, 02:34 PM
Last Post: DeaD_EyE
  Integer division plozaq 2 1,997 Sep-28-2020, 05:49 PM
Last Post: plozaq
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 5,048 Jun-12-2020, 01:52 PM
Last Post: dgrunwal
  Division of an integer into sub-numbers Richard_SS 4 2,956 Jun-14-2019, 11:47 AM
Last Post: DeaD_EyE
  Logic of using floor division and modulus for a different variable at different time SB_J 2 2,515 Nov-01-2018, 07:25 PM
Last Post: SB_J
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 40,753 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

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