Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rounding and floats
#1
Hi, I don't understand why 10/3 the answer is 3 and 10.0/3.0 is 3.3333? Can anyone explain this to me? Thanks!
Reply
#2
There's a python tutorial on the subject of floating point and rounding.

If you get 10/3 as 3, then that suggests you're using python 2, which has different semantics for floating point division and integer division. I suggest you start using python3 and learn the rules for that version instead.

In python2, the division result depends on the arguments (integer vs floating point). In python3, regular division can always be floating point if necessary. You use a different operator (//) to force integer results.

# python3
>>> 10/3
3.3333333333333335
>>> 10.0/3.0
3.3333333333333335
Reply
#3
I wouldn't understand why 10/3 == 3 either. I cannot find a way to make 10/3 == 3. But I cannot do that because I am running Python 3.8. If I was running Python 2.7 10/3 does == 3.

In Python pre 3, dividing an integer by an integer produced an integer. That is why 10 / 3 == 3. It cannot equal 3.33333 because 3.3333 is not an integer. 3 the integer part left over when you remove the fractional .333333 part, so the answer is 3.

To do the same trick in Python 3 you need to add extra code. int(10/3) == 3.

By the way, integer division in C or C++ works the same way.

Things change when you start using floats. A float divided by anything produces a float and dividing anything by a float produces a float. That is why 10.0 / 3.0 == 3.333333 and 10.0 / 3 == 3.3333 and 10 / 3.0 == 3.33333. In this Python 2 and Python 3 agree. When you do math operations, if any of the operands is a float the result is a float.

By the way, floating point division in C or C++ works the same way.

There is a misconception that "//" is integer division in Python 3. This is incorrect. "//" is "floor division". Floor division is similar to integer division, but the result is not necessarily an integer.
10 // 3 == 3, 3 is an integer
10.0 // 3.0 == 3.0, 3.0 is a float if dividend or divisor are floats.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Not rounding to desired decimal places? pprod 2 488 Mar-05-2021, 11:11 AM
Last Post: pprod
  Decimal Rounding error project_science 4 632 Jan-06-2021, 03:14 PM
Last Post: project_science
  Rounding to the nearest eight wallgraffiti 2 589 Jul-15-2020, 06:05 PM
Last Post: wallgraffiti
  int, floats, eval menator01 2 671 Jun-26-2020, 09:03 PM
Last Post: menator01
  Stuck comparing two floats Tizzle 7 828 Jun-26-2020, 08:23 AM
Last Post: Tizzle
  rounding question DPaul 16 1,583 Apr-12-2020, 02:30 PM
Last Post: DPaul
  price + tax rounding mlieqo 11 2,230 Sep-21-2019, 04:53 PM
Last Post: mlieqo
  rounding floats to a number of bits Skaperen 2 808 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  str.format rounding to the left of the decimal ClassicalSoul 2 962 Mar-27-2019, 11:12 AM
Last Post: perfringo
  Rounding/Truncating Numbers Trinx 3 1,188 Jan-16-2019, 10:54 PM
Last Post: Trinx

Forum Jump:

User Panel Messages

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