# Are Rationals that expensive?

**URL:** <https://discourse.julialang.org/t/are-rationals-that-expensive/1374>\
**Category:** New to Julia\
**Created:** [January 9, 2017, 5:32pm UTC](https://discourse.julialang.org/t/are-rationals-that-expensive/1374 "2017-01-09T17:32:48Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [January 12, 2017, 12:04am UTC](https://discourse.julialang.org/t/are-rationals-that-expensive/1374/21 "2017-01-12T00:04:38Z")

</div>

Memory allocation is expected for rationals because `BigInt` isn’t a machine type.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 12, 2017, 2:04am UTC](https://discourse.julialang.org/t/are-rationals-that-expensive/1374/22 "2017-01-12T02:04:14Z")

</div>

> [@abulak](#):
>
> Hence I was surprised that code performing well on Floats suffers from “unexpected memory allocation” after switching to rationals…

You can think of `BigInt`s as pointers to dynamically-sized numbers of bits which hold the information for how big that integer is. So as with anything with pointers involved (arrays), you’ll be allocating a lot of memory to make it work. That’s why most math is not done with arbitrary precision arithmetic (of which `Rational{BigInt}` is as far as it gets): it’s very expensive and there’s no way for it to not be (unless you have an idea… that would be a great Julia library!)

If you want fast arbitrary precision arithmetic, the best setup is likely to use ArbFloats.

> **[GitHub - JuliaArbTypes/ArbFloats.jl: Much faster than BigFloat at precisions...](https://github.com/JuliaArbTypes/ArbFloats.jl)**
>
> Much faster than BigFloat at precisions up to 3,500 bits (1050 digits) - GitHub - JuliaArbTypes/ArbFloats.jl: Much faster than BigFloat at precisions up to 3,500 bits (1050 digits)

They will be much faster than BigFloats until you get up to some absurd levels of precision. They also allow for doing interval arithmetic to show uncertainties, but as @stevangj has already mentioned, interval arithmetic is known to have very pessimistic error bounds and so it is really only applicable in some domains.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [January 12, 2017, 10:12am UTC](https://discourse.julialang.org/t/are-rationals-that-expensive/1374/23 "2017-01-12T10:12:29Z")

</div>

For `Rational{Int}` performance, see [issue #11522](https://github.com/JuliaLang/julia/issues/11522). These changes could probably be made soon, if someone is keen.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [March 16, 2017, 8:29pm UTC](https://discourse.julialang.org/t/are-rationals-that-expensive/1374/24 "2017-03-16T20:29:08Z")

</div>

@dpsanders, Thanks for the tip, I transformed the problem to use ValidatedNumerics!

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 17, 2017, 1:17am UTC](https://discourse.julialang.org/t/are-rationals-that-expensive/1374/25 "2017-03-17T01:17:37Z")

</div>

Great! Let me know if you have any questions. I’d be very interested to see your code.

---

<div class="post-metadata">

**Author:** ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)\
**Post date:** [April 25, 2017, 8:22pm UTC](https://discourse.julialang.org/t/are-rationals-that-expensive/1374/26 "2017-04-25T20:22:25Z")

</div>

@dpsanders  
Paper describing what we did is here:

> **[Certifying numerical estimates of spectral gaps](https://arxiv.org/abs/1703.09680v2)**
>
> We establish a lower bound on the spectral gap of the Laplace operator on special linear groups using conic optimisation. In particular, this provides a constructive (but computer assisted) proof that these groups have Kazhdan property (T). A...

Relevant code is located here:  
[https://git.wmi.amu.edu.pl/kalmar/PropertyT.jl/src/master/src/checksolution.jl](https://git.wmi.amu.edu.pl/kalmar/PropertyT.jl/src/master/src/checksolution.jl)

comments, questions are welcome!  
btw. how can I cite ValidatedNumerics package?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 25, 2017, 11:35pm UTC](https://discourse.julialang.org/t/are-rationals-that-expensive/1374/27 "2017-04-25T23:35:00Z")

</div>

Great, thanks, I’ll take a look.

Good question about citing VN, I’ll get back to you about that.

[Previous page](https://discourse.julialang.org/t/are-rationals-that-expensive/1374.md?page=1)
