From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, MSGID_SHORT,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!swrinde!emory!hubcap!billwolf%hazel.cs.clemson.edu From: billwolf%hazel.cs.clemson.edu@hubcap.clemson.edu (William Thomas Wolfe, 2847 ) Newsgroups: comp.lang.ada Subject: Re: Truncation of FLOATs Message-ID: <7626@hubcap.clemson.edu> Date: 10 Jan 90 21:35:20 GMT References: <1550@castle.ed.ac.uk> Sender: news@hubcap.clemson.edu Reply-To: billwolf%hazel.cs.clemson.edu@hubcap.clemson.edu List-Id: >From ssj@castle.ed.ac.uk (S Johal): > [Trying to get the integer part of a floating point number:] > if FLOATING_NUMBER - ROUNDED >= 0.0 then > TRUNCATED_PART := ROUNDED; > else > TRUNCATED_PART := ROUNDED - 1; > end if; First, I currently handle it as follows: generic type FLOAT_TYPE is digits <>; type INTEGER_TYPE is range <>; function TRUNCATE (THE_FLOAT : in FLOAT_TYPE) return INTEGER_TYPE; -- Results may vary for values which are FLOAT_TYPE'EPSILON -- less than a whole number, depending on the compiler. It -- makes use of the compiler's integer-to-real conversion -- function, the semantics of which, at the point halfway -- between two whole numbers, is defined to be -- compiler-dependent (Ada 83 LRM 4.6 (7)). function TRUNCATE (THE_FLOAT : in FLOAT_TYPE) return INTEGER_TYPE is begin -- function TRUNCATE return INTEGER_TYPE (THE_FLOAT - (0.5 - FLOAT_TYPE'EPSILON)); -- works correctly @ boundary iff rounding is down... -- Another option is: -- return ( INTEGER_TYPE'(10.0 * THE_FLOAT) / 10 ); -- which eliminates the rounding error by truncating it away, -- but there are numeric overflow problems with this approach. end TRUNCATE; Fortunately, there is a new secondary standard from NUMWG which will provide a better-defined alternative; the proposed standard package GENERIC_PRIMITIVE_FUNCTIONS will provide a TRUNCATE function: function TRUNCATE (X : FLOAT_TYPE) return FLOAT_TYPE; -- may raise PRIMITIVE_FUNCTIONS_EXCEPTIONS.REPRESENTATION_ERROR I'm told that there will be an article posted in the near future which gives a comprehensive description of ths proposed standard, as well as another similar proposed standard package, GENERIC_ELEMENTARY_FUNCTIONS, so stay tuned. Bill Wolfe, wtwolfe@hubcap.clemson.edu