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

OPAQUE Type COMPLEX

Wastebasket.OPAQUETypeCOMPLEX History

Hide minor edits - Show changes to output

2010-04-09 18:59 by benjk -
Added lines 1-79:
[@DEFINITION MODULE COMPLEX;

(* Complex Numbers *)

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


(* COMPLEX type *)

TYPE
    COMPLEX = OPAQUE RECORD ("C-Type")
        re,        (* real part *)
        im : REAL; (* imaginary part *)
    END;


(* unbound accessors for convenience *)

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

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


(* bound accessor and mutator *)

PROCEDURE [.] component ( n : CARDINAL; z : COMPLEX ) : REAL;
(* Returns the value of the n-th component of complex number z *)

PROCEDURE [!] setComponent ( n : CARDINAL; VAR z : COMPLEX; r : REAL );
(* Overwrites the value of the n-th component of complex number z with r *)


(* literal assignment *)

CONST [{}] allowValueConstructors = TRUE;


(* monadic arithmetic operations *)

PROCEDURE [ABS] abs ( z : COMPLEX ) : COMPLEX;

PROCEDURE [NEG] neg ( z : COMPLEX ) : COMPLEX;


(* dyadic arithmetic operations *)

PROCEDURE [+] add ( z1, z2 : COMPLEX ) : COMPLEX;

PROCEDURE [-] sub ( z1, z2 : COMPLEX ) : COMPLEX;

PROCEDURE [*] multiply ( z1, z2 : COMPLEX ) : COMPLEX;

PROCEDURE [/] divide ( z1, z2 : COMPLEX ) : COMPLEX;


(* relational operations *)

PROCEDURE [=] isEqual ( z1, z2 : COMPLEX ) : BOOLEAN;


(* IO operations, bound to READ, WRITE and WRITEF *)

PROCEDURE Read( infile : File; VAR z : COMPLEX );
(* Reads the textual representation of a COMPLEX value in simple format 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 *)

PROCEDURE Write( outfile : File; z : COMPLEX );
(* Writes the textual representation of COMPLEX value z in simple format to output stream outfile *)

PROCEDURE WriteF( outfile : File; fmtStr : ARRAY OF CHAR; items : CARDINAL; VARIADIC v[items] OF z : COMPLEX );
(* Writes a formatted textual representation of one or more COMPLEX values to output stream outfile. The value
  of parameter items is calculated and inserted automatically. The output format is determined by fmtStr. *)

END COMPLEX.@]