Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2): Difference between revisions

Line 1,822:
 
===Using multiple precision numbers===
{{trans|Standard ML}}
 
For this program you need [https://sourceforge.net/p/chemoelectric/ats2-xprelude ats2-xprelude].
 
The program closely follows the Standard ML code, so one can compare the two languages with each other. They have similar syntaxes, but are very different. Notice, for instance, that ATS has overloads, whereas SML does not. (SML has signatures with respective namespaces.) In ATS, a function is not a closure unless you explicitly make it one, whereas in SML no special notation is needed. And so on.
 
ATS is translated to C, and its functions (except closures) are essentially just C functions. One can write Arduino code and Linux kernel modules in ATS, because ATS is essentially an elaborate way to write C. Nevertheless, there is enough similarity between ATS and Standard ML to easily translate the SML code for this task to ATS.
 
I have broken the program into three files, to demonstrate what an ATS program might look like, if it were broken into separately compiled parts.
 
The first file is an "interface" specification for a <code>continued_fraction</code> data type. The file is called <code>continued_fraction.sats</code>:
 
<syntaxhighlight lang="ats">
(* "Static" file. (Exported declarations.) *)
 
(* To set up a predictable name-mangling scheme: *)
#define ATS_PACKNAME "rosetta-code.continued_fraction"
 
(* Load declarations from ats2-xprelude: *)
#include "xprelude/HATS/xprelude_sats.hats"
staload "xprelude/SATS/exrat.sats"
 
(* A term_generator thunk generates terms, which a continued_fraction
data structure memoizes. The internals of continued_fraction are
not exposed here. It is an abstract type, the size of (but not the
same type as) a pointer. SIDE NOTE: In ATS2, we get a conventional
function, rather than a closure, unless we say explicitly that we
want a closure; "cloref1" means a particular kind of closure one
often uses when linking the program with Boehm GC. *)
typedef term_generator = () -<cloref1> Option exrat
abstype continued_fraction = ptr
 
(* Create a continued fraction. *)
fn continued_fraction_make : term_generator -> continued_fraction
 
(* Does the indexed term exist? *)
fn term_exists : (continued_fraction, intGte 0) -> bool
 
(* Retrieve the indexed term. Raise an exception if there is no such
term. The precedence of the overload must exceed that of an
overload that is in the prelude. (To see what I mean, try removing
the "of 1".) *)
val get_term_exn : (continued_fraction, intGte 0) -> exrat
overload [] with get_term_exn of 1
 
(* Use a continued_fraction as a term_generator thunk. *)
fn continued_fraction_to_term_generator :
continued_fraction -> term_generator
 
(* Get a human-readable string. *)
val default_max_terms : ref (intGte 1)
fn continued_fraction_to_string_given_max_terms :
(continued_fraction, intGte 1) -> string
fn continued_fraction_to_string_default_max_terms :
continued_fraction -> string
overload continued_fraction_to_string with
continued_fraction_to_string_given_max_terms
overload continued_fraction_to_string with
continued_fraction_to_string_default_max_terms
overload cf2string with continued_fraction_to_string
 
(* Make a continued_fraction for an integer. *)
fn int_to_continued_fraction : int -> continued_fraction
overload i2cf with int_to_continued_fraction
 
(* Make a continued_fraction for a rational number. *)
fn exrat_to_continued_fraction : exrat -> continued_fraction
fn rational_to_continued_fraction :
(int, [d : int | d != 0] int d) -> continued_fraction
overload r2cf with exrat_to_continued_fraction
overload r2cf with rational_to_continued_fraction
 
(* Make a continued_fraction with one term repeated forever. *)
fn continued_fraction_make_constant_term : int -> continued_fraction
overload constant_term_cf with continued_fraction_make_constant_term
 
(* Make a continued fraction via binary arithmetic operations. (I have
not bothered here to implement ng4, although one likely would wish
to have ng4 as well.) *)
(* The @() denotes an unboxed tuple. A boxed tuple is written '() and
would be put in the heap. An unboxed tuple may also be written
without the @-sign, but then the compiler might confuse it with,
for instance, an argument list. (ATS2 has conventional argument
lists that are distinct from tuples, and supports
call-by-reference, where an argument is mutable.) *)
typedef ng8 = @(exrat, exrat, exrat, exrat,
exrat, exrat, exrat, exrat)
typedef continued_fraction_binary_op_cloref =
(continued_fraction, continued_fraction) -<cloref1> continued_fraction
(* ng8_make_int takes ONE argument, which is a tuple. *)
val ng8_make_int : @(int, int, int, int, int, int, int, int) -> ng8
val ng8_stopping_processing_threshold : ref exrat
val ng8_infinitization_threshold : ref exrat
val ng8_apply : ng8 -> continued_fraction_binary_op_cloref
val ng8_apply_add : continued_fraction_binary_op_cloref
val ng8_apply_sub : continued_fraction_binary_op_cloref
val ng8_apply_mul : continued_fraction_binary_op_cloref
val ng8_apply_div : continued_fraction_binary_op_cloref
(* The following two are regular functions, not closures. They are
translated by the ATS compiler into ordinary C functions. *)
fn ng8_apply_neg : continued_fraction -> continued_fraction
fn ng8_apply_pow : (continued_fraction, int) -> continued_fraction
overload + with ng8_apply_add
overload - with ng8_apply_sub
overload * with ng8_apply_mul
overload / with ng8_apply_div
overload ~ with ng8_apply_neg
overload ** with ng8_apply_pow
 
(* Miscellanous continued fractions. *)
val zero : continued_fraction
val one : continued_fraction
val two : continued_fraction
val three : continued_fraction
val four : continued_fraction
//
val one_fourth : continued_fraction
val one_third : continued_fraction
val one_half : continued_fraction
val two_thirds : continued_fraction
val three_fourths : continued_fraction
//
val golden_ratio : continued_fraction
val silver_ratio : continued_fraction
val sqrt2 : continued_fraction
val sqrt5 : continued_fraction
</syntaxhighlight>
 
=={{header|C}}==
1,448

edits