- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
public static IEnumerable<float> Single(float from, float to, float step)
{
if (step <= 0.0f) step = (step == 0.0f) ? 1.0f : -step;
if (from <= to)
{
for (float f = from; f <= to; f += step) yield return f;
}
else
{
for (float f = from; f >= to; f -= step) yield return f;
}
}
public static IEnumerable<double> Double(double from, double to, double step)
{
if (step <= 0.0) step = (step == 0.0) ? 1.0 : -step;
if (from <= to)
{
for (double d = from; d <= to; d += step) yield return d;
}
else
{
for (double d = from; d >= to; d -= step) yield return d;
}
}
Такие методы накопированы для всех типов данных, которые известны поциенту.
Но особо интересны эти джва метода
Unlike some other programmimg languages (notably F#), C# doesn't have any built-in support for dealing with ranges of numbers. The .NET Framework does have the Enumerable.Range() method.
- It can only deal with Int32's.
- You can't specify a 'step' from one element of the range to the next. In effect, the step is always one. In this article, I'd therefore like to present a static Range class to deal with these deficiencies.
http://www.c-sharpcorner.com/uploadfile/b942f9/dealing-with-ranges-of-numbers-in-C-Sharp
USB 26.10.2012 18:03 # 0
bormand 26.10.2012 18:45 # 0
bormand 26.10.2012 19:13 # −1
http://ideone.com/N96Zi6
Правильный ответ (на PHP, у которого range для даблов написан грамотно, в отличие от этого кода):
http://ideone.com/aD03qL
Ну и нахуя нужен такой рейндж, который даже на таких маленьких числах поюзать нельзя?
MOXHATKA 25.08.2021 21:03 # 0