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

DEFINITION MODULE COMPLEX [CTYPE];

(* Single Precision Complex Numbers *) 


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


(* COMPLEX type *)

TYPE
    COMPLEX = RECORD
        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. *)

END COMPLEX.