home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / numana01.zip / SRC / MISCM2.MOD < prev    next >
Text File  |  1996-07-31  |  12KB  |  350 lines

  1. IMPLEMENTATION MODULE MiscM2;
  2.  
  3.         (********************************************************)
  4.         (*                                                      *)
  5.         (*          Miscellaneous utility procedures            *)
  6.         (*                                                      *)
  7.         (*  Programmer:         P. Moylan                       *)
  8.         (*  Last edited:        31 July 1996                    *)
  9.         (*  Status:             OK                              *)
  10.         (*                                                      *)
  11.         (*   Shortcomings:                                      *)
  12.         (*      1. The PressAnyKey procedure is failing to      *)
  13.         (*         return until end-of-line is found.  As a     *)
  14.         (*         workaround, I've changed it to require the   *)
  15.         (*         <Enter> key to be pressed.                   *)
  16.         (*      2. (fixed)                                      *)
  17.         (*                                                      *)
  18.         (*      The purpose of this module is to provide        *)
  19.         (*      the non-portable part of a numerical            *)
  20.         (*      analysis package - i.e. to separate out the     *)
  21.         (*      library dependencies, so that most of the       *)
  22.         (*      work in porting the software to another         *)
  23.         (*      compiler or library lies in rewriting this      *)
  24.         (*      (simple) module.                                *)
  25.         (*                                                      *)
  26.         (*      Many of the procedures here relate to output    *)
  27.         (*      to a screen window.  For use in an environment  *)
  28.         (*      which does not support screen windows, you      *)
  29.         (*      simply have to replace the definition of        *)
  30.         (*      type "Window" by a dummy definition, and let    *)
  31.         (*      the implementation ignore the "Window"          *)
  32.         (*      parameters.                                     *)
  33.         (*                                                      *)
  34.         (*      One catch with the present approach is that     *)
  35.         (*      it requires the concept of the "current         *)
  36.         (*      window".  Do not attempt to use this module     *)
  37.         (*      in multitasking applications, because if        *)
  38.         (*      more than one task is doing screen output       *)
  39.         (*      then there is an ambiguity in what constitutes  *)
  40.         (*      the current window.                             *)
  41.         (*                                                      *)
  42.         (*      This version is for use with the XDS compiler.  *)
  43.         (*                                                      *)
  44.         (********************************************************)
  45.  
  46. FROM SYSTEM IMPORT
  47.     (* type *)  ADDRESS,
  48.     (* proc *)  MOVE, ADDADR;
  49.  
  50. IMPORT LongMath, STextIO, SWholeIO, SRealIO, SLongIO, Conversions;
  51.  
  52. (************************************************************************)
  53.  
  54. VAR
  55.     (* The currently selected screen window. *)
  56.  
  57.     cw: Window;
  58.  
  59. (************************************************************************)
  60. (*                      MATHEMATICAL FUNCTIONS                          *)
  61. (************************************************************************)
  62.  
  63. PROCEDURE Exp (x: LONGREAL): LONGREAL;
  64.  
  65.     (* Exponential. *)
  66.  
  67.     BEGIN
  68.         RETURN LongMath.exp(x);
  69.     END Exp;
  70.  
  71. (************************************************************************)
  72.  
  73. PROCEDURE Log (x: LONGREAL): LONGREAL;
  74.  
  75.     (* Natural logarithm. *)
  76.  
  77.     BEGIN
  78.         RETURN LongMath.ln(x);
  79.     END Log;
  80.  
  81. (************************************************************************)
  82.  
  83. PROCEDURE Power (x, y: LONGREAL): LONGREAL;
  84.  
  85.     (* Computes x to the power of y. *)
  86.  
  87.     BEGIN
  88.         RETURN LongMath.power(x,y);
  89.     END Power;
  90.  
  91. (************************************************************************)
  92.  
  93. PROCEDURE Sin (x: LONGREAL): LONGREAL;
  94.  
  95.     (* Sine of x (radians). *)
  96.  
  97.     BEGIN
  98.         RETURN LongMath.sin(x);
  99.     END Sin;
  100.  
  101. (************************************************************************)
  102.  
  103. PROCEDURE Cos (x: LONGREAL): LONGREAL;
  104.  
  105.     (* Cosine of x (radians). *)
  106.  
  107.     BEGIN
  108.         RETURN LongMath.cos(x);
  109.     END Cos;
  110.  
  111. (************************************************************************)
  112.  
  113. PROCEDURE Sqrt (x: LONGREAL): LONGREAL;
  114.  
  115.     (* Square root. *)
  116.  
  117.     BEGIN
  118.         RETURN LongMath.sqrt(x);
  119.     END Sqrt;
  120.  
  121. (************************************************************************)
  122.  
  123. PROCEDURE ATan2 (x, y: LONGREAL): LONGREAL;
  124.  
  125.     (* Inverse tangent of y/x.  Result is in range -PI to PI. *)
  126.  
  127.     VAR result: LONGREAL;
  128.  
  129.     BEGIN
  130.         IF x = 0.0 THEN
  131.            IF y = 0.0 THEN RETURN 0.0
  132.            ELSIF y < 0.0 THEN RETURN -0.5*PI
  133.            ELSE RETURN 0.5*PI
  134.            END (*IF*);
  135.         ELSE
  136.             result := LongMath.arctan (y/x);
  137.             IF x < 0.0 THEN
  138.                 IF y >= 0.0 THEN result := PI - result
  139.                 ELSE result := result - PI
  140.                 END (*IF*);
  141.             END (*IF*);
  142.             RETURN result;
  143.         END (*IF*);
  144.     END ATan2;
  145.  
  146. (************************************************************************)
  147. (*                      MISCELLANEOUS UTILITIES                         *)
  148. (************************************************************************)
  149.  
  150. PROCEDURE BlockCopy (source, destination: ADDRESS;  bytecount: CARDINAL);
  151.  
  152.     (* Copies an array of bytes from the source address to the          *)
  153.     (* destination address.                                             *)
  154.  
  155.     BEGIN
  156.         MOVE (source, destination, bytecount);
  157.     END BlockCopy;
  158.  
  159. (************************************************************************)
  160.  
  161. PROCEDURE AddOffset (A: ADDRESS;  increment: CARDINAL): ADDRESS;
  162.  
  163.     (* Returns a pointer to the memory location whose physical address  *)
  164.     (* is Physical(A)+increment.  It is assumed that the caller will    *)
  165.     (* never try to run off the end of a segment.                       *)
  166.  
  167.     BEGIN
  168.         RETURN ADDADR (A, increment);
  169.     END AddOffset;
  170.  
  171. (************************************************************************)
  172. (*                  NUMERIC-TO-STRING CONVERSION                        *)
  173. (************************************************************************)
  174.  
  175. PROCEDURE LongRealToString (number: LONGREAL;
  176.                                         VAR (*OUT*) buffer: ARRAY OF CHAR;
  177.                                         fieldsize: CARDINAL);
  178.  
  179.     (* Converts the number to a decimal character string in array       *)
  180.     (* "buffer", right-justified in a field of fieldsize characters.    *)
  181.     (* The format depends on the size of the number relative to the     *)
  182.     (* size of the buffer.                                              *)
  183.  
  184.     BEGIN
  185.         Conversions.LongRealToString (number, buffer, fieldsize);
  186.     END LongRealToString;
  187.  
  188. (************************************************************************)
  189. (*                          SCREEN OUTPUT                               *)
  190. (************************************************************************)
  191.  
  192. PROCEDURE SelectWindow (w: Window);
  193.  
  194.     (* Specifies that all screen output, up until the next call to      *)
  195.     (* SelectWindow, will be to window w.                               *)
  196.  
  197.     BEGIN
  198.         cw := w;
  199.     END SelectWindow;
  200.  
  201. (************************************************************************)
  202.  
  203. PROCEDURE WriteString (s: ARRAY OF CHAR);
  204.  
  205.     (* Writes s to the current window. *)
  206.  
  207.     BEGIN
  208.         STextIO.WriteString (s);
  209.     END WriteString;
  210.  
  211. (************************************************************************)
  212.  
  213. PROCEDURE WriteLn;
  214.  
  215.     (* Writes an end-of-line to the current window. *)
  216.  
  217.     BEGIN
  218.         STextIO.WriteLn;
  219.     END WriteLn;
  220.  
  221. (************************************************************************)
  222.  
  223. PROCEDURE PressAnyKey;
  224.  
  225.     (* "Press any key to continue". *)
  226.     (* Bug: this is requiring "Enter" before the character can  *)
  227.     (* be read.  I'm not yet sure how to solve this.            *)
  228.     (* As a temporary work-around, we require the user to       *)
  229.     (* press the <Enter> key rather than the <Any> key.         *)
  230.  
  231.     (*VAR dummy: CHAR;*)
  232.  
  233.     BEGIN
  234.         STextIO.WriteLn;
  235.         STextIO.WriteString ("Press Enter to continue");
  236.         STextIO.SkipLine;
  237.         (*STextIO.ReadChar (dummy);*)
  238.  
  239.         (* Alternative approaches that didn't work: *)
  240.  
  241.         (*conio.getch();*)              (* Linker can't find getch *)
  242.         (*InOut.Read (dummy);*)         (* Same problem as with STextIO.ReadChar *)
  243.         (*OS2.KbdCharIn(dummy);*)       (* Compiler can't find KbdCharIn, even though *)
  244.                                         (* :INCL_KBD+ was specified.  Error in OS2.DEF? *)
  245.  
  246.     END PressAnyKey;
  247.  
  248. (************************************************************************)
  249.  
  250. PROCEDURE Error (message: ARRAY OF CHAR);
  251.  
  252.     (* Puts a message to the screen. *)
  253.  
  254.     (* VAR w, save: Window; *)
  255.  
  256.     BEGIN
  257.         (*
  258.         save := cw;
  259.         Windows.OpenWindow (w, Windows.black, Windows.green, 11, 14, 10, 69,
  260.                                 Windows.simpleframe, Windows.nodivider);
  261.         SelectWindow (w);
  262.         *)
  263.         WriteString ("Error: ");  WriteString (message);
  264.         PressAnyKey;
  265.         (*
  266.         Windows.CloseWindow (w);
  267.         SelectWindow (save);
  268.         *)
  269.     END Error;
  270.  
  271. (************************************************************************)
  272.  
  273. PROCEDURE WriteCard (N: CARDINAL);
  274.  
  275.     (* Writes a cardinal value. *)
  276.  
  277.     BEGIN
  278.         SWholeIO.WriteCard (N, 8);
  279.     END WriteCard;
  280.  
  281. (************************************************************************)
  282.  
  283. PROCEDURE WriteRJCard (number, fieldsize: CARDINAL);
  284.  
  285.     (* Like WriteCard, but the result is right justified in a field     *)
  286.     (* of fieldsize characters.                                         *)
  287.  
  288.     BEGIN
  289.         SWholeIO.WriteCard (number, fieldsize);
  290.     END WriteRJCard;
  291.  
  292. (************************************************************************)
  293.  
  294. PROCEDURE WriteReal (x: REAL;  places: CARDINAL);
  295.  
  296.     (* Writes x in a field "places" characters wide. *)
  297.  
  298.     BEGIN
  299.         SRealIO.WriteReal (x, places);
  300.     END WriteReal;
  301.  
  302. (************************************************************************)
  303.  
  304. PROCEDURE WriteLongReal (x: LONGREAL;  places: CARDINAL);
  305.  
  306.     (* Writes x in a field "places" characters wide. *)
  307.  
  308.     VAR buffer: ARRAY [0..127] OF CHAR;
  309.  
  310.     BEGIN
  311.         (* I've scrapped my use of SLongIO here because it was giving wrong answers. *)
  312.         (*SLongIO.WriteReal (x, places);*)
  313.  
  314.         LongRealToString (x, buffer, places);
  315.         WriteString (buffer);
  316.  
  317.     END WriteLongReal;
  318.  
  319. (************************************************************************)
  320. (*                         KEYBOARD INPUT                               *)
  321. (************************************************************************)
  322.  
  323. PROCEDURE ReadCard (VAR (*OUT*) N: CARDINAL);
  324.  
  325.     (* Reads a cardinal from the keyboard, echoing it to screen. *)
  326.  
  327.     BEGIN
  328.         SWholeIO.ReadCard (N);
  329.     END ReadCard;
  330.  
  331. (************************************************************************)
  332.  
  333. PROCEDURE ReadLongReal(): LONGREAL;
  334.  
  335.     (* Reads and converts a numeric string from the keyboard.   *)
  336.  
  337.     VAR result: LONGREAL;
  338.  
  339.     BEGIN
  340.         SLongIO.ReadReal(result);
  341.         RETURN result;
  342.     END ReadLongReal;
  343.  
  344. (************************************************************************)
  345.  
  346. BEGIN
  347.     cw := 0;
  348. END MiscM2.
  349.  
  350.