FiniteContinuedFractions.jl - a number type for rational numbers

nsajko
Sun, 24 Nov 2024 03:54:18 +0000
Package Announcements

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

GitLab

Neven Sajko / FiniteContinuedFractions.jl · GitLab

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:

en.wikipedia.org

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 ...

en.wikipedia.org

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> 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?

5 posts - 3 participants