comp.lang.ada
 help / color / mirror / Atom feed
* conversions between fixed-point types
@ 2009-09-18 21:35 Dirk Herrmann
  2009-09-18 22:42 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 31+ messages in thread
From: Dirk Herrmann @ 2009-09-18 21:35 UTC (permalink / raw)


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 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.

Thanks in advance,

Dirk Herrmann
(email address gladly given on request)



^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2009-09-28 20:50 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-18 21:35 conversions between fixed-point types Dirk Herrmann
2009-09-18 22:42 ` Adam Beneschan
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

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