source: to-imperative/trunk/java/refal/refal/plus/Rational.rf @ 3999

Last change on this file since 3999 was 3999, checked in by orlov, 12 years ago
  • Added Rational library from samples.
  • Property svn:eol-style set to native
File size: 1.7 KB
Line 
1// vim: set et ts=2 sw=2 :
2
3$use Access;
4$use Arithm;
5$use Convert;
6$use Compare;
7
8$func QSimplify s s = t;
9$func QConvert t = s s;
10
11QAdd tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
12  <QSimplify <Add <Mult sX1 sY2> <Mult sX2 sY1>> <Mult sX2 sY2>>;
13
14QSub tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
15  <QSimplify <Sub <Mult sX1 sY2> <Mult sX2 sY1>> <Mult sX2 sY2>>;
16
17QMul tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
18  <QSimplify <Mult sX1 sY1> <Mult sX2 sY2>>;
19
20QDiv tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
21  {
22    <Compare (sY1) (0)> : '<', <Mult sY1 -1> <Mult sY2 -1>;
23    sY1 sY2;
24  } :: sY1 sY2,
25  <QSimplify <Mult sX1 sY2> <Mult sX2 sY1>>;
26
27QCompare tX tY, <QConvert tX> :: sX1 sX2, <QConvert tY> :: sY1 sY2,
28  <Compare (<Mult sX1 sY2>) (<Mult sX2 sY1>)>;
29
30QToInt tX, <DivRem <QConvert tX>> :: sQ sR, sQ;
31
32QToString sN tX, <QConvert tX> :: s1 s2,
33  <DivRem s1 s2> :: sQ sR,
34  {
35    <Compare (s1) (0)> :'<', { sQ : 0, '-'; /*empty*/;} :: eSing, eSing sQ <Mult sR -1>;
36    sQ sR;
37  } :: eOut sR,
38  sN sR /*empty*/
39  $iter {
40     <DivRem <Mult sR 10> s2> :: sQ sR,
41     <Sub sN 1> sR eOut2 sQ;
42  } :: sN sR eOut2, sN : 0 =
43  'T' eOut2
44  $iter {
45    eOut2 : eOut3 0, 'T' eOut3;
46    'F' eOut2;
47  } :: sP eOut2, sP : 'F' =
48  {
49    eOut2 : /*empty*/, eOut;
50    eOut '.' eOut2;
51  } :: eOut, <ToChars eOut>;
52
53QFromString \{
54  eIn1 '.' eIn2 =
55  {eIn1 : /*empty*/, '0'; eIn1;} :: eIn1,
56  <Length eIn2> 1
57  $iter <Sub sN 1> <Mult sM 10> ::
58  sN sM, sN : 0 =
59  \{
60    eIn1 : e '-' e = <QSub <ToInt eIn1> <QDiv <ToInt eIn2> sM>>;
61    <QAdd <ToInt eIn1> <QDiv <ToInt eIn2> sM>>;
62  };
63
64  eIn1 = (<ToInt eIn1> 1);
65};
66
67
68QSimplify s1 s2, <Gcd s1 s2> :: s3, (<Div s1 s3> <Div s2 s3>);
69
70QConvert {
71  (s1 s2) = s1 s2;
72  s1 = s1 1;
73};
Note: See TracBrowser for help on using the repository browser.