[comp.lang.modula2] origins of entier

Barry_Cornelius@durham.ac.UK (05/11/88)

 ________________________________________________________________

 Message: 259300, 28 lines
 Posted: 9:21am BST, Mon May 9/88
 Subject: MathLib0 procedures :: "Michael Drechsler +49 (0201) 343062" <HPA111@E
 To: Barry Cornelius
 From: Coloured Books Server

 X-Agent: SPCP
 Via:      UK.AC.RL.EARN; Mon, 09 May 88 09:21:05 BST
 Received: from UKACRL by UK.AC.RL.IB (Mailer X1.25) with BSMTP id 3987; Mon, 09
           May 88 09:21:05 BS
 Received: by UKACRL (Mailer X1.25) id 3976; Mon, 09 May 88 09:21:02 BST
 Date:     Mon, 9 May 88 10:01:03 GMT
 Reply-To: Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 Sender:   Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 From:     "Michael Drechsler +49 (0201) 343062" <HPA111@EARN.DE0HRZ1A>
 Subject:  MathLib0 procedures
 To:       no name <barry_cornelius@UK.AC.DURHAM.MTS>

 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
 ________________________________________________________________

 Message: 259671, 36 lines
 Posted: 7:21pm BST, Mon May 9/88
 Subject: Re:  MathLib0 procedures :: Harvard Townsend <harv@EDU.KSU.CIS.HARRIS>
 To: Barry Cornelius
 From: Coloured Books Server

 X-Agent: SPCP
 Via:      UK.AC.RL.EARN; Mon, 09 May 88 19:21:34 BST
 Received: from UKACRL by UK.AC.RL.IB (Mailer X1.25) with BSMTP id 3351; Mon, 09
           May 88 19:21:34 BS
 Received: by UKACRL (Mailer X1.25) id 3342; Mon, 09 May 88 19:21:33 BST
 Date:     Mon, 9 May 88 09:02:31 CDT
 Reply-To: Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 Sender:   Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 From:     Harvard Townsend <harv@EDU.KSU.CIS.HARRIS>
 Subject:  Re:  MathLib0 procedures
 X-To:     HPA111@DE0HRZ1A
 To:       no name <barry_cornelius@UK.AC.DURHAM.MTS>

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

 ________________________________________________________________

 Message: 259863, 40 lines
 Posted: 12:14pm BST, Tue May 10/88
 Subject: Re: MathLib0 procedures :: Alan Lovejoy <pdn!alan@NET.UU.UUNET>
 To: Barry Cornelius
 From: Coloured Books Server

 X-Agent: SPCP
 Via:      UK.AC.RL.EARN; Tue, 10 May 88 12:12:58 BST
 Received: from UKACRL by UK.AC.RL.IB (Mailer X1.25) with BSMTP id 1460; Tue, 10
           May 88 12:12:58 BS
 Received: by UKACRL (Mailer X1.25) id 1376; Tue, 10 May 88 12:12:41 BST
 Date:     Tue, 10 May 88 01:34:24 GMT
 Reply-To: Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 Sender:   Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 Comments: Warning -- original Sender: tag was
           editor%ucf1vm.BITNET@jade.berkeley.e
 From:     Alan Lovejoy <pdn!alan@NET.UU.UUNET>
 Subject:  Re: MathLib0 procedures
 X-To:     info-modula-2@ucf1vm
 To:       no name <barry_cornelius@UK.AC.DURHAM.MTS>

 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!
 ________________________________________________________________

 Message: 259865, 46 lines
 Posted: 12:14pm BST, Tue May 10/88
 Subject: Re:  MathLib0 procedures :: Alan Lovejoy <pdn!alan@NET.UU.UUNET>
 To: Barry Cornelius
 From: Coloured Books Server

 X-Agent: SPCP
 Via:      UK.AC.RL.EARN; Tue, 10 May 88 12:13:04 BST
 Received: from UKACRL by UK.AC.RL.IB (Mailer X1.25) with BSMTP id 1476; Tue, 10
           May 88 12:13:04 BS
 Received: by UKACRL (Mailer X1.25) id 1407; Tue, 10 May 88 12:12:47 BST
 Date:     Tue, 10 May 88 01:58:27 GMT
 Reply-To: Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 Sender:   Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 Comments: Warning -- original Sender: tag was
           editor%ucf1vm.BITNET@jade.berkeley.e
 From:     Alan Lovejoy <pdn!alan@NET.UU.UUNET>
 Subject:  Re:  MathLib0 procedures
 X-To:     info-modula-2@ucf1vm
 To:       no name <barry_cornelius@UK.AC.DURHAM.MTS>

 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!
 ________________________________________________________________

 Message: 260023, 67 lines
 Posted: 5:34pm BST, Tue May 10/88
 Subject: Re:  MathLib0 procedures :: Robert Nagler <nagler%olsen%unizh.UUCP@EAR
 To: Barry Cornelius
 From: Coloured Books Server

 X-Agent: SPCP
 Via:      UK.AC.RL.EARN; Tue, 10 May 88 17:31:43 BST
 Received: from UKACRL by UK.AC.RL.IB (Mailer X1.25) with BSMTP id 3957; Tue, 10
           May 88 17:31:43 BS
 Received: by UKACRL (Mailer X1.25) id 3947; Tue, 10 May 88 17:31:43 BST
 Date:     Tue, 10 May 88 10:02:32 +0200
 Reply-To: Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 Sender:   Info-Modula-2 mailing list <INFO-M2@EARN.DB0TUI11>
 From:     Robert Nagler <nagler%olsen%unizh.UUCP@EARN.CERNVAX>
 Subject:  Re:  MathLib0 procedures
 X-To:     info-modula-2@klaus.UUCP
 To:       no name <barry_cornelius@UK.AC.DURHAM.MTS>

 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;

rbh@computer-science.nottingham.ac.UK (Roger Henry) (05/11/88)

Have you checked how these views compare with our proposals?