Files
mercury/tests/hard_coded/rational_test.m
Zoltan Somogyi 33eb3028f5 Clean up the tests in half the test directories.
tests/accumulator/*.m:
tests/analysis_*/*.m:
tests/benchmarks*/*.m:
tests/debugger*/*.{m,exp,inp}:
tests/declarative_debugger*/*.{m,exp,inp}:
tests/dppd*/*.m:
tests/exceptions*/*.m:
tests/general*/*.m:
tests/grade_subdirs*/*.m:
tests/hard_coded*/*.m:
    Make these tests use four-space indentation, and ensure that
    each module is imported on its own line. (I intend to use the latter
    to figure out which subdirectories' tests can be executed in parallel.)

    These changes usually move code to different lines. For the debugger tests,
    specify the new line numbers in .inp files and expect them in .exp files.
2015-02-14 20:14:03 +11:00

67 lines
1.5 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
%
% 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 int.
:- import_module integer.
:- import_module io.
:- import_module list.
:- import_module rational.
:- import_module string.
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) + rational__reciprocal(CF) :-
CF = cf2rat(Ns).
% 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)]
).