# Special Functions (Elliptic.jl) and arbitrary precision

**URL:** https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658
**Category:** General Usage
**Created:** [May 3, 2020, 9:28am UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658 "2020-05-03T09:28:00Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![StarecZosima](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@StarecZosima](https://discourse.julialang.org/u/StarecZosima)
#### Post date: [May 3, 2020, 9:28am UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/1 "2020-05-03T09:28:00Z")

</div>

I do not understand how to work in arbitrary precision in Julia.

I encountered the problem when using a function from the Elliptic.jl package

```
         K(1-10^-23)

```

returns  
Inf

while for example Mathematica accurately computes a value of approx- 30.168, using 200 digits of working precision.

If my understanding is right, the BigFloat type shoud allow arbitrary precision computations in Julia.  
I tried

```
setprecision(500)
a = BigFloat(1-10^-23)
K(a)

```

but still obtain Inf as a result. As a matter of fact

```
 BigFloat(1-10^-23) == 1

```

returns True.

Could somebody please explain what is that I am doing wrong? Thanks a lot

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 3, 2020, 9:39am UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/2 "2020-05-03T09:39:38Z")

</div>

You identify the source of the problem correctly: precision is lost _before_ converting to `BigFloat`.

You should convert something to `BigFloat` _before_ performing that calculation, eg make

```julia
a = 1 - BigFloat(10)^-23

```

or similar.

---

<div class="post-metadata">

### Author: ![StarecZosima](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@StarecZosima](https://discourse.julialang.org/u/StarecZosima)
#### Post date: [May 3, 2020, 9:45am UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/3 "2020-05-03T09:45:18Z")

</div>

Thanks for your reply, but I did try that one too

```
      a = 1 -BigFloat(10)^-23

```

and while I now noticed that indeed, a =! 1

still

```
        K(a)

```

diverges.  
At least now I can define the argument correctly. Could it be something to do with the function itself?  
Thanks

---

<div class="post-metadata">

### Author: ![EHBaozi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ehbaozi/32/14854_2.png) [@EHBaozi](https://discourse.julialang.org/u/EHBaozi)
#### Post date: [May 3, 2020, 11:07am UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/4 "2020-05-03T11:07:50Z")

</div>

This is probably a problem with the package? I see [here](https://github.com/nolta/Elliptic.jl/blob/master/src/Elliptic.jl#L100) that `K` converts everything to `Float64`.

---

<div class="post-metadata">

### Author: ![EHBaozi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ehbaozi/32/14854_2.png) [@EHBaozi](https://discourse.julialang.org/u/EHBaozi)
#### Post date: [May 3, 2020, 11:18am UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/5 "2020-05-03T11:18:56Z")

</div>

As an alternative, the `DoubleFloats` package exports the `ellipk` function, but it seems to give a different answer than Mathematica:

```julia-repl
julia> using DoubleFloats; ellipk(1-Double64(10)^(-23))
27.866022930551416

```

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [May 3, 2020, 11:36am UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/6 "2020-05-03T11:36:15Z")

</div>

Another alternative: [SpecialFunctions.jl/elliptic.jl at 37e5999c5fe0f778f2de2e41b6beeff339a21cb8 · JuliaMath/SpecialFunctions.jl · GitHub](https://github.com/JuliaMath/SpecialFunctions.jl/blob/37e5999c5fe0f778f2de2e41b6beeff339a21cb8/src/elliptic.jl)

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [May 3, 2020, 11:37am UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/7 "2020-05-03T11:37:53Z")

</div>

This precision loss appears to be [a known issue](https://github.com/nolta/Elliptic.jl/issues/11) with the package, but it has not been addressed in two-and-a-half years.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [May 3, 2020, 11:42am UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/8 "2020-05-03T11:42:36Z")

</div>

```julia
julia> using ArbNumerics
julia> setextrabits(64); setprecision(ArbFloat, bits=256);
julia> x = 1 - ArbFloat(10)^(-23)
0.99999999999999999999999
julia> elliptic_k(x)
27.866022930551415985041433136843867902103629991741788896439319076571988943551

```

---

<div class="post-metadata">

### Author: ![EHBaozi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ehbaozi/32/14854_2.png) [@EHBaozi](https://discourse.julialang.org/u/EHBaozi)
#### Post date: [May 3, 2020, 12:13pm UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/9 "2020-05-03T12:13:26Z")

</div>

Nice! This should be the one then.

Also, @StarecZosima could you kindly check whether your Mathematica is doing something strange? Mine gives the same answer of ~27.866.

---

<div class="post-metadata">

### Author: ![StarecZosima](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@StarecZosima](https://discourse.julialang.org/u/StarecZosima)
#### Post date: [May 3, 2020, 6:43pm UTC](https://discourse.julialang.org/t/special-functions-elliptic-jl-and-arbitrary-precision/38658/10 "2020-05-03T18:43:46Z")

</div>

Thanks to all very much, I got it working now, good to learn about these additional packages.  
@EHBaozi, I inadvertendly typed 10^-25 and not 10^-23 when checking. Indeed in Mathemica

```
 EllipticK [1-10^-23] = 27.866022930551415985

```

so matching what you got and Julia’s ArbNumerics.

Thanks once more
