Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9077227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:17:34+00:00 2026-06-16T19:17:34+00:00

Possible Duplicate: Python rounding error with float numbers python maths is wrong I can’t

  • 0

Possible Duplicate:
Python rounding error with float numbers
python maths is wrong

I can’t get Python to correctly do the subtraction 1 – 0.8 and assign it. It keeps coming up with the incorrect answer, 0.19999999999999996.

I explored a bit:

sq = {}
sub = {}
for i in range(1000):
    sq[str(i/1000.)+'**2']=((i/1000.)**2)
    sub['1-'+str(i/1000.)]=(1.0-(i/1000.))

and discovered that this error happens with a somewhat random group of the floats between 0 and 1 to the third decimal place. A similar error also occurs when you square those floats, but to a different subset.

I’m hoping for an explanation of this and how to make Python do the arithmetic right. Using round(x,3) is the work-around I’m using for now, but it’s not elegant.

Thanks!

This is a session in my Python 2.7.3 shell:

*** Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32. ***
*** Remote Python engine  is active ***
>>> 1-0.8
0.19999999999999996
>>> print 1-0.8
0.2
>>> a = 1-0.8
>>> a
0.19999999999999996
>>> print a
0.2
>>> a = 0.2
>>> print a
0.2
>>> a
0.2
>>> 

Here’s the code I put into a couple online interpreters:

def doit():
    d = {'a':1-0.8}
    return d

print doit()

and the output:

{'a': 0.19999999999999996}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:17:35+00:00Added an answer on June 16, 2026 at 7:17 pm

    Floating numbers don’t work as you’re expecting them to.

    For starters, read the floating point guide. Long story short: computers represent floating point numbers as binary, and it turns out that storing a precise decimal fraction as binary is not possible (try it for yourself on paper to see why). For practical purposes, 0.19999999999999996 is “close enough” to 0.2. If you wanted to print it as 0.2, then you could do something like:

    print "%0.1f" % floating_point_value
    

    So what you’re seeing isn’t an error. It’s expected behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python rounding error with float numbers I have a rounding Problem in
Possible Duplicate: Python list subtraction operation In Python you can concatenate lists like so:
Possible Duplicate: Python import MySQLdb error - Mac 10.6 I've tried everything I can
Possible Duplicate: Python: Get object by id Typically when you see the string representation
Possible Duplicate: [python]: path between two nodes Can anyone point me to some resources
Possible Duplicate: Python float - str - float weirdness Python float division does not
Possible Duplicate: Python nested functions variable scoping After much trial and error I have
Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: Python list subtraction operation I want to remove the common elements between
Possible Duplicate: Python code to get current function into a variable? is there a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.