for review: tests for rational.m

Bert Thompson aet at hydra.cs.mu.oz.au
Wed Apr 15 19:35:38 AEST 1998


Gday people,

This is quick...

------------------------------------------------------------

Estimated hours taken: 0.5

Added simple tests for library/rational.m.

general/rational_test.m:
	Test code.
general/rational_test.exp:
	Expected output.
general/Mmakefile:
	Added rational_test.
general/.cvsignore:
	Added rational_test.

CVS: ----------------------------------------------------------------------
CVS: Enter Log.  Lines beginning with `CVS: ' are removed automatically
CVS: 
CVS: Committing in .
CVS: 
CVS: Modified Files:
CVS: 	.cvsignore Mmakefile 
CVS: Added Files:
CVS: 	rational_test.exp 
CVS: ----------------------------------------------------------------------
------------------------------------------------------------
	% 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)]
	).
------------------------------------------------------------

Index: .cvsignore
===================================================================
RCS file: /home/mercury1/repository/tests/general/.cvsignore,v
retrieving revision 1.13
diff -u -r1.13 .cvsignore
--- 1.13	1998/04/15 08:14:01
+++ .cvsignore	1998/04/15 09:28:51
@@ -13,3 +13,4 @@
 mode_inf
 intermod_type
 integer_test
+rational_test
Index: Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/general/Mmakefile,v
retrieving revision 1.14
diff -u -r1.14 Mmakefile
--- 1.14	1998/04/15 08:14:02
+++ Mmakefile	1998/04/15 09:28:37
@@ -53,6 +53,7 @@
 		parse_list \
 		petdr1 \
 		prune_switch \
+		rational_test \
 		semidet_map \
 		set_test \
 		string_format_test \



More information about the developers mailing list