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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:21:13+00:00 2026-05-18T20:21:13+00:00

I want a function that creates an array of increasing floats. I run into

  • 0

I want a function that creates an array of increasing floats. I run into problems when the incremenent cannot be represented precisely in binary. An example in an increment of 0.1, whose binary representation is infinitely repeating.

var incrementalArray = function(start, end, step) {
    var arr = [];
    for (var i = start; i <= end; i += step) {
        // 0.1 cannot be represented precisely in binary
        // not rounding i will cause weird numbers like 0.99999999
        if (step === 0.1) {
            arr.push(i.toFixed(1));
        } else {
            arr.push(i);
        }
    }
    return arr;
};

There are two problems with this implementation. A) It only covers the case of having an increment of 0.1. Aren’t there numerous other magic numbers that have repeating binary representation? B) The returned array does not include the end value when the increment is 0.1. However, it does include the end value when the increment is otherwise, making this function unpredictable.

incrementalArray(0.0, 3.0, 0.1);
// [0.0, 0.1, 0.2, .... 2.8, 2.9]

incrementalArray(2,10,2);
// [2, 4, 6, 8, 10]

How can this function work for all special increments and be predictable?

  • 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-05-18T20:21:14+00:00Added an answer on May 18, 2026 at 8:21 pm

    I think this might work:

    var incrementalArray = function(start, end, step) {
        var arr = [];
        // convert count to an integer to avoid rounding errors
        var count = +((end - start) / step).toFixed();
        for (var j = 0; j <= count; j++) {
            var i = start + j * step;
            arr.push(i);
        }
        return arr;
    };
    

    Output: 0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9, 1, 1.1, 1.2000000000000001, 1.3, 1.4000000000000001, 1.5, 1.6, 1.7000000000000001, 1.8, 1.9000000000000001, 2, 2.1, 2.2, 2.3000000000000002, 2.4000000000000003, 2.5, 2.6, 2.7, 2.8000000000000002, 2.9000000000000003, 3

    If you want the output truncated to the same number of decimals as the input, you can use this:

    var incrementalArray = function(start, end, step) {
        var prec = ("" + step).length - ("" + step).indexOf(".") - 1;
        var arr = [];
        // convert count to an integer to avoid rounding errors
        var count = +((end - start) / step).toFixed();
        for (var j = 0; j <= count; j++) {
            var i = start + +(j * step).toFixed(prec);
            arr.push(i);
        }
        return arr;
    };
    

    Output: 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0

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

Sidebar

Related Questions

I want a function that creates an array of increasing floats. I run into
I want to create a C macro that creates a function with a name
Say I want a function that takes two floats ( x and y ),
I want a simple function that receives a string and returns an array of
I have this working function that finds folders and creates an array. function dua_get_files($path)
I want to create a function that performs a function passed by parameter on
I want to create a function that takes a 2 dimensional list and outputs
I want to create a postgres function that builds the set of columns it
I want to create a function such that each time the function runs, the
I want to create NUnit test to ensure that my function does not throw

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.