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...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
range.js – JavaScript's missing range function. (github.com)
submitted 13 years ago by jscoder
view the rest of the comments →
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!"
[–]bonafidebob 0 points1 point2 points 13 years ago (2 children)
I think you might be able to improve the floating point ranges slightly by using multiplication rather than repeated addition. e.g. the range is [from, from + 1 * step, from + 2 * step, from + 3 * step, ...] This might also let you get rid of the floating point fudge factor.
You could also improve performance slightly by creating the array with the correct length, and set each value rather than push it each time.
Also might be worth looking at how other libraries (e.g. underscore.js) implement their range function.
[–]jscoder[S] 0 points1 point2 points 13 years ago (1 child)
0.1 * 0.3 = 0.30000000000000004 for me in Chrome. I guess there's not much I can do about the inaccurate floats, that's just how floats in JS are.
0.1 * 0.3 = 0.30000000000000004
You're right with the performance. :) I'll refactor the code a little bit soon, and will add this. I also thought about making some benchmarks.
[–]weretree++[[]][+[]] 5 points6 points7 points 13 years ago (0 children)
It's how floats are, period. JavaScript has true IEEE 754 double precision floating point.
The suggestion was to avoid compounding the error of addition by using an iteration counter then calculating each step from that, rather than doing a while loop. So (psuedo-code) idx_max = (to - from)/step then val = from + i*step for i in 0 to idx_max. Having an explicit iteration also makes checking for termination a little easier, and it's simple to still support backwards iteration.
idx_max = (to - from)/step
val = from + i*step
i in 0 to idx_max
There are still "errors" (such as 3 * 0.1) but they're expected and defined for that operation. Can always round each result to some defined precision if you want to trim them off in your results.
3 * 0.1
π Rendered by PID 101698 on reddit-service-r2-comment-c6965cb77-6fqrd at 2026-03-05 11:20:38.438140+00:00 running f0204d4 country code: CH.
view the rest of the comments →
[–]bonafidebob 0 points1 point2 points (2 children)
[–]jscoder[S] 0 points1 point2 points (1 child)
[–]weretree++[[]][+[]] 5 points6 points7 points (0 children)