exp.ob07 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. MODULE exp;
  11. IMPORT In, Out, Console;
  12. CONST
  13. PRINT = 1024;
  14. DIGITS = PRINT + (PRINT + 31) DIV 32;
  15. TYPE
  16. number = ARRAY DIGITS + 1 OF INTEGER;
  17. VAR
  18. s, x: number;
  19. xs, i: INTEGER;
  20. PROCEDURE init (VAR x: number; n: INTEGER);
  21. VAR
  22. i: INTEGER;
  23. BEGIN
  24. x[0] := n;
  25. FOR i := 1 TO DIGITS DO x[i] := 0 END
  26. END init;
  27. PROCEDURE divide (VAR x: number; xs, n: INTEGER;
  28. VAR y: number; VAR ys: INTEGER);
  29. VAR
  30. i, c: INTEGER;
  31. BEGIN
  32. c := 0;
  33. FOR i := xs TO DIGITS DO
  34. c := 10 * c + x[i];
  35. y[i] := c DIV n;
  36. c := c MOD n
  37. END;
  38. ys := xs;
  39. WHILE (ys <= DIGITS) & (y[ys] = 0) DO INC(ys) END
  40. END divide;
  41. PROCEDURE add (VAR s, x: number; xs: INTEGER);
  42. VAR
  43. i, c: INTEGER;
  44. BEGIN
  45. c := 0;
  46. FOR i := DIGITS TO xs BY -1 DO
  47. c := c + s[i] + x[i];
  48. IF c >= 10 THEN
  49. s[i] := c - 10;
  50. c := 1
  51. ELSE
  52. s[i] := c;
  53. c := 0
  54. END
  55. END;
  56. i := xs;
  57. WHILE c # 0 DO
  58. DEC(i);
  59. c := c + s[i];
  60. IF c >= 10 THEN
  61. s[i] := c - 10;
  62. c := 1
  63. ELSE
  64. s[i] := c;
  65. c := 0
  66. END
  67. END
  68. END add;
  69. BEGIN
  70. Console.open;
  71. init(s, 0);
  72. init(x, 1);
  73. xs := 0;
  74. add(s, x, xs);
  75. i := 0;
  76. REPEAT
  77. INC(i);
  78. divide(x, xs, i, x, xs);
  79. add(s, x, xs);
  80. UNTIL xs > DIGITS;
  81. Out.Ln;
  82. Out.String (" e = ");
  83. Out.Char (CHR(s[0] + ORD("0")));
  84. Out.Char (".");
  85. FOR i := 1 TO PRINT DO
  86. Out.Char (CHR(s[i] + ORD("0")));
  87. IF i MOD 64 = 0 THEN
  88. Out.Ln;
  89. Out.Int (i, 5);
  90. Out.String (" ")
  91. END
  92. END;
  93. Out.Ln;
  94. Out.Ln;
  95. In.Ln;
  96. Console.exit(TRUE)
  97. END exp.