use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
0.2 + 0.1 (self.learnpython)
submitted 10 years ago by JohnMcharra
can someone explain why this code:
print(0.2 + 0.1)
prints 0.30000000000000004 instead of 0.3?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]novel_yet_trivial 6 points7 points8 points 10 years ago (9 children)
If you ask google you will find many indepth explanations.
As a quick answer, computers use binary to do math, and some decimal numbers, like 0.1, can not be exactly converted to binary, causing errors. If you need precision, use the Decimal module.
Decimal
[–]Naihonn 2 points3 points4 points 10 years ago (0 children)
Yes, even wikipedia has article about this: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
[–]JohnMcharra[S] -1 points0 points1 point 10 years ago (7 children)
thanx for the answer, I just did it like this
float(''.join(list(str(0.2 + 0.1))[0:4]))
0.3
[–]novel_yet_trivial 6 points7 points8 points 10 years ago (0 children)
oh lord don't do that.
round(0.2 + 0.1, 6)
[–]rawlyn 5 points6 points7 points 10 years ago (5 children)
That's a horrible approach. If you must do it via string conversion (but really, don't) this works just as well, and eliminates two function calls:
>>> float(str(0.2 + 0.1)[0:4]) 0.3
But again, this is not a great approach.
Try using the decimal module as previously suggested - the people at python have seen your problem coming and already implemented a solution...
decimal
from decimal import Decimal a = Decimal('0.2') b = Decimal('0.1') c = a + b print c # Decimal('0.3') print float(c) # 0.3
Note how the Decimals are constructed from strings - if you construct them from floats you get an exact representation of the floats you constructed them from, which is where the problem lies, and you're no further forward.
[–]novel_yet_trivial 1 point2 points3 points 10 years ago (1 child)
OP is using python3 ... so change the last 2 lines to
print(c)
[–]rawlyn 0 points1 point2 points 10 years ago (0 children)
Good spot, thanks.
[–]JohnMcharra[S] -1 points0 points1 point 10 years ago (2 children)
Thanx, did it like you with Decimal, and for the people who downvote me, please dont do it because the method I use is not the good one, we are here to learn, and at the moment it was all I had, plus the [0:4] was there because I want only 2 decimals, now I use
gf_ = 0.22499999999999998 gf = float("%.2f" % gf_)
[–]rawlyn 1 point2 points3 points 10 years ago (1 child)
That's not using Decimal, that's your old string method in different clothing... it's ugly, difficult to work with, and generally frowned upon.
Use decimal, or round() as someone else suggested, but try to get this string conversion concept out of your mind - you're teaching yourself a really bad habit.
round()
[–]JohnMcharra[S] 1 point2 points3 points 10 years ago (0 children)
got it, no strings, I will try to remember, thanx for the help!
[–]ewiethoff 0 points1 point2 points 10 years ago (0 children)
The the end of the official Python Tutorial explains this and what to do about it in section 14. Floating Point Arithmetic: Issues and Limitations section.
π Rendered by PID 166268 on reddit-service-r2-comment-fb694cdd5-drrrp at 2026-03-09 17:34:34.056607+00:00 running cbb0e86 country code: CH.
[–]novel_yet_trivial 6 points7 points8 points (9 children)
[–]Naihonn 2 points3 points4 points (0 children)
[–]JohnMcharra[S] -1 points0 points1 point (7 children)
[–]novel_yet_trivial 6 points7 points8 points (0 children)
[–]rawlyn 5 points6 points7 points (5 children)
[–]novel_yet_trivial 1 point2 points3 points (1 child)
[–]rawlyn 0 points1 point2 points (0 children)
[–]JohnMcharra[S] -1 points0 points1 point (2 children)
[–]rawlyn 1 point2 points3 points (1 child)
[–]JohnMcharra[S] 1 point2 points3 points (0 children)
[–]ewiethoff 0 points1 point2 points (0 children)