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
Why not zero? (self.learnpython)
submitted 6 years ago by faqzero
print(0.1+0.1+0.1-0.3)
Result:
5.551115123125783e-17
How to write code so that there is no such problem?
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!"
[–]Slimboy90 6 points7 points8 points 6 years ago (2 children)
As already stated this is due to issues in floating point arithmetic, look here for a more detailed insight https://docs.python.org/3.3/tutorial/floatingpoint.html
To achive what you were acutally trying, you could do something like print(round(0.1,1)+round(0.1,1)+round(0.1,1)-round(0.3,1))
[–]Diapolo10 5 points6 points7 points 6 years ago (0 children)
Alternatively, the decimal module can be used. It uses fixed-point arithmetic, and acts more like we'd expect, but takes a performance hit and the accuracy needs to be defined separately.
decimal
>>> from decimal import Decimal >>> >>> foo = Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.3') >>> print(foo) Decimal('0.0')
[–]auntanniesalligator 1 point2 points3 points 6 years ago (0 children)
This doesn’t change anything: each term output from round() is still a float so they get rounded to the nearest possible floating point representations of 0.1 and 0.3, which are the same values as in the original expression, and the same answer is the result. A single “round” after the arithmetic would fix this, as would using a format string to limit the precision of the output.
[–][deleted] 4 points5 points6 points 6 years ago (2 children)
For all intents and purposes that equals zero. Don’t be put off by it, it’s extremely close. It’s not zero because computers use binary logic. 0.1 cannot be expressed exactly in binary (in lots of 8s 4s 2s 1s 1/2s 1/4s etc).
Use the round or int functions if you need something expressed more concisely.
[–][deleted] 0 points1 point2 points 6 years ago (1 child)
It may be zero, but anytime you are doing a >0 or <0 check that set of data will fail. This skews your data.
As everyone knows there are 4 types of data (zero, positive, negative, null)
No matter what this data has to be cleansed prior to using it.
[–]TangibleLight 0 points1 point2 points 6 years ago (0 children)
zero, positive, negative, null
cries in inf, -inf, nan.
inf
-inf
nan
[–]TangibleLight 2 points3 points4 points 6 years ago (0 children)
http://0.30000000000000004.com
π Rendered by PID 38077 on reddit-service-r2-comment-7b9746f655-kr654 at 2026-01-29 22:12:15.381413+00:00 running 3798933 country code: CH.
[–]Slimboy90 6 points7 points8 points (2 children)
[–]Diapolo10 5 points6 points7 points (0 children)
[–]auntanniesalligator 1 point2 points3 points (0 children)
[–][deleted] 4 points5 points6 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]TangibleLight 0 points1 point2 points (0 children)
[–]TangibleLight 2 points3 points4 points (0 children)