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

  • SEARCH
  • Home
  • 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 6713613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:24:04+00:00 2026-05-26T08:24:04+00:00

I came across a strange behavior in Python (2.6.1) dictionaries: The code I have

  • 0

I came across a strange behavior in Python (2.6.1) dictionaries:

The code I have is:

new_item = {'val': 1.4}
print new_item['val']
print new_item

And the result is:

1.4
{'val': 1.3999999999999999}

Why is this? It happens with some numbers, but not others. For example:

  • 0.1 becomes 0.1000…001
  • 0.4 becomes 0.4000…002
  • 0.7 becomes 0.6999…996
  • 1.9 becomes 1.8888…889
  • 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-05-26T08:24:04+00:00Added an answer on May 26, 2026 at 8:24 am

    This is not Python-specific, the issue appears with every language that uses binary floating point (which is pretty much every mainstream language).

    From the Floating-Point Guide:

    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.

    When the code is compiled or interpreted, your “0.1” is already
    rounded to the nearest number in that format, which results in a small
    rounding error even before the calculation happens.

    Some values can be exactly represented as binary fraction, and output formatting routines will often display the shortest number that is closer to the actual value than to any other floating-point number, which masks some of the rounding errors.

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

Sidebar

Related Questions

While refactoring some code, I came across this strange compile error: The constructor call
I came across this strange code snippet which compiles fine: class Car { public:
I came across strange behavior in Visual Studio 2010 C++ compiler. Following code compiles
I came across on a strange behavior of the following code, while playing around
I came across this strange looking declaration in some code early this morning (before
I came across this strange behavior in Google Chrome recently (version 17.0): I defined
I have just started python and came across something kind of strange. The following
I came across a strange behavior while theming Drupal. I turned a few modules
I came across this strange bit of CSS tonight... display: inline !ie; Now I've
I came across this recently, up until now I have been happily overriding the

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.