# FiniteContinuedFractions.jl - a number type for rational numbers

**URL:** https://discourse.julialang.org/t/finitecontinuedfractions-jl-a-number-type-for-rational-numbers/123007
**Category:** Package Announcements
**Tags:** package, announcement, rationals, math, juliamath
**Created:** [November 24, 2024, 3:54am UTC](https://discourse.julialang.org/t/finitecontinuedfractions-jl-a-number-type-for-rational-numbers/123007 "2024-11-24T03:54:18Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [November 24, 2024, 3:54am UTC](https://discourse.julialang.org/t/finitecontinuedfractions-jl-a-number-type-for-rational-numbers/123007/1 "2024-11-24T03:54:18Z")

</div>

A package that provides the `FiniteContinuedFraction` type, an alternative to `Rational`. Both subtype `Real`.

> **[Neven Sajko / FiniteContinuedFractions.jl · GitLab](https://gitlab.com/nsajko/FiniteContinuedFractions.jl)**
>
> A package for the Julia programming language. Implements an alternative to the built-in Rational type. Based on the concept of finite simple continued fractions.

I don’t know if `FiniteContinuedFraction` is ever a good replacement for `Rational` in package code, seeing as it’s not _isbits_ so constructing an instance is expensive. However perhaps it could be fun for interactive play with continued fractions in the REPL.

Relevant Wikipedia pages:

> **[Continued fraction](https://en.wikipedia.org/wiki/Continued_fraction)**
>
> A continued fraction is a mathematical expression that can be writen as a fraction with a denominator that is a sum that contains another simple or continued fraction. Depending on whether this iteration terminates with a simple fraction or not, the continued fraction is finite or infinite.
> Different fields of mathematics have different terminology and notation for continued fraction. In number theory the standard unqualified use of the term continued fraction refers to the special case where ...

> **[Simple continued fraction](https://en.wikipedia.org/wiki/Simple_continued_fraction)**
>
> A simple or regular continued fraction is a continued fraction with numerators all equal one, and denominators built from a sequence 
>   
>     
>       
> {
>         
> a
>           
> i
>           
>         
> }
>       
>     
> {\\displaystyle \\{a\_{i}\\}}
>   
> of integer numbers. The sequence can be finite or infinite, resulting in a finite (or terminated) continued fraction like
> or an infinite continued fraction like
> Typically, such a continued fraction is obtained through ...

Example usage:

```julia-repl
julia> using FiniteContinuedFractions

julia> FiniteContinuedFraction(8 // 5)
1 + 1//(1 + 1//(1 + 1//2))

julia> inv(ans)
1//(1 + 1//(1 + 1//(1 + 1//2)))

julia> 1 / ans
8//5

```

A note on the implementation: while binary operations return `Rational`, the arithmetic is actually done without converting the input argumens to `Rational`. Rather, the Gosper/HAKMEM-inspired algorithm used produces the reduced numerator and denominator directly from the continued fractions.

It should also be possible to somewhat extend support to quadratic irrationals (the solutions of quadratic equations), in addition to the rationals, by allowing representation of (eventually) periodic (simple) continued fractions. Not sure how sensible would that be, though, considering that this class of numbers (union of the rationals and quadratic irrationals) isn’t closed for some common operations.

Looking forward to any thoughts. Also, I wonder if it’d be possible to move the package from my Gitlab to the JuliaMath Github to make it more discoverable, before registering it?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [November 24, 2024, 7:36am UTC](https://discourse.julialang.org/t/finitecontinuedfractions-jl-a-number-type-for-rational-numbers/123007/2 "2024-11-24T07:36:04Z")

</div>

How does it compare with RealContinuedFractions.jl ?

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [November 24, 2024, 12:18pm UTC](https://discourse.julialang.org/t/finitecontinuedfractions-jl-a-number-type-for-rational-numbers/123007/3 "2024-11-24T12:18:11Z")

</div>

They’re quite different.

| FiniteContinuedFraction.jl | RealContinuedFractions.jl |
| --- | --- |
| The only exported name is the type `FiniteContinuedFraction`. | The main exported name is the type `ContinuedFraction`. |
| `FiniteContinuedFraction` is a number, in fact it subtypes `Real`. | `ContinuedFraction` isn’t a number, it seems to be somewhat of a container, though. Although it’s not an iterator and doesn’t support indexing, so its not a full featured collection, either. |
| Tries to play nice with the ecosystem, exposing its functionality by adding methods to pre-existing functions and constructors. | RealContinuedFractions.jl exports several new functions. |
| Doesn’t provide any access to the convergents. It could be considered if there is desire though, I just didn’t see need for it. | Provides convergent-related functions. |
| Supports most conversion, constructors, promotion, arithmetic and predicates that would be expected from such a (rational) number. | Doesn’t seem to support any such operations. |

EDIT:

> Supports most conversion, constructors, promotion, arithmetic and predicates that would be expected from such a (rational) number.

TBH there’s currently no conversion from/to many relevant types. Perhaps I should add support for conversion with `AbstractFloat`, `AbstractIrrational`, etc. EDIT2: IMO this should be supported via a `rationalize`-like function. I can’t add relevant methods to `rationalize` as far as I understand the `rationalize` doc string, so I’ll add a `rationalize_to_continued_fraction` function.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [November 24, 2024, 10:37pm UTC](https://discourse.julialang.org/t/finitecontinuedfractions-jl-a-number-type-for-rational-numbers/123007/4 "2024-11-24T22:37:59Z")

</div>

Good for project Euler.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [December 8, 2024, 12:01pm UTC](https://discourse.julialang.org/t/finitecontinuedfractions-jl-a-number-type-for-rational-numbers/123007/5 "2024-12-08T12:01:38Z")

</div>

The latest commit now supports signed infinities and signed zeros. Signed infinity brings `FiniteContinuedFraction` to feature parity with `Rational`, while allowing signed zero avoids the bug/loss of information in taking the inverse of negative infinity (as present with `Rational`).

Support for infinity allows simplifying the representation and implementation. In particular, the empty continued fraction is infinity, so zero can be simply the inverse of infinity. That said, getting the signs right in arithmetic requires some special casing 😕.

Another change from the last post is that binary arithmetic operations between continued fractions return continued fractions, instead of `Rational`.

## Roadmap

I’m looking into:

- Allowing arbitrary `AbstractVector` values for storing the partial denominators, instead of just `GenericMemory`. Given an _isbits_ vector, the continued fraction would likewise be _isbits_, which should allow avoiding all heap allocation: [relax type parameter from `GenericMemory` to `AbstractVector` (#18) · Issues · Neven Sajko / FiniteContinuedFractions.jl · GitLab](https://gitlab.com/nsajko/FiniteContinuedFractions.jl/-/issues/18)
- Packing the partial denominators into a tighter representation, so, e.g., multiple partial denominators could fit into a single machine word: [compression or packed representation? (#16) · Issues · Neven Sajko / FiniteContinuedFractions.jl · GitLab](https://gitlab.com/nsajko/FiniteContinuedFractions.jl/-/issues/16)
