Files
mercury/tests/hard_coded/rational_test.m
Bert Thompson 30ef37923c Moved tests of library/{integer,rational}.m from tests/general to
Estimated hours taken: 0.1

Moved tests of library/{integer,rational}.m from tests/general to
tests/hard_coded since the library modules and their tests cannot
be compiled under NU-Prolog.

general/Mmakefile:
general/.cvsignore:
	Removed references to integer_test and rational_test.
general/integer_test.m:
general/integer_test.exp:
general/rational_test.exp:
general/rational_test.m:
	File removed.
hard_coded/Mmakefile:
hard_coded/.cvsignore:
	Added references to integer_test and rational_test.
hard_coded/integer_test.m:
hard_coded/integer_test.exp:
hard_coded/rational_test.exp:
hard_coded/rational_test.m:
	File added.
1998-04-16 12:00:32 +00:00

63 lines
1.2 KiB
Mathematica

% simple test of arbitrary precision rationals.
:- module rational_test.
:- interface.
:- import_module io.
:- pred main(io:state, io:state).
:- mode main(di, uo) is det.
:- implementation.
:- import_module rational, int, integer, string, list, io.
main -->
io:write_string(rat2s(cf2rat(root2_cf(80)))), io:nl,
io:write_string(rat2s(cf2rat(e_cf(20)))), io:nl.
:- func rat2s(rational) = string.
rat2s(Rat) = S :-
Num = numer(Rat),
Den = denom(Rat),
NS = integer:to_string(Num),
ND = integer:to_string(Den),
string:append_list([NS," / ",ND],S).
:- func cf2rat(list(int)) = rational.
cf2rat([]) = one.
cf2rat([N|Ns]) = rational(N,1) + inverse(CF) :-
CF = cf2rat(Ns).
:- func inverse(rational) = rational.
inverse(Rat) = rational(1,1) / Rat.
% Continued fraction expansion of Euler's constant `e'.
:- func e_cf(int) = list(int).
e_cf(N) = CF :-
list:append([2,1,2],Rest,CF),
Rest = e_aux(N,4).
:- func e_aux(int, int) = list(int).
e_aux(N,A) = List :-
( N =< 0 ->
List = []
;
List = [1,1,A|e_aux(N-1,A+2)]
).
:- func root2_cf(int) = list(int).
root2_cf(N) = [1|Rest] :-
Rest = n_of(N,2).
:- func n_of(int, T) = list(T).
n_of(N, A) =
( N =< 0 ->
[]
;
[A|n_of(N-1,A)]
).