# How to slightly extend a range?

**URL:** <https://discourse.julialang.org/t/how-to-slightly-extend-a-range/105499>\
**Category:** General Usage\
**Created:** [October 28, 2023, 11:22am UTC](https://discourse.julialang.org/t/how-to-slightly-extend-a-range/105499 "2023-10-28T11:22:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 28, 2023, 11:22am UTC](https://discourse.julialang.org/t/how-to-slightly-extend-a-range/105499/1 "2023-10-28T11:22:26Z")

</div>

Hello!

Suppose I have a range:

```julia
r = 1:10

```

I found a function to extend the range:

```julia
extended_range(r,δ) = (first(r) - δ):step(r):(last(r) + δ)

```

But I observe some different behaviours:

```julia
extended_range(r,1)
0:1:11

extended_range(r,eps(Float64))
0.9999999999999998:1.0:10.0

```

It works as intended with a large number, but not with a very small number. Is what I request possible to do in some way?

The left part, 0.99999…8 is correct and what I want. For 10.0 I would have wanted 10.000…2

Kind regards

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 28, 2023, 11:24am UTC](https://discourse.julialang.org/t/how-to-slightly-extend-a-range/105499/2 "2023-10-28T11:24:49Z")

</div>

May I ask why you must only extend by eps()?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 28, 2023, 11:27am UTC](https://discourse.julialang.org/t/how-to-slightly-extend-a-range/105499/3 "2023-10-28T11:27:41Z")

</div>

Inspired by code here: [https://github.com/dhoegh/Rainflow.jl/blob/master/src/Rainflow.jl](https://github.com/dhoegh/Rainflow.jl/blob/master/src/Rainflow.jl)

I am trying to optimize this function to avoid using `collect`. My version uses vcat below:

```julia
""" Sums the cycle count given intervals of range_intervals and mean_intervals. The range_intervals and mean_intervals is given in fraction of range size"""
function sum_cycles(cycles::Vector{Cycle{T}}, range_intervals::Interval{T}, mean_intervals::Interval{T}) where {T <: Real}
    bounds = find_boundary_vals(cycles)
    bins = zeros(length(range_intervals)-1, length(mean_intervals)-1)
    range_in = (range_intervals*bounds.max_range)/100
    mean_in = (mean_intervals*(bounds.max_mean-bounds.min_mean))/100
    mean_in = mean_in .+ bounds.min_mean
    
    @assert issorted(mean_intervals) "The array needs to be sorted in raising order"
    @assert issorted(range_intervals) "The array needs to be sorted in raising order"

    mean_i = vcat(mean_in)
    range_i = vcat(range_in)
    
    #ensure the cycles is in the intervals by adding a small error to the end values of the interal.
    error_m = (bounds.max_mean-bounds.min_mean) #instead of 1e-14 - The rounding is performed due to numerical noise in the floats when comparing
    mean_i[end]+=error_m
    mean_i[1]-=error_m
    error_r = bounds.max_range #instead of 1e-14
    range_i[end]+=error_r
    range_i[1]-=error_r

    @inbounds for cycle in cycles
        i = find_range(range_i, cycle.range)
        j = find_range(mean_i, cycle.mean)
        bins[i,j] += cycle.count
    end
    return bins
end

```

Trying to see if I can make the code a bit prettier/faster, by extending the range slightly through a function.

Kind regards

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 28, 2023, 11:35am UTC](https://discourse.julialang.org/t/how-to-slightly-extend-a-range/105499/4 "2023-10-28T11:35:22Z")

</div>

It doesn’t look like one must use eps() for this? Just use a small number like 10^-12 your original approach should be fine

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [October 28, 2023, 11:47am UTC](https://discourse.julialang.org/t/how-to-slightly-extend-a-range/105499/5 "2023-10-28T11:47:19Z")

</div>

It is not:

```julia
extended_range(r,1e-12)
0.999999999999:1.0:9.999999999999

extended_range(r,0.01)
0.99:1.0:9.99

```

It is decreasing range, when it should be extending it

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [October 28, 2023, 12:06pm UTC](https://discourse.julialang.org/t/how-to-slightly-extend-a-range/105499/6 "2023-10-28T12:06:56Z")

</div>

May I ask why you keep the same step? That looks / reads strange to me mathematically and might explain your (strange) end points…

If you have `1:10` your step is `1.0`, if you now try to use your function (let me take a more extreme ` δ`) with ` δ=0.25`  
you would take as first point `0.75` but still steps of size 1, so you would not reach your desired end of `10.25` but only `9.75`?

_edit:_ You could keep the length (instead of keeping the step), then the step is adapted automatically, e.g.

```julia-repl
julia> extend_range2(r,δ) = range(start=first(r)-δ, stop=last(r)+δ, length=length(r))
extend_range2 (generic function with 1 method)

julia> extend_range2(1:10, 0.25)
0.75:1.0555555555555556:10.25

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 28, 2023, 12:46pm UTC](https://discourse.julialang.org/t/how-to-slightly-extend-a-range/105499/7 "2023-10-28T12:46:46Z")

</div>

Couple things to be aware about:

1. Ranges compute the inclusive endpoints during construction, so if your step is too large, adjusting the likely exclusive input endpoint won’t make a difference

```julia
julia> 1:2:8.0, 1:2:8.01
(1.0:2.0:7.0, 1.0:2.0:7.0)

```

The intention is that the same sequence is represented by the same instance for comparison, even if the input values (3 of start, step, stop, or length) are different.

1. Adding `eps(Float64)` to 10 is actually doing nothing. You probably intended to add the smallest difference, but the thing about floating points is that difference depends on the absolute value. `eps(Float64)` does the difference for `1.0`, and that is smaller than the difference for `10.0`.

```julia
julia> eps(Float64), eps(1.0), eps(10.0)
(2.220446049250313e-16, 2.220446049250313e-16, 1.7763568394002505e-15)

julia> (10 + eps(Float64)) - 10
0.0

julia> (10 + eps(10.0)) - 10
1.7763568394002505e-15

```
