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

Real Number Formatting

There are multiple formats for textual representation of real numbers

Scientific format, defined by the following EBNF:

sciReal :
  ( '+' | '-' )? decimalDigit '.' digitGroup ( ',' digitGroup )* scaleFactor ;
digitGroup :
  decimalDigit decimalDigit decimalDigit ;
scaleFactor :
  ( '+' | '-') decimalDigit+ ;
decimalDigit :
  '0' .. '9' ;

Example: 1.234,567,890E+00

Formatting parameters:

  • number of places after decimal point

Engineering format, defined by the following EBNF:

engReal :
  ( '+' | '-' )? decimalDigit decimalDigit? decimalDigit? ( ',' digitGroup )*
  '.' digitGroup ( ',' digitGroup )* scaleFactor ;
digitGroup :
  decimalDigit decimalDigit decimalDigit ;
scaleFactor :
  ( '+' | '-') decimalDigit+ ;
decimalDigit :
  '0' .. '9' ;

The numeric value of scaleFactor must be divisible by three.

Example: 1,234.567,890E+03

Formatting parameters:

  • number of places before decimal point
  • number of places after decimal point
  • exponent

Ledger format, defined by the following EBNF:

ledgerReal :
  ( '+' | '-' )? whitespace* decimalDigit decimalDigit? decimalDigit? ( ',' digitGroup )*
  ( '.' decimalDigits+ )?
digitGroup :
  decimalDigit decimalDigit decimalDigit ;
decimalDigit :
  '0' .. '9' ;

Examples: 1,234,567.00 or 1,234,567

Formatting parameters:

  • index of decimal point in output field
  • number of places after decimal point

Simple format, defined by the following EBNF:

simpleReal :
  ( '+' | '-' )? decimalDigit+ ( '.' decimalDigits+ )?
decimalDigit :
  '0' .. '9' ;

Examples: 123456.7890 or 123456

Formatting parameters:

  • index of decimal point in output field
  • number of places after decimal point

IO Procedures for Real Number types

The standard IO library shall provide the following output procedures for every real number type that is provided by the language core or by the standard library. It shall be recommended that user libraries implementing real number types also provide these procedures.

PROCEDURE Write( f : File; r : <RealType> );

shall produce output in scientific format with 9 places after the decimal point.

PROCEDURE WriteF( f : File; r : <RealType>; n : CARDINAL );

shall produce output in scientific format with n places after the decimal point.

PROCEDURE WriteEng( f : File; r : <RealType>; n, m, exp : CARDINAL );

shall produce output in engineering format with n places before, m places after the decimal point and exponent exp, whereby parameters m and exp shall be rounded to the nearest multiple of three.

PROCEDURE WriteLedger( f : File; r : <RealType>; i, n : CARDINAL );

shall produce output in ledger format with the decimal point at index i and n places after the decimal point.

PROCEDURE WriteFixed( f : File; r : <RealType>; i, n : CARDINAL );

shall produce output in simple format with the decimal point at index i and n places after the decimal point.

Mapping of WRITE and WRITEF macros

The compiler shall expand the WRITE and WRITEF macros when invoked with a real number argument as follows:

WRITE( file, realnum ); shall expand to: TypeOf(realnum).Write( file, realnum );

WRITEF( file, realnum, n ); shall expand to: TypeOf(realnum).WriteF( file, realnum, n );

WRITEF( file, realnum, i, n ); shall expand to: TypeOf(realnum).WriteFixed( file, realnum, i, n );

whereby TypeOf(realnum) is to be replaced by the name of the real number type.

Affected Types

  • pervasive type REAL
  • pervasive type LONGREAL
  • standard library type BCD
  • standard library type LONGBCD