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 autolearn=ham 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!l25g2000prn.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Truncating a fixed-point value Date: Fri, 9 Jul 2010 14:52:52 -0700 (PDT) Organization: http://groups.google.com Message-ID: <5a96a81e-12a6-4f30-84c3-4b76e079b198@l25g2000prn.googlegroups.com> References: <732bea9d-aacb-4b87-b64b-657aabce6736@w31g2000yqb.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1278712372 14358 127.0.0.1 (9 Jul 2010 21:52:52 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 9 Jul 2010 21:52:52 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: l25g2000prn.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:12300 Date: 2010-07-09T14:52:52-07:00 List-Id: On Jul 9, 1:39=A0pm, Gene wrote: > On Jul 9, 3:06=A0pm, Adam Beneschan wrote: > > > 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 define= d > > 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? > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- Adam > > There is a prior discussion of this question: > > http://coding.derkeiler.com/Archive/Ada/comp.lang.ada/2003-10/0729.html Thanks for the link. I was aware of AI95-60 but I hadn't been aware that the problem had been brought up again. > The problem is that fixed point types don't have to include all the > integers. Ah, I see. This is a problem because 'Truncation and 'Floor are defined to return the same fixed-point type. It seems to me that the language could use a way to do the truncation (or floor) and return an integer, without having to worry about an intermediate result of the same fixed-point type (which might not be a possible value of the type). I think someone mentioned that in the thread, but mentioned that it would probably have to be generic. > An ugly approach is to convert to e.g. a long float, take the floor, > and convert back. =A0No doubt others will have something more elegant. That will probably work for my purpose; but in general, it's easy to define a fixed-point number with a 'Small of 2**(-N) that has more bits to the right of the binary point than are available in the mantissa part of the floating-point. If that's the case, this approach will fail on certain values (probably M - (K * T'Small) for integer M and suitably small integer K). Since this situation lends itself to really efficient code (basically just a bit shift), it's kind of a shame that the language doesn't give you a good way to do this. -- Adam