Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
poosible bug
#1
Hi,

a="1.1"
b="2.2"
print(float(a)+float(b))

on Python 3.6, this code returns 3.3000000000000003.
Is it a bag or I made a mistake?
Reply
#2
No, this is not a bug: http://0.30000000000000004.com/
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I don't think it's a bug. It's just unique to python3.6.

You could use round:

a="1.1"
b="2.2"

# Here the number of decimal places is the 2nd argument.
x=round(float(a)+float(b), 2)

print(x)
Reply
#4
(Sep-28-2018, 10:05 AM)Mr_Keystrokes Wrote: I don't think it's a bug. It's just unique to python3.6.
It's not only unique to python3.6,see @DeaD_EyE link.
Basic Answers.
Quote:Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

Python has Decimal module,then it work like excepted eg doing financial calculation.
>>> 0.4 - 0.1
0.30000000000000004

# Now it will be 0,3
>>> from decimal import Decimal
>>> Decimal('0.4') - Decimal('0.1')
Decimal('0.3')

>>> print(Decimal('0.4') - Decimal('0.1'))
0,3
Reply


Forum Jump:

User Panel Messages

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