comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: conversions between fixed-point types
Date: Fri, 18 Sep 2009 15:42:18 -0700 (PDT)
Date: 2009-09-18T15:42:18-07:00	[thread overview]
Message-ID: <57bc59c4-d556-473a-8179-5f715e380a9b@p10g2000prm.googlegroups.com> (raw)
In-Reply-To: h90uf3$bdo$2@news.albasani.net

On Sep 18, 2:35 pm, Dirk Herrmann <fight_s...@invalid.invalid> 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.  This language
> offers special support for fixed-point arithmetics.  I am investigating
> whether Ada would be a better choice, and thus the fixed-point handling
> is important.  Note 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
>     type FpA is delta 0.5 range -10.0 .. +10.0;
>     for FpA'Small use 0.5;
>     type FpB is delta 0.4 range -10.0 .. +10.0;
>     for FpB'Small use 0.4;
> conversions from FpA to FpB work as follows:
>
>     FpA       FpB
>     -1.5 ---> -1.2
>     -1.0 ---> -0.8
>     -0.5 ---> -0.4
>     -0.0 ---> -0.0
>     +0.5 ---> +0.4
>     +1.0 ---> +0.8
>     +1.5 ---> +1.2
>
> That is, the conversion is performed similar to a truncation (always
> choosing the closest value towards zero).  However, in principle there
> are other possibilities how the conversion could be done:
>
>                truncation  floor  ceiling  round
>     -1.5 --->  -1.2        -1.6   -1.2     -1.6
>     -1.0 --->  -0.8        -1.2   -0.8     -1.2
>     -0.5 --->  -0.4        -0.8   -0.4     -0.4
>      0.0 --->   0.0         0.0    0.0      0.0
>     +0.5 --->  +0.4        +0.4   +0.8     +0.4
>     +1.0 --->  +0.8        +0.8   +1.2     +1.2
>     +1.5 --->  +1.2        +1.2   +1.6     +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.  All
> of the above (and probably more options) seem to be legal, and GNAT just
> chooses truncation.  Is 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.  It might be possible that for
> different conversions within the same program different strategies are
> chosen.  Regarding 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?  Maybe I
> have missed something and the language itself offers some possibility?
> Is there, for example, some attribute S'Round(X : some fixed point type)
>   that rounds X to the nearest value of S?  I 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?  As 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 := 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



  reply	other threads:[~2009-09-18 22:42 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-18 21:35 conversions between fixed-point types Dirk Herrmann
2009-09-18 22:42 ` Adam Beneschan [this message]
2009-09-19 12:41 ` Dirk Herrmann
2009-09-19 14:48   ` John B. Matthews
2009-09-20  8:15     ` Dirk Herrmann
2009-09-20 14:22       ` Robert A Duff
2009-09-20 18:55         ` Dirk Herrmann
2009-09-20 20:34           ` Simon Clubley
2009-09-23 20:46             ` Dirk Herrmann
2009-09-27 17:15               ` Simon Clubley
2009-09-27 19:22           ` sjw
2009-09-28 20:18             ` Dirk Herrmann
2009-09-28 18:37           ` Robert A Duff
2009-09-28 20:50             ` Dirk Herrmann
2009-09-20 15:18       ` John B. Matthews
2009-09-20 19:13         ` Dirk Herrmann
2009-09-20 20:09       ` tmoran
2009-09-21 17:24       ` Jeffrey R. Carter
2009-09-23 20:57         ` Dirk Herrmann
2009-09-23 22:28           ` Jeffrey R. Carter
2009-09-24  1:05             ` Adam Beneschan
2009-09-24  3:57               ` Jeffrey R. Carter
2009-09-25  8:47               ` Stuart
2009-09-25 20:41                 ` sjw
2009-09-25 21:58                   ` Jeffrey R. Carter
2009-09-28 13:40                   ` Stuart
2009-09-26 14:43                 ` Dirk Herrmann
2009-09-28 15:15                   ` Adam Beneschan
2009-09-26 14:31               ` Dirk Herrmann
2009-09-19 18:38   ` tmoran
2009-09-20  8:22 ` sjw
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox