0

I need to generate a large array of numbers ranging from 0.00 to say 10000000.00 i.e 0.01 0.02 0.03 ... 1.51 1.52 1.583 ... etc

After looking around, I tried this answer which works fine for integers but however not working for decimals

desw
  • 13
  • 1
  • 3
  • what is the max precision should be ? – Andy Aug 03 '23 at 09:33
  • There are literally an infinite amount of decimal numbers between any two numbers, and you are not clear about what the interval here should be (all seems clear, but then 1.583???). As such, this question, in its current form, needs more info. – spender Aug 03 '23 at 09:33

7 Answers7

2

You didn't specify a language, so I will use Python.

import numpy as np

numbers = np.arange(0, 10000000.01, 0.01)

# Print first 10 elements
print(numbers[:10])

# Print last 10 elements
print(numbers[-10:])

ghostly
  • 31
  • 4
1

A simple way, try this:

let array = [];
for (let i = 0; i <= 1000000000; i++)
{
   array.push((i * 0.01).toFixed(2));
}
Lee Dungx
  • 91
  • 4
  • You won't see `1.583` in this array, but yes, using integers in your loop is very sensible (but will break if you exceed 9007199254740991.. we probably ran out of memory before then!) – spender Aug 03 '23 at 09:35
  • 1
    @kind user: I did it with C# too much, it makes me obsolete :D – Lee Dungx Aug 03 '23 at 09:45
  • @spender: I trust 1.583 is probably a mistake, there is no logic to that :v – Lee Dungx Aug 03 '23 at 09:47
  • Yes the 1.583 was one of the numbers in the range, sorry i shouldn't have included it – desw Aug 03 '23 at 10:20
1

In javascript, you can try:

let numbers = [];
for (let i = 0; i <= 10000000; i += 0.01) {
    numbers.push(parseFloat(i.toFixed(2))); //you can use toFixed(3) too if you want
}

Please note that creating a very large array like this will consume a lot of memory and may lead to performance issue.

Yusuf Ganiyu
  • 842
  • 9
  • 8
  • Your right I have tried a for loop however `parseFloat(args...)` inreases the time by 285ms per record and keeps growing. I may need to chunk it into parts with an async function: – desw Aug 03 '23 at 10:29
  • `const processBatch = async function(value, precision) { let numbers = []; for (let i = 0; i <= 10000000; i += 0.01) { numbers.push(parseFloat(i.toFixed(2))); //you can use toFixed(3) too if you want } }` – desw Aug 03 '23 at 10:29
0
const arr = Array.from(new Array(10000000), (_, index) => (index / 100).toFixed(2));

console.log(arr);

With this code, you will get an array of the numbers described in the question, after the dot there will be 2 numbers: 0.00, 0.01 and so on.

0

Try the following

let array = [];
let val = 0.00;
do{
   val += 0.01;
   array.push(val);
}while(val<1000000000.00);
  • This will fail because there is no exact representation of `0.01` in floating-point numbers. `val` will quickly start drifting from its intended value. – spender Aug 03 '23 at 15:22
0

You didn't specify a language, so I will use Javascript.

To generate a large array of numbers ranging from 0.00 to 10000000.00, you can use a loop to create the array in JavaScript. Here's a sample code to achieve that:

function generateIncrementedNumbers(start, end, step) {
    const result = [];
    for (let i = start; i <= end; i += 0.01) {
        result.push(i.toFixed(2));
    }
    return result;
}

const startNumber = 0.0;
const endNumber = 10000000.0;

const largeArray = generateIncrementedNumbers(startNumber, endNumber);
console.log(largeArray);
  • The tags at the bottom of the question is a general indication of what programming language the poster is interested in if included, in this case they have included [javascript]. – moken Aug 07 '23 at 04:07
0

In Javascript

function generateNumberRange(start, end, decimalPlaces) {
  const step = 1 / Math.pow(10, decimalPlaces);
  const range = [];

  for (let num = start; num <= end; num += step) {
    range.push(parseFloat(num.toFixed(decimalPlaces)));
  }

  return range;
}

// Example usage:
const startValue = 0.00;
const endValue = 10000000.00;
const decimalPlaces = 2;

const resultArray = generateNumberRange(startValue, endValue, decimalPlaces);
console.log(resultArray);
Thomas
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 06 '23 at 10:56