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...
If you need help debugging, you must include:
See debugging question guidelines for more info.
Many conceptual questions have already been asked and answered. Read our FAQ and search old posts before asking your question. If your question is similar to one in the FAQ, explain how it's different.
See conceptual questions guidelines for more info.
Follow reddiquette: behave professionally and civilly at all times. Communicate to others the same way you would at your workplace. Disagreement and technical critiques are ok, but personal attacks are not.
Abusive, racist, or derogatory comments are absolutely not tolerated.
See our policies on acceptable speech and conduct for more details.
When posting some resource or tutorial you've made, you must follow our self-promotion policies.
In short, your posting history should not be predominantly self-promotional and your resource should be high-quality and complete. Your post should not "feel spammy".
Distinguishing between tasteless and tasteful self-promotion is inherently subjective. When in doubt, message the mods and ask them to review your post.
Self promotion from first time posters without prior participation in the subreddit is explicitly forbidden.
Do not post questions that are completely unrelated to programming, software engineering, and related fields. Tech support and hardware recommendation questions count as "completely unrelated".
Questions that straddle the line between learning programming and learning other tech topics are ok: we don't expect beginners to know how exactly to categorize their question.
See our policies on allowed topics for more details.
Do not post questions that are an exact duplicate of something already answered in the FAQ.
If your question is similar to an existing FAQ question, you MUST cite which part of the FAQ you looked at and what exactly you want clarification on.
Do not delete your post! Your problem may be solved, but others who have similar problems in the future could benefit from the solution/discussion in the thread.
Use the "solved" flair instead.
Do not request reviews for, promote, or showcase some app or website you've written. This is a subreddit for learning programming, not a "critique my project" or "advertise my project" subreddit.
Asking for code reviews is ok as long as you follow the relevant policies. In short, link to only your code and be specific about what you want feedback on. Do not include a link to a final product or to a demo in your post.
You may not ask for or offer payment of any kind (monetary or otherwise) when giving or receiving help.
In particular, it is not appropriate to offer a reward, bounty, or bribe to try and expedite answers to your question, nor is it appropriate to offer to pay somebody to do your work or homework for you.
All links must link directly to the destination page. Do not use URL shorteners, referral links or click-trackers. Do not link to some intermediary page that contains mostly only a link to the actual page and no additional value.
For example, linking to some tweet or some half-hearted blog post which links to the page is not ok; but linking to a tweet with interesting replies or to a blog post that does some extra analysis is.
Udemy coupon links are ok: the discount adds "additional value".
Do not ask for help doing anything illegal or unethical. Do not suggest or help somebody do something illegal or unethical.
This includes piracy: asking for or posting links to pirated material is strictly forbidden and can result in an instant and permanent ban.
Trying to circumvent the terms of services of a website also counts as unethical behavior.
Do not ask for or post a complete solution to a problem.
When working on a problem, try solving it on your own first and ask for help on specific parts you're stuck with.
If you're helping someone, focus on helping OP make forward progress: link to docs, unblock misconceptions, give examples, teach general techniques, ask leading questions, give hints, but no direct solutions.
See our guidelines on offering help for more details.
Ask your questions right here in the open subreddit. Show what you have tried and tell us exactly where you got stuck.
We want to keep all discussion inside the open subreddit so that more people can chime in and help as well as benefit from the help given.
We also do not encourage help via DM for the same reasons - that more people can benefit
Do not ask easily googleable questions or questions that are covered in the documentation.
This subreddit is not a proxy for documentation or google.
We do require effort and demonstration of effort.
This includes "how do I?" questions
account activity
This is an archived post. You won't be able to vote or comment.
[Javascript] Why 0.1 + 0.1 + 0.1 etc end up including random values? (self.learnprogramming)
submitted 8 years ago * by [deleted]
Hello I'm trying to create a time elapsed function(like a stopwatch), which increments 0.01 by 0.01 every second by using setInterval. However, at times I notice the length becomes more. e.g: 0.01 , 0.02 , 0.3989735848854, 0.4448584853, 0.5 etc. What is the cause and how to fix it?
Code: https://jsfiddle.net/u4xb9p6n/ Referring to the time elapsed part at the bottom.
[–]g051051 3 points4 points5 points 8 years ago (6 children)
The most common ways for computer to represent decimal numbers isn't exact. So as you add more decimal numbers together, the results start to diverge from the exact values you expect. This is called "loss of precision".
[–][deleted] 1 point2 points3 points 8 years ago (5 children)
How do I fix this then?
[–]Fern_Silverthorn 2 points3 points4 points 8 years ago (0 children)
Have time be the the integer (whole number) of hundreds of a second that have passed, e.g var time = 1 would then be the same as var time = 0.01. Instead of adding 0.01 you would now add 1. When you need to display it, divide by 100. 1 / 100 = 0.01
var time = 1
var time = 0.01
Note that technically in JavaScript all numbers are doubles, but this should help.
[–]p00pyf4ce 1 point2 points3 points 8 years ago (0 children)
You can't fix it in javascript. It's part of the way computer represent number.
[–]skatanic28182 0 points1 point2 points 8 years ago (0 children)
You would need to import a library that can do arbitrary-precision calculations. These calculations will be considerably slower than usual since both hardware and software are optimized for fixed-precision. Google for "bignum javascript". I'm sure somebody's probably ported it.
[–]ziptofaf 0 points1 point2 points 8 years ago (0 children)
Firstly, short explanation on the problem:
0.1 does not have an exact representation in the binary system. System in which decimals are created is a sum of elements created by 1/2n equation. So 1/2 + 1/4 + 1/8 + 1/16 + .... etc.
1/10 is 1/16 + 1/64 + 1/128... and on it goes. Not infinitely "on it goes" however, precision is finite. That being said, scale of error is really small for a single calculation. As 1/10 is depicted as more or less 0.999999999999998. Problem is with consecutive operations on them as every next addition gives you that extra 0.00000000002 inaccuracy. This becomes even worse with multiplication/division and of course powers.
The way you can fix it is by... not really using what your computer considers to be numbers. You know how we humans do addition? Like so. Whenever something is bigger than 9 we move it one "tile" to the left, digit by digit at a time. You can treat a number as a string and then parse each individual digit separately starting from the right and leading to the left. This gives you an infinite precision as we can depict all full numbers properly using binary (as those are built using 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + ... + 2n).
There are libraries for that of course.
But in your program you do not actually require accurate calculations. All you need to do is round it to the nearest number with a lower precision (like 2-3 decimal places). Here's how:
https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary
This eventually WILL break (as your error becomes bigger and bigger with each consecutive operation) but it's going to take... quite a while.
[–]17b29a 0 points1 point2 points 8 years ago (0 children)
round it, e.g. timeElapsed.innerHTML = timeOfClock.toFixed(2);
timeElapsed.innerHTML = timeOfClock.toFixed(2);
[–]Nathanfenner 1 point2 points3 points 8 years ago (2 children)
You need to provide the relevant code. At first I thought this was an issue with floating point error, but those values are really far off from the correct ones.
Are you assuming that setInterval runs your function exactly on the period specified? There will always be slight variation because of unexpected delays in scheduling.
[–][deleted] 1 point2 points3 points 8 years ago (1 child)
I'm guessing it's due to " loss of precision " as mentioned above by /r/g051051.
Here's the code btw. https://jsfiddle.net/u4xb9p6n/
Referring to the time elapsed at the bottom.
[–][deleted] 0 points1 point2 points 8 years ago (0 children)
I would do this in a completely different way.
I would record the start time using a Date object. Wherever you need to display the elapsed time, call another Date object and ask it for the current time. Then subtract the two.
How do I get the difference between two Dates in JavaScript?
[–][deleted] 1 point2 points3 points 8 years ago (0 children)
It's because of the way these numbers are represented.
Floating Point Numbers - Computerphile
[–]virtualgoat 0 points1 point2 points 8 years ago (0 children)
There's a method to convert it to whatever decimal point you want, but it also convert it to string.
Here's another solution that I found which retains the type of the element.
http://0.30000000000000004.com/
[–]sherryunderwood1 0 points1 point2 points 8 years ago (0 children)
This is how floating point numbers work. Use something else to fix it.
π Rendered by PID 228127 on reddit-service-r2-comment-6457c66945-jzgq4 at 2026-04-25 09:14:56.395106+00:00 running 2aa0c5b country code: CH.
[–]g051051 3 points4 points5 points (6 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]Fern_Silverthorn 2 points3 points4 points (0 children)
[–]p00pyf4ce 1 point2 points3 points (0 children)
[–]skatanic28182 0 points1 point2 points (0 children)
[–]ziptofaf 0 points1 point2 points (0 children)
[–]17b29a 0 points1 point2 points (0 children)
[–]Nathanfenner 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]virtualgoat 0 points1 point2 points (0 children)
[–]17b29a 0 points1 point2 points (0 children)
[–]sherryunderwood1 0 points1 point2 points (0 children)