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.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,9ea5d59767aa4a7d X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!i28g2000yqa.googlegroups.com!not-for-mail From: Gene Newsgroups: comp.lang.ada Subject: Re: Truncating a fixed-point value Date: Sun, 11 Jul 2010 12:43:15 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: <732bea9d-aacb-4b87-b64b-657aabce6736@w31g2000yqb.googlegroups.com> <6d238b76-10ba-4ac8-ac16-86bc691fc96f@v6g2000prd.googlegroups.com> <9377ec5d-cd11-41e5-8891-ce9436767bb4@j4g2000yqh.googlegroups.com> NNTP-Posting-Host: 184.12.83.126 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1278877395 4198 127.0.0.1 (11 Jul 2010 19:43:15 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 11 Jul 2010 19:43:15 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: i28g2000yqa.googlegroups.com; posting-host=184.12.83.126; posting-account=-BkjswoAAACC3NU8b6V8c50JQ2JBOs04 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; GTB6.5; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:12323 Date: 2010-07-11T12:43:15-07:00 List-Id: On Jul 9, 11:13=A0pm, Gene wrote: > On Jul 9, 7:21=A0pm, Adam Beneschan wrote: > > > > > > > On Jul 9, 1:34=A0pm, Simon Wright wrote: > > > > Adam Beneschan writes: > > > > I must be suffering from an embarrassing mental block, because I'm > > > > sure there's a simple solution, but I can't see what it is. > > > > > I have a value X of a fixed-point type, X >=3D 0.0, and I need to > > > > compute Floor(X) as an Integer. =A0'Truncation and 'Floor aren't de= fined > > > > for fixed-point types; the type conversion Integer(X) rounds; and > > > > Integer(X-0.5) fails if X=3D0.0. > > > > > How do others do this? =A0Or do I have to resort to an IF statement= (or > > > > conditional expression in Ada 2012) to handle the different cases? > > > > Looks as though Integer(X+0.5)-1 might do the trick .. > > > Yes, it looks like it, as long as X >=3D 0.0 and 0.5 is a model number > > of the fixed-point type (I think I'm using the right term). =A0Those > > conditions do hold for the code I need it for. =A0It isn't useful for > > negative numbers---it yields -2 if X=3D-1.0, which is not useful whethe= r > > you want a truncation or a floor operation---but I did stipulate > > nonnegative X in my original question. > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- Adam > > Shouldn't there be a way to convert a fixed point type to a > corresponding modular or integer type with one integer value per > floating point value, use rem to compute a floor, and convert back?- Hide= quoted text - I'll try to answer my own question. Here is an attempt that works for a variety of fixed point types. I'm no expert on fixed point. Perhaps I've embedded some non-portable assumptions. With optimizations turned up and range checks off, the Floor function inlines as the 3 (x86) instructions you'd expect (subtract, "and" with mask, add) using GPL 2007. with Ada.Text_IO; use Ada.Text_IO; procedure FPF is type FP is delta 0.125 range -10.0 .. 50.0; function Floor(X : FP) return FP is Delta_Inv : constant :=3D 1.0 / FP'Delta; type Bits is mod 2 ** 32; type FP_Bits is delta 1.0 range 0.0 .. (FP'Last - FP'First) * Delta_Inv; Ordinal : constant Bits :=3D Bits((X - FP'First) * Delta_Inv) and not(Bits(Delta_Inv) - 1); begin return FP'Base(FP_Bits(Ordinal) * FP'Delta) + FP'First; end; X : FP :=3D FP'First; begin loop Put_Line(FP'Image(X) & " -> " & FP'Image(Floor(X))); exit when X =3D FP'Last; X :=3D X + FP'Delta; end loop; end;