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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6a77269912f77a70 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-24 21:29:48 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.mathworks.com!wn13feed!wn11feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi_feed4!attbi.com!attbi_s54.POSTED!not-for-mail Message-ID: <3F99FBE1.1030601@comcast.net> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Why no 'Floor for fixed point types References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.34.139.183 X-Complaints-To: abuse@comcast.net X-Trace: attbi_s54 1067056187 24.34.139.183 (Sat, 25 Oct 2003 04:29:47 GMT) NNTP-Posting-Date: Sat, 25 Oct 2003 04:29:47 GMT Organization: Comcast Online Date: Sat, 25 Oct 2003 04:29:47 GMT Xref: archiver1.google.com comp.lang.ada:1646 Date: 2003-10-25T04:29:47+00:00 List-Id: Nick Roberts wrote: > For a fixed point type Tf -- assuming it has a delta which is less than > 1 -- you can write: > > Scale: constant := 1/Tf'Small; > > type Ti is range Tf'First*Scale .. Tf'Last*Scale; > > function Scaled_Floor (X: in Tf) return Ti is > begin > return Ti(X*Scale) - Ti(X*Scale) mod Ti(Scale); > end; > > The basic idea is that with fixed point types, a 'floor' function will > inevitably need to be scaled, and the integer type it returns needs to > be customised to the fixed point type (its range and delta) and the > chosen scale. I've shown a scale of 1/Tf'Small, but the chances are that > the scale you will require in a certain situation will be different (it > might be unity). Ignore this. To quote RM 3.5.9(8): "The set of values of a fixed point type comprise the integral multiples of a number called the small of the type." So the effect of all the code above can be better accomplished by: function Mantissa(X: in Tf) return Ti is begin return Ti(X/Tf'Small); end Mantissa; -- Technically you could use the constant named Scale above, but there -- are some potential rounding issues. Writing it this way allows the -- compiler to convert the division to a multiplication that gives the -- same result, if it can. -- Of course, any decent compiler should be able to recognize this -- special case and do nothing. If your compiler doesn't recognize this -- you could rewrite Mantissa as an instance of Unchecked_Conversion. -- Robert I. Eachus "Quality is the Buddha. Quality is scientific reality. Quality is the goal of Art. It remains to work these concepts into a practical, down-to-earth context, and for this there is nothing more practical or down-to-earth than what I have been talking about all along...the repair of an old motorcycle." -- from Zen and the Art of Motorcycle Maintenance by Robert Pirsig