# Step of DoubleProgression

**URL:** <https://discuss.kotlinlang.org/t/step-of-doubleprogression/6769>\
**Category:** Language Design\
**Created:** [February 24, 2018, 10:45am UTC](https://discuss.kotlinlang.org/t/step-of-doubleprogression/6769 "2018-02-24T10:45:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasan.oryx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hasan.oryx/32/5515_2.png) [@hasan.oryx](https://discuss.kotlinlang.org/u/hasan.oryx)\
**Post date:** [February 24, 2018, 10:45am UTC](https://discuss.kotlinlang.org/t/step-of-doubleprogression/6769/1 "2018-02-24T10:45:09Z")

</div>

In the `for` loop, there are 3 types of `step` progression as shown [here](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/step.html) which are `Int`,  
`Long` and `char`.

How can I create:

`infix fun DoubleProgression.step(step: double): DoubleProgression`

So that I can execute my loop below:

`for (i in 0..4 step 0.05)`

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 24, 2018, 12:34pm UTC](https://discuss.kotlinlang.org/t/step-of-doubleprogression/6769/2 "2018-02-24T12:34:34Z")

</div>

I would take a look at the source of [IntRange](https://github.com/JetBrains/kotlin/blob/master/core/builtins/src/kotlin/Ranges.kt#L50), [IntProgression](https://github.com/JetBrains/kotlin/blob/master/core/builtins/src/kotlin/Progressions.kt#L80) and [IntProgressionIterator](https://github.com/JetBrains/kotlin/blob/master/core/builtins/src/kotlin/ProgressionIterators.kt#L49).

The “magic” you want is in the IntProgressionIterator.

You would probably want to create a DoubleRange as well as `step` functions from IntRange and DoubleRange to your new DoubleProgression 🙂

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [February 24, 2018, 1:42pm UTC](https://discuss.kotlinlang.org/t/step-of-doubleprogression/6769/3 "2018-02-24T13:42:06Z")

</div>

I would recommend you to consider using a for loop with `Int` or `Long`, and then adjust the result to the scale you need, instead of using `Double` in the first place, because floating-point numbers trend to lose precision.

For instance, `for (i in 0..4 step 0.05) print("$i, ")` would output:

```
0.0, 0.05, 0.1, 0.15000000000000002, 0.2, 0.25, 0.3, 0.35, 0.39999999999999997, 0.44999999999999996, 0.49999999999999994, 0.5499999999999999, 0.6, 0.65, 0.7000000000000001, 0.7500000000000001, 0.8000000000000002, 0.8500000000000002, 0.9000000000000002, 0.9500000000000003, 1.0000000000000002, 1.0500000000000003, 1.1000000000000003, 1.1500000000000004, 1.2000000000000004, 1.2500000000000004, 1.3000000000000005, 1.3500000000000005, 1.4000000000000006, 1.4500000000000006, 1.5000000000000007, 1.5500000000000007, 1.6000000000000008, 1.6500000000000008, 1.7000000000000008, 1.7500000000000009, 1.800000000000001, 1.850000000000001, 1.900000000000001, 1.950000000000001, 2.000000000000001, 2.0500000000000007, 2.1000000000000005, 2.1500000000000004, 2.2, 2.25, 2.3, 2.3499999999999996, 2.3999999999999995, 2.4499999999999993, 2.499999999999999, 2.549999999999999, 2.5999999999999988, 2.6499999999999986, 2.6999999999999984, 2.7499999999999982, 2.799999999999998, 2.849999999999998, 2.8999999999999977, 2.9499999999999975, 2.9999999999999973, 3.049999999999997, 3.099999999999997, 3.149999999999997, 3.1999999999999966, 3.2499999999999964, 3.2999999999999963, 3.349999999999996, 3.399999999999996, 3.4499999999999957, 3.4999999999999956, 3.5499999999999954, 3.599999999999995, 3.649999999999995, 3.699999999999995, 3.7499999999999947, 3.7999999999999945, 3.8499999999999943, 3.899999999999994, 3.949999999999994, 3.999999999999994,

```

However, `for (i in 0..80 step 1) print("${i / 20.0}, ")` outputs:

```
0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2.0, 2.05, 2.1, 2.15, 2.2, 2.25, 2.3, 2.35, 2.4, 2.45, 2.5, 2.55, 2.6, 2.65, 2.7, 2.75, 2.8, 2.85, 2.9, 2.95, 3.0, 3.05, 3.1, 3.15, 3.2, 3.25, 3.3, 3.35, 3.4, 3.45, 3.5, 3.55, 3.6, 3.65, 3.7, 3.75, 3.8, 3.85, 3.9, 3.95, 

```

It’s up to you to decide which option is the best, but I think it preaty clear.

---

<div class="post-metadata">

**Author:** ![hasan.oryx](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/hasan.oryx/32/5515_2.png) [@hasan.oryx](https://discuss.kotlinlang.org/u/hasan.oryx)\
**Post date:** [February 24, 2018, 5:28pm UTC](https://discuss.kotlinlang.org/t/step-of-doubleprogression/6769/4 "2018-02-24T17:28:48Z")

</div>

Thanks, do you have any idea why numbers like `0.15000000000000002` and `0.39999999999999997` are appearing!

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [February 24, 2018, 7:53pm UTC](https://discuss.kotlinlang.org/t/step-of-doubleprogression/6769/5 "2018-02-24T19:53:43Z")

</div>

It has to do with the way floating point numbers are represented in memory. A `double` is represented by 8 bytes, therefor there are only `2^64` different numbers that can be represented. The format used by Java and therefor Kotlin is [IEE 754](https://en.wikipedia.org/wiki/Double-precision_floating-point_format).  
Basicly if you add `0.05` multiple times, the error accumulates. That’s why you sometimes get these long, strange numbers.  
If you use ints and divide them like @SackCastellon suggested you also might not get exactly `0.3 ` but your are guaranteed to get the closest value.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [February 26, 2018, 3:03am UTC](https://discuss.kotlinlang.org/t/step-of-doubleprogression/6769/6 "2018-02-26T03:03:57Z")

</div>

And if you need to work in exact values for decimal numbers that is what BigDecimal is for.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [February 17, 2020, 3:57pm UTC](https://discuss.kotlinlang.org/t/step-of-doubleprogression/6769/7 "2020-02-17T15:57:38Z")

</div>

Just needed to do something similar (stepping from 0.5 to 5 in 0.25 increments using float) and this is what I did:

```
generateSequence(0.5f, {it + 0.25f} )
    .takeWhile { it <= 5f }
    .forEach { }

```
