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 9200959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:53:22+00:00 2026-06-17T22:53:22+00:00

Can someone please explain what’s happening below: (I use Python 3.3) 1. >>> Decimal(0.1)

  • 0

Can someone please explain what’s happening below: (I use Python 3.3)

1. >>> Decimal("0.1") + Decimal("0.1") + Decimal("0.1") - Decimal("0.3")
       Decimal('0.0')

2. >>> Decimal(0.1) + Decimal(0.1) + Decimal(0.1) - Decimal(0.3)
       Decimal('2.775557561565156540423631668E-17')

3. >>> Decimal(0.1 + 0.1 + 0.1 - 0.3)
       Decimal('5.5511151231257827021181583404541015625E-17')

I know it has to do with floating point limitation, I’d be glad if someone can explain why

  • What has the " " got to do with the difference between example 1 and 2 above
  • Why does 2 produce a difference answer from 3 given that both have no " "?
  • 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-17T22:53:23+00:00Added an answer on June 17, 2026 at 10:53 pm

    In a nutshell, neither 0.1 nor 0.3 can be represented exactly as float:

    In [3]: '%.20f' % 0.1
    Out[3]: '0.10000000000000000555'
    
    In [4]: '%.20f' % 0.3
    Out[4]: '0.29999999999999998890'
    

    Consequently, when you use 0.1 or 0.3 to initialize Decimal(), the resulting value is approximately 0.1 or 0.3.

    Using strings ("0.1" or "0.3") does not have this problem.

    Finally, your second example produces a different result to your third example because, even though both involve implicit rounding, they involve rounding to a different number of decimal places.

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

Sidebar

Related Questions

Can someone please explain the major differences between Scala, Groovy and Clojure. I know
Can someone please explain how to read the below code to find last column
Can someone please explain the below to me. First is how I call the
Can someone please explain in plain English how to calculate it? I know that
Can someone please explain to me, why every time I use the FlowLayout Layout
Can someone please explain why this is happening? This is expected: $ echo -e
Can someone please explain to me what is going on with python in ubuntu
Can someone please explain why push behaves the way as shown below? Basically I
Can someone please explain what is going on in the code below? I understand
Can someone please explain when and why use commit? http://msdn.microsoft.com/en-us/library/ms190295.aspx . 1) Update in

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.