Python Forum
Why is 0.1 * 0.2 arithmetically incorrect?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is 0.1 * 0.2 arithmetically incorrect?
#1
On the face of it, this seems an easy sum to do:

>>> 0.2 * 0.1
0.020000000000000004

Python is not good at arithmetic?
Reply
#2
See the python FAQ

(which will also suggest that you check out the floating point tutorial.)
Pedroski55 likes this post
Reply
#3
It's the way floating point arithmetic work, Basic Answers.
https://0.30000000000000004.com/

There is a Decimal module or other method to overcome this if needed.
>>> from decimal import Decimal
>>> 
>>> result = Decimal('0.2') * Decimal('0.1')
>>> result
Decimal('0.02')
>>> print(result)
0.02
Pedroski55 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code is returning the incorrect values. syntax error 007sonic 6 1,205 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  error 1102 (42000) incorrect database name 's' Anldra12 4 1,700 Jun-08-2022, 09:00 AM
Last Post: Anldra12
  openpyxl incorrect delete rows VladislavM 6 4,078 Jul-19-2021, 08:54 AM
Last Post: VladislavM
  Incorrect Type Error milkycow 4 2,892 Jun-25-2021, 06:04 AM
Last Post: milkycow
  user input producing incorrect boolean al_Czervik 4 3,070 Mar-05-2020, 09:50 PM
Last Post: al_Czervik
  Incorrect time format KoSik 5 3,006 Aug-15-2019, 05:10 PM
Last Post: KoSik
  Incorrect code output (File not being created) Hass 4 2,919 Jan-01-2019, 04:10 AM
Last Post: ichabod801
  Incorrect lambda vndywarhol 3 2,767 Aug-13-2018, 02:21 AM
Last Post: ichabod801
  mysql.connector - incorrect integer value Milo 3 11,195 Aug-14-2017, 06:29 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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