Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

COMPLEX

Spec.COMPLEX History

Hide minor edits - Show changes to output

2010-05-24 13:06 by benjk -
Changed lines 1-2 from:
[@DEFINITION MODULE COMPLEX;
to:
[@DEFINITION MODULE COMPLEX [CTYPE];
Changed line 12 from:
   COMPLEX = RECORD ("C-Type")
to:
   COMPLEX = RECORD
2010-05-24 11:55 by benjk -
Changed lines 3-6 from:
(* by R. Sutcliffe, last modified 2010 01 07 *)

IMPORT
File;
to:
(* Single Precision Complex Numbers *)


FROM FileIO IMPORT
File; (* required for IO *)


(* COMPLEX type *)

Changed lines 12-70 from:
  COMPLEX = OPAQUE RECORD
      re : REAL;
 
    im : REAL;
  END;

PROCEDURE Cmplx
(real, imag : REAL) : COMPLEX;
(* constructs a complex from an ordered pair
*)

PROCEDURE Re (num : COMPLEX) : REAL;
(* returns the real part of a complex number *)

PROCEDURE Im (num :
COMPLEX) : REAL;
(* returns the imaginary part of a complex number *)

PROCEDURE Add (z1, z2 :
COMPLEX) : COMPLEX;

PROCEDURE Subtract
(z1, z2 : COMPLEX) : COMPLEX;

PROCEDURE Multiply (z1, z2 : COMPLEX) : COMPLEX;

PROCEDURE Divide (z1, z2 : COMPLEX) : COMPLEX;

PROCEDURE abs (z: COMPLEX): REAL;
  (* Returns the length of z
*)

(* I/O section *)

PROCEDURE Read (fid :File; VAR complex:
COMPLEX);
  (* Skips leading spaces, and removes any remaining characters from cid that form part
    of a complex number.  The value of this number is assigned to complex. The file state
    is set to
the value allRight, outOfRange, wrongFormat, endOfLine, or endOfInput. *)
 
(* The following procedure affects all the Write procs below *)

PROCEDURE SetVerbose (verbose
: BOOLEAN);
  (* if true prints both components even if one is zero; else prints only one if the other is zero.
    The default is false. *)

(* We assume in the following that REAL.Writexx is called for each part, with its semantics. *)

PROCEDURE Write (fid :File; complex: COMPLEX; width: CARDINAL);
  (* Writes the value of complex to cid, as WriteFixed if the sign and magnitude can be shown
     in the given width, or otherwise as WriteFloat.  The number of places or significant digits
 
   depends on the given width. *)

PROCEDURE WriteFloat (fid :File; complex: COMPLEX; sigFigs: CARDINAL; width: CARDINAL);
  (* Writes the value of complex to cid in floating-point real text form, with sigFigs significant
    figures, in a field of the given minimum width. The width for the real parts is 0 if the
 
  supplied width is 3 or less, and it is (width - 4) DIV 2 otherwise. *)

PROCEDURE WriteEng (fid :File; complex: COMPLEX; sigFigs: CARDINAL; width: CARDINAL);

  (* As for WriteFloat, except that the number is scaled with one to three
    digits in the whole number part, and with an exponent that is a multiple of three. *)
 
PROCEDURE WriteFixed (fid
:File; complex: COMPLEX; place: INTEGER; width: CARDINAL);
  (* Writes the
value of complex to cid in fixed-point text form, with real parts rounded to the
    given place relative to the decimal point, in a field of the given minimum width. *)
to:
    COMPLEX = RECORD ("C-Type")
       re, im : REAL;
    END; (* COMPLEX *)


(* C-Type bindings to operators, pervasives and primitives: *)


(* Monadic arithmetic operations *
)

PROCEDURE [ABS] abs
( z : COMPLEX ) : COMPLEX;
(* Returns the absolute value of
COMPLEX value z.
  This function is bound to pervasive function ABS for type
COMPLEX. *)

PROCEDURE [NEG] neg
( z : COMPLEX ) : COMPLEX;
(* Returns the sign reversed value of COMPLEX value z.
 
  This function is bound to the unary minus operator for type COMPLEX. *)


(* Dyadic arithmetic operations *)

PROCEDURE [+] add ( z1, z2 :
COMPLEX ) : COMPLEX;
(* Adds COMPLEX values z1 and z2 and returns the result.
  This function is bound to the + operator for type COMPLEX. *)

PROCEDURE [-] sub ( z1, z2 : COMPLEX ) : COMPLEX;
(* Subtracts COMPLEX value z2 from z1 and returns
the result.
  This function is bound to the - operator for type COMPLEX. *)

PROCEDURE [*] multiply ( z1, z2
: COMPLEX ) : COMPLEX;
(* Multiplies COMPLEX values z1 and z2 and returns the result.
  This function it bound to the * operator for type COMPLEX. *)

PROCEDURE [/] divide ( z1, z2 : COMPLEX ) : COMPLEX;
(* Divives COMPLEX value z1 by z2 and returns the result.
 
  This function is bound to the / operator for type COMPLEX. *)


(* Relational operations *)

PROCEDURE [=] isEqual ( z1, z2 : COMPLEX ) : BOOLEAN;
(* Returns TRUE if COMPLEX values z1 and z2 are equal, otherwise FALSE.
  This function is bound to operators = and # for type COMPLEX
. *)


(* IO operations *)

PROCEDURE Read( infile
: File; VAR z : COMPLEX );
(* Reads
the textual representation of a COMPLEX value from input stream infile
  - any leading whitespace is skipped
  - any remaining characters that are part of the numeral being read are
    removed from infile
   - the numeric value of the numeral string read is assigned to the variable
    passed in for z
  - the file status is set to any of:
    success, outOfRange, wrongFormat, endOfLine, or endOfInput. This
  procedure is substituted for invocations of READ with a COMPLEX argument. *)

PROCEDURE Write( outfile : File; CONST z
: COMPLEX );
(* Writes the textual representation of value z to output stream outfile. This
  procedure is substituted for invocations of WRITE with a COMPLEX argument.*)

PROCEDURE WriteF ( outfile      : File;
 
               CONST fmtStr : ARRAY OF CHAR;
                  items        : VARIADIC OF CONST COMPLEX );
(* Writes a formatted textual representation of one or more COMPLEX values to
  output stream outfile. The output format is determined by fmtStr. This
  procedure is substituted for invocations of WRITEF with one or more
  COMPLEX arguments. *)