Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. (*
  2.    adapted to Oberon-07 by 0CodErr, KolibriOS team
  3.                                                    *)
  4. (* Print first 'PRINT' digits of 'e'.
  5.  *
  6.  * Originally written in Pascal by Scott Hemphill
  7.  * Rewritten in Modula-2 and modified by Andrew Cadach
  8.  *
  9.  *)
  10.  
  11. MODULE exp;
  12.  
  13. IMPORT In, Out, Console;
  14.  
  15.  
  16. CONST
  17.     PRINT = 1024;
  18.     DIGITS = PRINT + (PRINT + 31) DIV 32;
  19.  
  20.  
  21. TYPE
  22.     number = ARRAY DIGITS + 1 OF INTEGER;
  23.  
  24.  
  25. VAR
  26.     s, x: number;
  27.     xs, i: INTEGER;
  28.  
  29.  
  30. PROCEDURE init (VAR x: number; n: INTEGER);
  31. VAR
  32.     i: INTEGER;
  33.  
  34. BEGIN
  35.     x[0] := n;
  36.     FOR i := 1 TO DIGITS DO x[i] := 0 END
  37. END init;
  38.  
  39.  
  40. PROCEDURE divide (VAR x: number; xs, n: INTEGER;
  41.                   VAR y: number; VAR ys: INTEGER);
  42. VAR
  43.     i, c: INTEGER;
  44.  
  45. BEGIN
  46.     c := 0;
  47.     FOR i := xs TO DIGITS DO
  48.         c := 10 * c + x[i];
  49.         y[i] := c DIV n;
  50.         c := c MOD n
  51.     END;
  52.     ys := xs;
  53.     WHILE (ys <= DIGITS) & (y[ys] = 0) DO INC(ys) END
  54. END divide;
  55.  
  56.  
  57. PROCEDURE add (VAR s, x: number; xs: INTEGER);
  58. VAR
  59.     i, c: INTEGER;
  60. BEGIN
  61.     c := 0;
  62.     FOR i := DIGITS TO xs BY -1 DO
  63.         c := c + s[i] + x[i];
  64.         IF c >= 10 THEN
  65.             s[i] := c - 10;
  66.             c := 1
  67.         ELSE
  68.             s[i] := c;
  69.             c := 0
  70.         END
  71.     END;
  72.     i := xs;
  73.     WHILE c # 0 DO
  74.         DEC(i);
  75.         c := c + s[i];
  76.         IF c >= 10 THEN
  77.             s[i] := c - 10;
  78.             c := 1
  79.         ELSE
  80.             s[i] := c;
  81.             c := 0
  82.         END
  83.     END
  84. END add;
  85.  
  86.  
  87. BEGIN
  88.     Console.open;
  89.  
  90.     init(s, 0);
  91.     init(x, 1);
  92.     xs := 0;
  93.     add(s, x, xs);
  94.     i := 0;
  95.     REPEAT
  96.         INC(i);
  97.         divide(x, xs, i, x, xs);
  98.         add(s, x, xs);
  99.     UNTIL xs > DIGITS;
  100.     Out.Ln;
  101.     Out.String ("   e = ");
  102.     Out.Char (CHR(s[0] + ORD("0")));
  103.     Out.Char (".");
  104.     FOR i := 1 TO PRINT DO
  105.         Out.Char (CHR(s[i] + ORD("0")));
  106.         IF i MOD 64 = 0 THEN
  107.             Out.Ln;
  108.             Out.Int (i, 5);
  109.             Out.String ("    ")
  110.         END
  111.     END;
  112.     Out.Ln;
  113.     Out.Ln;
  114.     In.Ln;
  115.  
  116.     Console.exit(TRUE)
  117. END exp.