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

Unsigned Real 60

DEFINITION MODULE UnsignedReal60 [RTYPE];

(* Unsigned Real Subrange Type With Values from 0.0 to 59.999 *) 


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


(* UnsignedReal60 type *)

TYPE
    UnsignedReal60 = OPAQUE RECORD
        value : ARRAY 2 OF OCTET; (* 16 bits *)
    END; (* UnsignedReal60 *)


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


(* Range *) 

CONST [TMIN] minValue = 0.000;
(* Smallest value of type UnsignedReal60.
   This value is bound to TMIN for type UnsignedReal60. *) 

CONST [TMAX] maxValue = +59.999;
(* Largest value of type UnsignedReal60.
   This value is bound to TMAX for type UnsignedReal60. *)


(* Literal assignment *)

PROCEDURE [:=] assign ( VAR assignTo : UnsignedReal60; literal : ARRAY OF CHAR );
(* Converts string literal to a UnsignedReal60 value and assigns it to assignTo.
   This procedure is bound to the := operator for literal assignment. *)


(* Type conversions *)

PROCEDURE [::] toCARD  ( r : UnsignedReal60 ) : CARDINAL;
(* Converts UnsignedReal60 value r to a CARDINAL value and returns it.
   This function is bound to the :: operator for UnsignedReal60 to CARDINAL
   conversion. *)

PROCEDURE [::] toINT ( r : UnsignedReal60 ) : INTEGER; 
(* Converts UnsignedReal60 value r to an INTEGER value and returns it.
   This function is bound to the :: operator for UnsignedReal60 to INTEGER
   conversion. *)

PROCEDURE [::] toREAL ( r : UnsignedReal60 ) : REAL;
(* Converts UnsignedReal60 value r to a REAL value and returns it.
   This function is bound to the :: operator for UnsignedReal60 to REAL
   conversion. *)


(* Monadic arithmetic operations *) 

PROCEDURE floor ( r : UnsignedReal60 ) : UnsignedReal60;
(* Returns the floor value of r. *)

PROCEDURE ceil ( r : UnsignedReal60 ) : UnsignedReal60;
(* Returns the ceiling value of r. *)

PROCEDURE frac ( r : UnsignedReal60 ) : UnsignedReal60;
(* Returns the fractional part of r. *)


(* Dyadic arithmetic operations *) 

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

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

PROCEDURE multiply ( r : UnsignedReal60; n : CARDINAL ) : UnsignedReal60;
(* Multiplies UnsignedReal60 value r by scalar n and returns the result. *)

PROCEDURE divide ( r : UnsignedReal60; n : CARDINAL ) : UnsignedReal60;
(* Divives UnsignedReal60 value r by scalar n and returns the result. *)

PROCEDURE trunc ( r : UnsignedReal60; n : CARDINAL ) : UnsignedReal60;
(* Returns the value of r truncated to n decimal places. *)

PROCEDURE round ( r : UnsignedReal60; n : CARDINAL ) : UnsignedReal60;
(* Returns the value of r rounded to n decimal places. *)


(* Relational operations *) 

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

PROCEDURE [<] isLess ( r1, r2 : UnsignedReal60 ) : BOOLEAN;
(* Returns TRUE if UnsignedReal60 value r1 is less than r2, otherwise FALSE.
   This function is bound to operators < and >= for type UnsignedReal60. *)

PROCEDURE [>] isGreater ( r1, r2 : UnsignedReal60 ) : BOOLEAN;
(* Returns TRUE if UnsignedReal60 value r1 is greater than r2, otherwise FALSE.
   This function is bound to operators > and <= for type UnsignedReal60. *)


(* Scalar conversion primitives *)

CONST digitCapacity = 5; (* maximum total digits *)

PROCEDURE [TO] toSXF ( r : UnsignedReal60; VAR s : ARRAY OF CHAR );
(* Converts a UnsignedReal60 value to a string in scalar exchange format.
   This procedure is used to synthesise conversions to other scalar types
   when no direct conversion path exists. *)

PROCEDURE [FROM] fromSXF ( VAR r : UnsignedReal60; CONST s : ARRAY OF CHAR );
(* Converts a string in scalar exchange format to a UnsignedReal60 value.
   This procedure is used to synthesise conversions from other scalar types
   when no direct conversion path exists. *)


(* IO operations *)

PROCEDURE Read( infile : File; VAR r : UnsignedReal60 );
(* Reads the textual representation of a UnsignedReal60 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 r
   - the file status is set to any of:
     success, outOfRange, wrongFormat, endOfLine, or endOfInput.
     This procedure is substituted for invocations of READ with an
     UnsignedReal60 argument.*)

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

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

END UnsignedReal60.