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

Type COMPLEX

WiP.TypeCOMPLEX History

Hide minor edits - Show changes to markup

2010-04-09 19:00 by benjk -
Changed line 11 from:
    COMPLEX = OPAQUE RECORD ("C-Type")
to:
    COMPLEX = RECORD ("C-Type")
Deleted lines 14-36:

(* 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;

2010-04-08 16:03 by benjk -
Changed lines 17-18 from:

(* convenience accessors *)

to:

(* unbound accessors for convenience *)

Changed line 26 from:

(* general accessor and mutator *)

to:

(* bound accessor and mutator *)

2010-04-08 16:00 by benjk -
Changed lines 17-21 from:

(* constructor and accessors *)

PROCEDURE Cmplx ( re, im : REAL ) : COMPLEX; (* constructs a complex number from an ordered pair *)

to:

(* convenience accessors *)

Added lines 24-32:

(* general 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 *)

2010-04-06 17:04 by benjk -
Deleted lines 16-20:

(* number type *)

CONST [TYPE] semanticModel = 'C'; (* obsolete *)

Changed line 19 from:

PROCEDURE Cmplx ( real, imag : REAL ) : COMPLEX;

to:

PROCEDURE Cmplx ( re, im : REAL ) : COMPLEX;

Changed lines 31-33 from:

CONST [{}] allowValueConstructors = TRUE; (* either this or ... *)

CONST [:=] allowValueConstructors = TRUE; (* but this needs more checks *)

to:

CONST [{}] allowValueConstructors = TRUE;

2010-04-05 12:17 by benjk -
Changed line 12 from:
        re : REAL; (* real part *)
to:
        re,        (* real part *)
2010-04-05 12:17 by benjk -
Changed line 11 from:
    COMPLEX = OPAQUE RECORD ['C']
to:
    COMPLEX = OPAQUE RECORD ("C-Type")
Changed line 19 from:

CONST [TYPE] semanticModel = 'C';

to:

CONST [TYPE] semanticModel = 'C'; (* obsolete *)

2010-04-05 10:37 by benjk -
Changed line 11 from:
    ['C'] COMPLEX = OPAQUE RECORD
to:
    COMPLEX = OPAQUE RECORD ['C']
2010-04-05 10:32 by benjk -
Changed line 11 from:
    COMPLEX = OPAQUE RECORD
to:
    ['C'] COMPLEX = OPAQUE RECORD
2010-04-04 10:01 by benjk -
Changed lines 36-38 from:

CONST [{}] allowStructuredLiteralAssignment = TRUE; (* this is simplest *)

CONST [:=] allowStructuredLiteralAssignment = TRUE; (* this needs more checks *)

to:

CONST [{}] allowValueConstructors = TRUE; (* either this or ... *)

CONST [:=] allowValueConstructors = TRUE; (* but this needs more checks *)

2010-04-04 09:59 by benjk -
Added lines 32-38:

(* literal assignment *)

CONST [{}] allowStructuredLiteralAssignment = TRUE; (* this is simplest *)

CONST [:=] allowStructuredLiteralAssignment = TRUE; (* this needs more checks *)

2010-04-04 09:30 by benjk -
2010-04-04 09:29 by benjk -
Changed lines 34-35 from:

(* unary arithmetic operations *)

to:

(* monadic arithmetic operations *)

Changed line 41 from:

(* binary arithmetic operations *)

to:

(* dyadic arithmetic operations *)

2010-04-04 09:29 by benjk -
Changed lines 17-19 from:

(* number system *)

CONST [C] isComplexNumberType = TRUE;

to:

(* number type *)

CONST [TYPE] semanticModel = 'C';

2010-03-23 05:45 by benjk -
Changed lines 57-59 from:

(* IO operations *)

(* hooks for standard procedures READ, WRITE and WRITEF *)

to:

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

2010-03-23 05:43 by benjk -
Deleted lines 57-58:
2010-03-23 05:36 by benjk -
Changed line 67 from:
   - the numeric value of the numeral string read is assigned to the variable passed in for b
to:
   - the numeric value of the numeral string read is assigned to the variable passed in for z
Changed line 71 from:

(* Writes the textual representation of value z in simple format to output stream outfile *)

to:

(* Writes the textual representation of COMPLEX value z in simple format to output stream outfile *)

2010-03-23 05:35 by benjk -
Changed lines 5-6 from:

FROM File IO? IMPORT File;

to:

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

(* COMPLEX type *)

Changed lines 12-13 from:
        re : REAL;
        im : REAL;
to:
        re : REAL; (* real part *)
        im : REAL; (* imaginary part *)
Added lines 16-21:

(* number system *)

CONST [C] isComplexNumberType = TRUE;

Changed lines 25-26 from:

(* constructs a complex from an ordered pair *)

to:

(* constructs a complex number from an ordered pair *)

Changed lines 56-58 from:

PROCEDURE [#] isNotEqual ( z1, z2 : COMPLEX ) : BOOLEAN;

to:
Changed lines 59-63 from:

PROCEDURE (* [READ] *) Read( infile : File; VAR z : COMPLEX );

to:

(* hooks for standard procedures READ, WRITE and WRITEF *)

PROCEDURE Read( infile : File; VAR z : COMPLEX );

Changed lines 67-91 from:
   - 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 allRight, outOfRange, wrongFormat, endOfLine, or endOfInput *)

PROCEDURE (* [WRITE] *) Write( outfile : File; z : COMPLEX ); (* Writes the textual representation of z in scientific format showing nine places after the

   decimal point to output stream outfile without padding. *)

PROCEDURE (* [WRITEF] *) Write F?( outfile : File; z : COMPLEX; n : CARDINAL); (* Writes the textual representation of z in scientific format showing n places after the

   decimal point to output stream outfile without padding. *)

PROCEDURE (* [WRITEF] *) Write Fixed?( outfile : File; z : COMPLEX; i, n : CARDINAL ); (* Writes the textual representation of z in simple format with the decimal point at position i

    and showing n places after the decimal point to output stream outfile.

(* unbound IO operations *)

PROCEDURE Write Eng?( outfile : File; num : COMPLEX; n, m, exp : CARDINAL ); (* Writes the textual representation of z in engineering format showing n places before and

   m places after the decimal point with exponent exp to output stream outfile without padding.
   Parameters m and exp are rounded to the nearest value of three. *)

PROCEDURE Write Ledger?( outfile : File; num : COMPLEX; i, n : CARDINAL ); (* Writes the textual representation of z in ledger format with the sign at position 0, the decimal

   point at position i and showing n places after the decimal point to output stream outfile. *)
to:
   - the numeric value of the numeral string read is assigned to the variable passed in for b
   - 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 value z in simple format to output stream outfile *)

PROCEDURE Write F?( 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. *)
2010-01-17 12:45 by benjk -
Added lines 1-82:
DEFINITION MODULE COMPLEX;

(* Complex Numbers *) 

FROM FileIO IMPORT File;

TYPE
    COMPLEX = OPAQUE RECORD
        re : REAL;
        im : REAL;
    END;

(* constructor and accessors *)

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

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 *)


(* unary arithmetic operations *) 

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

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


(* binary 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; 

PROCEDURE [#] isNotEqual ( z1, z2 : COMPLEX ) : BOOLEAN; 


(* IO operations *)

PROCEDURE (* [READ] *) 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 allRight, outOfRange, wrongFormat, endOfLine, or endOfInput *)

PROCEDURE (* [WRITE] *) Write( outfile : File; z : COMPLEX );
(* Writes the textual representation of z in scientific format showing nine places after the
   decimal point to output stream outfile without padding. *)

PROCEDURE (* [WRITEF] *) WriteF( outfile : File; z : COMPLEX; n : CARDINAL);
(* Writes the textual representation of z in scientific format showing n places after the
   decimal point to output stream outfile without padding. *)

PROCEDURE (* [WRITEF] *) WriteFixed( outfile : File; z : COMPLEX; i, n : CARDINAL );
(* Writes the textual representation of z in simple format with the decimal point at position i
    and showing n places after the decimal point to output stream outfile.

(* unbound IO operations *)

PROCEDURE WriteEng( outfile : File; num : COMPLEX; n, m, exp : CARDINAL );
(* Writes the textual representation of z in engineering format showing n places before and
   m places after the decimal point with exponent exp to output stream outfile without padding.
   Parameters m and exp are rounded to the nearest value of three. *)

PROCEDURE WriteLedger( outfile : File; num : COMPLEX; i, n : CARDINAL );
(* Writes the textual representation of z in ledger format with the sign at position 0, the decimal
   point at position i and showing n places after the decimal point to output stream outfile. *)

END COMPLEX.