# LinearInterpolation between two points

**URL:** <https://discourse.julialang.org/t/linearinterpolation-between-two-points/67090>\
**Category:** General Usage\
**Tags:** interpolations\
**Created:** [August 27, 2021, 10:17am UTC](https://discourse.julialang.org/t/linearinterpolation-between-two-points/67090 "2021-08-27T10:17:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ymtoo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ymtoo/32/18859_2.png) [@ymtoo](https://discourse.julialang.org/u/ymtoo)\
**Post date:** [August 27, 2021, 10:17am UTC](https://discourse.julialang.org/t/linearinterpolation-between-two-points/67090/1 "2021-08-27T10:17:34Z")

</div>

MWE:

```julia
using Interpolations

itp1 = LinearInterpolation([1,8600],[1,8600]; extrapolation_bc=Line())
convert(Float64, itp1(8700)) #-1421.6002606281033

itp2 = LinearInterpolation([1,3200,8600],[1,3200,8600]; extrapolation_bc=Line())
convert(Float64, itp2(8700)) #8700.0

itp3 = LinearInterpolation([1,2150,4300,6450,8600],[1,2150,4300,6450,8600]; extrapolation_bc=Line())
convert(Float64, itp3(8700)) #8700.0

```

The linear extrapolation between 2 points is wrong. Is that a limitation of the scheme?

Julia and package versions:

```julia
julia> versioninfo()
Julia Version 1.6.1
Commit 6aaedecc44 (2021-04-23 05:59 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)

(jl_QhuT0L) pkg> st
      Status `/tmp/jl_QhuT0L/Project.toml`
  [a98d9a8b] Interpolations v0.13.4

```

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [August 27, 2021, 11:37am UTC](https://discourse.julialang.org/t/linearinterpolation-between-two-points/67090/3 "2021-08-27T11:37:31Z")

</div>

It appears related to the (integer) type of the numbers you use. Apparently, when these numbers are small enough, things go fine:

```julia
julia> itp = LinearInterpolation([1,2],[1,2]; extrapolation_bc=Line())
2-element extrapolate(interpolate((::Vector{Int64},), ::Vector{Int64}, Gridded(Linear())), Line()) with element type Float64:
 Ratios.SimpleRatio{Int64}(1, 1)
 Ratios.SimpleRatio{Int64}(2, 1)

julia> convert(Float64,itp(3))
3.0

```

But as soon as the numbers are higher, the integers needed for defining the ratios within the approximation algorithm apparently overflow:

```julia
julia> itpi = LinearInterpolation([1,10000],[1,10000]; extrapolation_bc=Line())
2-element extrapolate(interpolate((::Vector{Int64},), ::Vector{Int64}, Gridded(Linear())), Line()) with element type Float64:
 Ratios.SimpleRatio{Int64}(99980001, 99980001)
 Ratios.SimpleRatio{Int64}(999800010000, 99980001)

julia> convert(Float64,itpi(10001))
773.9376918087789

```

An easy fix is to use floats as the input data:

```julia
julia> itpf = LinearInterpolation([1.0,10000.0],[1.0,10000.0]; extrapolation_bc=Line())
2-element extrapolate(interpolate((::Vector{Float64},), ::Vector{Float64}, Gridded(Linear())), Line()) with element type Float64:
     1.0
 10000.0

julia> convert(Float64,itpf(10001.0))
10001.0

```

---

<div class="post-metadata">

**Author:** ![ymtoo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ymtoo/32/18859_2.png) [@ymtoo](https://discourse.julialang.org/u/ymtoo)\
**Post date:** [August 27, 2021, 4:34pm UTC](https://discourse.julialang.org/t/linearinterpolation-between-two-points/67090/4 "2021-08-27T16:34:48Z")

</div>

@zdenek_hurak You’re right. In fact, the problem appears on interpolations of any number of points, e.g.,

```julia
itpi = LinearInterpolation([1,4000,10000],[1,4000,10000]; extrapolation_bc=Line())
convert(Float64, itpi(10001)) #-4232.5988223067525

```

The fix works. Thanks!

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [August 27, 2021, 6:17pm UTC](https://discourse.julialang.org/t/linearinterpolation-between-two-points/67090/5 "2021-08-27T18:17:12Z")

</div>

Hmm, I am hesitating if this should be regarded (and filed) as an Issue. In general the responsibility for the overflow problems is on the application programmer, right? But shouldn’t the interpolation function give some warning here? The parameters of the problem look innocent, don’t they?

EDIT: so I did it [https://github.com/JuliaMath/Interpolations.jl/issues/457](https://github.com/JuliaMath/Interpolations.jl/issues/457)

---

<div class="post-metadata">

**Author:** ![ymtoo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ymtoo/32/18859_2.png) [@ymtoo](https://discourse.julialang.org/u/ymtoo)\
**Post date:** [August 30, 2021, 2:51am UTC](https://discourse.julialang.org/t/linearinterpolation-between-two-points/67090/6 "2021-08-30T02:51:34Z")

</div>

Agreed. A warning message indicating integer overflow would be very helpful especially for new Julia users who do not familiar with the function output (`Rational` type).

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [September 11, 2021, 10:22am UTC](https://discourse.julialang.org/t/linearinterpolation-between-two-points/67090/7 "2021-09-11T10:22:34Z")

</div>

With SaferIntegers.jl version 3, you can detect overflow.

[https://github.com/JuliaMath/Interpolations.jl/issues/457](https://github.com/JuliaMath/Interpolations.jl/issues/457)

```julia
julia> using SaferIntegers, Interpolations

julia> itpi = LinearInterpolation(SafeInt[1,10000],SafeInt[1,10000]; extrapolation_bc=Line())
2-element extrapolate(interpolate((::Vector{SafeInt64},), ::Vector{SafeInt64}, Gridded(Linear())), Line()) with element type Float64:
 SimpleRatio{SafeInt64}(99980001, 99980001)
 SimpleRatio{SafeInt64}(999800010000, 99980001)

julia> itpi(10001)
ERROR: OverflowError: 999800010000 * 99980001 overflowed for type Int64
Stacktrace:
...

```
