Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (* Print first 'PRINT' digits of 'e'.
  2.  *
  3.  * Originally written in Pascal by Scott Hemphill
  4.  * Rewritten in Modula-2 and modified by Andrew Cadach
  5.  *
  6.  *)
  7.  
  8. <*+ STORAGE *>    (* default memory management is used *)
  9. <*- CHECKINDEX *>
  10. MODULE exp;
  11.  
  12. IMPORT InOut;
  13.  
  14. CONST
  15.    PRINT = 1024;
  16.    DIGITS = PRINT + (PRINT+31) DIV 32;
  17.  
  18. TYPE
  19.    number = ARRAY [0..DIGITS] OF CARDINAL;
  20.  
  21. VAR
  22.    s, x: POINTER TO number;
  23.    xs, i: CARDINAL;
  24.  
  25. PROCEDURE init (VAR x: number; n: CARDINAL);
  26. VAR i: CARDINAL;
  27. BEGIN
  28.    x[0] := n;
  29.    FOR i := 1 TO DIGITS DO x[i] := 0; END
  30. END init;
  31.  
  32. PROCEDURE divide (VAR x: number; xs, n: CARDINAL;
  33.                   VAR y: number; VAR ys: CARDINAL);
  34. VAR
  35.    i, c: CARDINAL;
  36. BEGIN
  37.    c := 0;
  38.    FOR i := xs TO DIGITS DO
  39.       c := 10*c + x[i];
  40.       y[i] := c DIV n;
  41.       c := c MOD n
  42.    END;
  43.    ys := xs;
  44.    WHILE (ys <= DIGITS) & (y[ys] = 0) DO INC (ys) END
  45. END divide;
  46.  
  47. PROCEDURE add (VAR s, x: number; xs: CARDINAL);
  48. VAR
  49.    i, c: CARDINAL;
  50. BEGIN
  51.    c := 0;
  52.    FOR i := DIGITS TO xs BY -1 DO
  53.       INC (c,  s[i] + x[i]);
  54.       IF c >= 10 THEN
  55.          s[i] := c - 10;
  56.          c := 1
  57.       ELSE
  58.          s[i] := c;
  59.          c := 0
  60.       END
  61.    END;
  62.    i := xs;
  63.    WHILE c # 0 DO
  64.       DEC (i);
  65.       INC (c, s[i]);
  66.       IF c >= 10 THEN
  67.          s[i] := c - 10;
  68.          c := 1
  69.       ELSE
  70.          s[i] := c;
  71.          c := 0
  72.       END
  73.    END
  74. END add;
  75.  
  76. BEGIN
  77.    NEW (s);
  78.    NEW (x);
  79.    init (s^, 0);
  80.    init (x^, 1);
  81.    xs := 0;
  82.    add (s^, x^, xs);
  83.    i := 0;
  84.    REPEAT
  85.       INC (i);
  86.       divide (x^, xs, i, x^, xs);
  87.       add (s^, x^, xs);
  88.    UNTIL xs > DIGITS;
  89.    InOut.WriteLn;
  90.    InOut.WriteString ("   e = ");
  91.    InOut.Write (CHR (s^[0]+ORD ('0')));
  92.    InOut.Write ('.');
  93.    FOR i := 1 TO PRINT DO
  94.       InOut.Write (CHR (s^[i]+ORD ('0')));
  95.       IF i MOD 64 = 0 THEN
  96.         InOut.WriteLn;
  97.         InOut.WriteCard (i, 5);
  98.         InOut.WriteString ("    ")
  99.       END;
  100.    END;
  101.    InOut.WriteLn;
  102.    InOut.WriteLn;
  103. END exp.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement