Click here to Skip to main content
16,004,587 members
Articles / Fractions
Tip/Trick

A Simple Continued Fraction Calculator

Rate me:
Please Sign up or sign in to vote.
4.41/5 (8 votes)
24 Jul 2024MIT2 min read 8.6K   330   11   4
Basic operations (-, +, *, /) of two finite simple continued fractions
The calculator does basic operations (-, +, *, /) of two finite simple continued fractions operands and shows the result, or the operands, in a browser control (with MathJax).

Image 1

Introduction

I am new to simple continued fractions (SCF) but there is a lot of literature about them out there 
and, as it seems, many mathematical applications, including fractals.

Background

CF class is the only class to do the four basic operations (-, +, *, /). I have followed two documents from S. Mugassabi [1] [2] to perform them.

Using the code

Instantiation can be done in three ways, depending on the arguments passed:
- a numerator and a denominator
- a double
- a List(Of BigInteger) object

Passing a Double, it is converted into a numerator and a denominator equal to one. If the double value is not integer, it is multiplied by 10 (as well as the denominator) as much as needed until it is converted into an integer. This is because the terms are stored as Biginteger and if not the decimal places would be lost. For example, if we instantiate:

VB
Dim operandA as new CF(1.5

1.5 is converted to a numerator (=15) and a denominator (=10) and simplified to 3/2 (=1+1/2). The integer part is one and the fraction is 1/2. So, it will define two terms [1; 2] = 1+1/2

Passing a list of BigInteger is the equivalent to define the SCF. For example, if the list is {1,2,3} the SCF will look like:

           1
1 + --------------
             1
      2  + -----
             3

To operate just do the math as with numbers:

VB.NET
Dim operatorA = new CF(1.5)
Dim operatorB = new CF(0.5)
Dim operatorC = operatorA + operatorB
Console.WriteLine(operatorC.ToString)
' will show [1; 1] = 2/1 = 2  (the SCF = num./denom. = ToDouble())

 

Limitations

The calculator can operate small or big Double values, for example 1e-100 or 1e+100, but the ToDouble() method will be limited, of course, to Double maximum and minimum.

This is not the case of method ToNumDen() that returns a BigInteger array of two elements: the calculated numerator and denominator of all the SCF.

History

Version 1.0.3 (2024/07/12)

Some fixes, including the CF class was sometimes generating a single term.

Version 1.0.5 (2024/07/21)

There was a fix in the particular case of addition and subtraction where the result had two terms. For example, 1/3+1/6.

Released a Console (Cli) version.

Version 1.0.6.0 (2024/07/24)

There was a fix in some particular cases of multiplication and division where the shorter operand had less than three terms.

Showing periodic continued fractions has been improved.

References

[1]. Mugassabi, S. I., & Mistiri, F. (2015). “The Elementary Arithmetic Operators of Continued Fraction”.

[2]. Mugassabi, S. I., & Amsheri, S., M. (2019). “The Multiplication and Division of Simple Continued Fractions”.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAnother vote of 5 Pin
FredWah19-Jul-24 3:10
FredWah19-Jul-24 3:10 
AnswerRe: Another vote of 5 Pin
Xavier Junqué i de Fortuny19-Jul-24 4:13
Xavier Junqué i de Fortuny19-Jul-24 4:13 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA3-Jul-24 17:27
professionalȘtefan-Mihai MOGA3-Jul-24 17:27 
GeneralRe: My vote of 5 Pin
Xavier Junqué i de Fortuny3-Jul-24 19:35
Xavier Junqué i de Fortuny3-Jul-24 19:35 
Thank you so much Ștefan. Currently I am starting to read Bill Gosper's paper, so to try to do arithmetic with non-regular continued fractions.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.