Continued fraction/Arithmetic/Construct from rational number: Difference between revisions
Continued fraction/Arithmetic/Construct from rational number (view source)
Revision as of 18:31, 27 February 2023
, 1 year ago→{{header|ATS}}
m (→{{header|ATS}}) |
|||
Line 293:
=={{header|ATS}}==
===Using a non-linear closure===
In the first example solution, I demonstrate concretely that the method of integer division matters. I use 'Euclidean division' (see ACM Transactions on Programming Languages and Systems, Volume 14, Issue 2, pp 127–144. https://doi.org/10.1145/128861.128862) and show that you get a different continued fraction if you start with (-151)/77 than if you start with 151/(-77). I verified that both continued fractions do equal -(151/77).▼
A closure is used to generate the next term (or '''None()''') each time it is called. The integer type is mostly arbitrary.
▲In the first example solution, I demonstrate concretely that the method of integer division matters. I use 'Euclidean division' (see ACM Transactions on Programming Languages and Systems, Volume 14, Issue 2, pp 127–144. https://doi.org/10.1145/128861.128862) and show that you get a different continued fraction if you start with (-151)/77 than if you start with 151/(-77). I verified that both continued fractions do equal -(151/77).
<syntaxhighlight lang="ats">
Line 495 ⟶ 491:
</pre>
=== Using a non-linear closure and multiple precision numbers ===
For this you need the [https://sourceforge.net/p/chemoelectric/ats2-xprelude '''ats2-xprelude'''] package. I start with octuple precision (IEEE binary256) approximations to the square root of 2 and 22/7.
Line 641 ⟶ 637:
</pre>
===Using a non-linear closure and memoizing in an array===
This example solution was written specifically with the idea of providing a general representation of possibly infinitely long continued fractions. Terms can be obtained arbitrarily, by their indices. One obtains '''Some term''' if the term is finite, or '''None()''' if the term is infinite.
One drawback is that, because a continued fraction is memoized, and its terms are generated as needed, the data structure may need to be updated. Therefore it must be stored in a mutable location, such as a '''var''' or '''ref'''.
<syntaxhighlight lang="ats">(*------------------------------------------------------------------*)
|