C # Array of increments

If I want to generate an array that goes from 1 to 6 and increments by .01, what is the most efficient way to do this?

What I want is an array with the minimum values โ€‹โ€‹mins and maxs ... as follows: x[1,1.01,1.02,1.03...]

+6
source share
8 answers

Taking on the values โ€‹โ€‹of start , end and increment , you can abstract this further:

 Enumerable .Repeat(start, (int)((end - start) / increment) + 1) .Select((tr, ti) => tr + (increment * ti)) .ToList() 

Let me break it:

Enumerable.Repeat takes a starting number, repeats the specified number of elements and returns an enumerated (collection). In this case, we start with the start element, find the difference between start and end and divide it by increment (this gives us the number of increments between start and end ) and add one to include the original number. This should give us the number of items to use. Just keep in mind that since increment is decimal / double, there may be rounding errors when passing to int.

Select converts all elements of an enumerated value specified by a particular selection function. In this case, we take the number that was generated and the index, and add the original number with the index times the increment.

Finally, a call to ToList save the collection in memory.

If you often use this, you can create a way to do this:

 public static List<decimal> RangeIncrement(decimal start, decimal end, decimal increment) { return Enumerable .Repeat(start, (int)((end - start) / increment) + 1) .Select((tr, ti) => tr + (increment * ti)) .ToList() } 

Edit: changed to use replay so that non-integer values โ€‹โ€‹will be saved. Also, error checking is not performed here, so you have to make sure that increment not 0 and that start < end * sign(increment ). The reason for multiplying the end by the sign of the increment is that if you increase a negative number, then the end must be before the start.

+14
source

The easiest way is to use Enumerable.Range :

 double[] result = Enumerable.Range(100, 500) .Select(i => (double)i/100) .ToArray(); 

(hence effective in terms of readability and lines of code)

+9
source

I would just do a simple function.

  public IEnumerable<decimal> GetValues(decimal start, decimal end, decimal increment) { for (decimal i = start; i <= end; i += increment) yield return i; } 

Then you can turn it into an array, query it, or do whatever you want with it.

  decimal[] result1 = GetValues(1.0m, 6.0m, .01m).ToArray(); List<decimal> result2 = GetValues(1.0m, 6.0m, .01m).ToList(); List<decimal> result3 = GetValues(1.0m, 6.0m, .01m).Where(d => d > 3 && d < 4).ToList(); 
+4
source

Use a for loop in steps of 0.01:

 List<decimal> myList = new List<decimal>(); for (decimal i = 1; i <= 6; i+=0.01) { myList.Add(i); } 
+2
source

Elegant

 double[] v = Enumerable.Range(1, 600).Select(x => x * 0.01).ToArray(); 

Effective

 Use for loop 
+1
source

No matter what you do, do not use a floating point data type (e.g. double ), they do not work for such things on behalf of rounding behavior. Go for a decimal or integers with a coefficient. For the latter:

 Decimal[] decs = new Decimal[500]; for (int i = 0; i < 500; i++){ decs[i] = (new Decimal(i) / 100)+1 ; } 
0
source

You could solve it like that. The solution method returns a double array

 double[] Solution(double min, int length, double increment) { double[] arr = new double[length]; double value = min; arr[0] = value; for (int i = 1; i<length; i++) { value += increment; arr[i] = value; } return arr; } 
0
source
 var ia = new float[500]; //guesstimate var x = 0; for(float i =1; i <6.01; i+= 0.01){ ia[x] = i; x++; } 

You can multithreadedly use this for speed, but it's probably not worth the overhead unless you plan on running it on a really very slow processor.

-1
source

Source: https://habr.com/ru/post/916458/


All Articles