Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confusing Math
#1
I'm trying to understand why python has issues with simple math.
If you try to subtract 0.92 from 1 (1-.92) the answer you get is 0.07999999999999996 rather than 0.08
Anybody know why?
Reply
#2
It is because the number cannot be represented exactly in base 2. This page explains the issue. By the way, this is not specific to python. Every system that stores floating numbers according to the ieee-754 standard has the same limitations.
Reply
#3
More About Floating Points
If you want to be precise. Then you have to use fractions.Fraction.
from fractions import Fraction
a = Fraction(1)
b = Fraction(92, 100)
print(float(a - b))
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  math.log versus math.log10 stevendaprano 10 2,400 May-23-2022, 08:59 PM
Last Post: jefsummers
  Confusing in [for loop] topic Sherine 11 3,500 Jul-31-2021, 02:53 PM
Last Post: deanhystad
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,764 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  Confusing logic Blob 4 2,401 Nov-18-2019, 03:26 AM
Last Post: Blob
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,826 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  IndentationError message could be confusing to new programmers insearchofanswers87 1 2,342 May-16-2018, 05:05 PM
Last Post: Larz60+
  Some Confusing Program Errors (Newbie stuff) WildPictus 1 2,788 Sep-03-2017, 05:00 PM
Last Post: hbknjr
  CPU utilisation is confusing Bidgey225 6 5,179 Mar-20-2017, 01:49 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