[comp.lang.modula2] MathLib0 procedures

HPA111@DE0HRZ1A.BITNET ("Michael Drechsler +49 343062", 0201) (05/09/88)

Hello,
at the end of Wirth's book PIM2 ed.3 (german version) is a listing of
the def mod MathLib0, but no description. This module contains the two
procedures "real" and "entier". I have some ideas what this procs do,
but I'm not sure.

Is the
   PROCEDURE real(x:INTEGER): REAL;
only another form of FLOAT(CARDINAL(x))

or is the
   PROCEDURE entier(x:REAL): INTEGER;
just another form of INTEGER(TRUNC(x)) ?

Thanks for your help,
Mike

harv@harris.cis.ksu.EDU (Harvard Townsend) (05/09/88)

>>Is the
>>   PROCEDURE real(x:INTEGER): REAL;
>>only another form of FLOAT(CARDINAL(x))

Correct.  That's all our implementation does:  "RETURN (FLOAT(x));"

>>or is the
>>   PROCEDURE entier(x:REAL): INTEGER;
>>just another form of INTEGER(TRUNC(x)) ?

This one is a little different.  I understand it to be the "nearest integer
to REAL x".  So I guess it is a TRUNC with rounding.  Any REAL number with
a fractional part >= .5 is rounded up to the next highest integer.
Anything < .5 is equivalent to TRUNC(x).
______________________________________
Harvard Townsend, Systems Manager
Dept. of Computing & Information Sciences
Kansas State University, Manhattan, KS 66506   (913)532-6350
CSNET:    harv@cis.ksu.edu -or- harv@kansas-state.csnet
BITNET: harv@ksuvax1.bitnet -or- harv%ksuvax1.bitnet@cunyvm.cuny.edu
UUCP:   {ihnp4,cbatt,dcdwest}!ncr-sd!ncrwic!ksuvax1!harv
    -or- ihnp4!wnuxa!ksuvax1!harv -or- ...!psuvax1!ksuvax1.bitnet!harv

alan@pdn.UUCP (Alan Lovejoy) (05/10/88)

In article <INFO-M2%88050910174344@DB0TUI11> Info-Modula2 Distribution List <INFO-M2%UCF1VM.bitnet@jade.berkeley.edu> writes:
>Is the
>   PROCEDURE real(x:INTEGER): REAL;
>only another form of FLOAT(CARDINAL(x))

Real is a function defined on INTEGERs instead of CARDINALs.  Note that
if x < 0, then CARDINAL(x) is gibberish.

>or is the
>   PROCEDURE entier(x:REAL): INTEGER;
>just another form of INTEGER(TRUNC(x)) ?

Entier is a function whose domain is the INTEGERs, not the natural
numbers (CARDINALs).  It is known in American math jargon as "floor".
It returns the greatest number not less than its input, where greater
and lesser are defined arithmetically, not in terms of absolute
magnitude.  The distinction is important for INTEGERs, but not for
CARDINALs.

-- 
Alan Lovejoy; alan@pdn; 813-530-8241; Paradyne Corporation: Largo, Florida.
Disclaimer: Do not confuse my views with the official views of Paradyne
            Corporation (regardless of how confusing those views may be).
Motto: Never put off to run-time what you can do at compile-time!  

alan@pdn.UUCP (Alan Lovejoy) (05/10/88)

In article <8805091402.AA23129@harris.cis.ksu.edu> Info-Modula2 Distribution List <INFO-M2%UCF1VM.bitnet@jade.berkeley.edu> writes:
>Correct.  That's all our implementation does:  "RETURN (FLOAT(x));"

But he wrote "FLOAT(CARDINAL(x))", which is not necessarily equivalent!

>>>   PROCEDURE entier(x:REAL): INTEGER;
>>>just another form of INTEGER(TRUNC(x)) ?
>
>This one is a little different.  I understand it to be the "nearest integer
>to REAL x".  So I guess it is a TRUNC with rounding.  Any REAL number with
>a fractional part >= .5 is rounded up to the next highest integer.
>Anything < .5 is equivalent to TRUNC(x).

In PIM2/3rd Ed., pg. 162, Wirth states: 

  TRUNC(x)   real number x truncated to its integral part (of type
	     CARDINAL).


Wirth does not define "entier".    But when I researched it back in 1984,
If found it to be a europeanism for "floor", which does no rounding.

Now if I could just remember where I found that piece of information...


-- 
Alan Lovejoy; alan@pdn; 813-530-8241; Paradyne Corporation: Largo, Florida.
Disclaimer: Do not confuse my views with the official views of Paradyne
            Corporation (regardless of how confusing those views may be).
Motto: Never put off to run-time what you can do at compile-time!  

nagler%olsen@unizh.UUCP (Robert Nagler) (05/10/88)

Modula-2 doesn't allow conversion of reals to integers, i.e. you can't
convert a negative integer to a real or vice-versa.  Apparently, Dr. Wirth
found this to be a needed feature, so he included it in a library.  Doing
type coercions to CARDINAL while yield incorrect results (unless there
are other bugs in the language implementation).

Enclosed (below) you will find implementations of these two routines
which are a "best guess" at how these procedures might be implemented
in a portable library.  Most compiler companies supply libraries which
play around with assembly language or floating point chips, therefore
it is often fruitless to look at them for algorithms.

NOTE: Modula-2 has problems with twos complement integers (MININT),
therefore the implementations provided will fail if they are passed MININT.
One can handle the problem, but I felt it would detract from the presentation
of the algorithm if I included the special case code.

Rob

PS. If you are wondering about the FLOAT usage (i.e. FLOAT is passed
    an integer), Modula-2 states that cardinals and integers are assignment
    compatible which means you can pass positive INTEGERs to procedures
    that accept CARDINALs.
---------------------------------------------------------
PROCEDURE real( (* Convert an integer to a real *)
    x : INTEGER (* should be in the range: [ -MAX( x ) .. MAX( x ) ] *)
    ) : REAL;
    VAR
        result : REAL;
    BEGIN (* real *)
        IF x >= 0 THEN
            RETURN FLOAT( x );
        END;
        result := FLOAT( -x );  (* breaks here if MININT (except on Univacs) *)
        RETURN -result;
    END real;

PROCEDURE entier( (* Truncate a real towards negative infinity *)
    x : REAL      (* same restrictions as "real" *)
    ) : INTEGER;
    VAR
        result : INTEGER;
    BEGIN (* entier *)
        IF x > 0.0 THEN
            RETURN TRUNC( x );
        END;
        x := -x;
        result := TRUNC( x );
        (* If the number is not exact, must adjust towards negative infinity *)
        IF FLOAT( result ) # x THEN
            INC( result );
        END;
        RETURN -result;
    END entier;