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-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!p10g2000prm.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: conversions between fixed-point types Date: Fri, 18 Sep 2009 15:42:18 -0700 (PDT) Organization: http://groups.google.com Message-ID: <57bc59c4-d556-473a-8179-5f715e380a9b@p10g2000prm.googlegroups.com> References: 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 1253313738 21798 127.0.0.1 (18 Sep 2009 22:42:18 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 18 Sep 2009 22:42:18 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p10g2000prm.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),gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:8388 Date: 2009-09-18T15:42:18-07:00 List-Id: On Sep 18, 2:35=A0pm, Dirk Herrmann wrote: > Hi, > > I am currently trying to learn about Ada's fixed-point types, and there > are some points that I couldn't find any statement about, or which I > simply could not understand. > > (Background: At my company there is a lot of code that makes heavy use > of fixed-point calculations in a proprietary language. =A0This language > offers special support for fixed-point arithmetics. =A0I am investigating > whether Ada would be a better choice, and thus the fixed-point handling > is important. =A0Note that the decision to use fixed-point arithmetics is > due to the fact that the target processors do not have a floating point > unit - and this will remain this way for some time.) > > The first set of questions I have is about conversions between different > fixed-point types. > > I realized that with GNAT for the following example types > =A0 =A0 type FpA is delta 0.5 range -10.0 .. +10.0; > =A0 =A0 for FpA'Small use 0.5; > =A0 =A0 type FpB is delta 0.4 range -10.0 .. +10.0; > =A0 =A0 for FpB'Small use 0.4; > conversions from FpA to FpB work as follows: > > =A0 =A0 FpA =A0 =A0 =A0 FpB > =A0 =A0 -1.5 ---> -1.2 > =A0 =A0 -1.0 ---> -0.8 > =A0 =A0 -0.5 ---> -0.4 > =A0 =A0 -0.0 ---> -0.0 > =A0 =A0 +0.5 ---> +0.4 > =A0 =A0 +1.0 ---> +0.8 > =A0 =A0 +1.5 ---> +1.2 > > That is, the conversion is performed similar to a truncation (always > choosing the closest value towards zero). =A0However, in principle there > are other possibilities how the conversion could be done: > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0truncation =A0floor =A0ceiling =A0round > =A0 =A0 -1.5 ---> =A0-1.2 =A0 =A0 =A0 =A0-1.6 =A0 -1.2 =A0 =A0 -1.6 > =A0 =A0 -1.0 ---> =A0-0.8 =A0 =A0 =A0 =A0-1.2 =A0 -0.8 =A0 =A0 -1.2 > =A0 =A0 -0.5 ---> =A0-0.4 =A0 =A0 =A0 =A0-0.8 =A0 -0.4 =A0 =A0 -0.4 > =A0 =A0 =A00.0 ---> =A0 0.0 =A0 =A0 =A0 =A0 0.0 =A0 =A00.0 =A0 =A0 =A00.0 > =A0 =A0 +0.5 ---> =A0+0.4 =A0 =A0 =A0 =A0+0.4 =A0 +0.8 =A0 =A0 +0.4 > =A0 =A0 +1.0 ---> =A0+0.8 =A0 =A0 =A0 =A0+0.8 =A0 +1.2 =A0 =A0 +1.2 > =A0 =A0 +1.5 ---> =A0+1.2 =A0 =A0 =A0 =A0+1.2 =A0 +1.6 =A0 =A0 +1.6 > > First question: > > There does not seem to be any definite statement in the reference manual > of Ada 2005 about how exactly the conversion has to be performed. =A0All > of the above (and probably more options) seem to be legal, and GNAT just > chooses truncation. =A0Is that right, or have I already missed something? I think you're right, even if the implementation supports the Numerics Annex (based on G.2.3(10,24)). > I have not even found a statement that a compiler has to be consistent > with respect to the strategy it chooses. =A0It might be possible that for > different conversions within the same program different strategies are > chosen. =A0Regarding GNAT, I could not find any statement in the GNAT > documentation about whether GNAT will do it the same way throughout. > > Second question: > > Is there any way to control how the conversion is performed? =A0Maybe I > have missed something and the language itself offers some possibility? > Is there, for example, some attribute S'Round(X : some fixed point type) > =A0 that rounds X to the nearest value of S? =A0I could not find anything > like that. > > Or, if this is not the case, do any libraries exist that provide generic > conversion functions to achieve some or all of the "truncate", "floor", > "ceil" or "round" behaviours? =A0As I mentioned at the top of the mail, > any solutions that would require conversions between fixed-point and > floating-point to implement these conversions would probably not be > acceptable for our systems. The following seems to work if you want to round: X : FpA; Y : FpB; Y :=3D Integer (X / FpB'Small) * FpB'Small; The conversion to Integer will round (4.6(33)). I didn't check the resulting code, but this shouldn't use any floating-point operations. Unfortunately, I can't think of a good solution if you want to insist (portably) on truncation, even though GNAT does this by default. Floor and Ceiling probably require you to write a conditional that behaves differently depending on whether X is < 0 or > 0, but on most processors that is probably about as efficient as if support for this existed in the language and the compiler generated the code itself. -- Adam