From: Adam Beneschan <adam@irvine.com>
Subject: Re: Truncating a fixed-point value
Date: Fri, 9 Jul 2010 14:52:52 -0700 (PDT)
Date: 2010-07-09T14:52:52-07:00 [thread overview]
Message-ID: <5a96a81e-12a6-4f30-84c3-4b76e079b198@l25g2000prn.googlegroups.com> (raw)
In-Reply-To: f7b0cc71-bf8c-48bb-b651-b32b30ae3d4a@y11g2000yqm.googlegroups.com
On Jul 9, 1:39 pm, Gene <gene.ress...@gmail.com> wrote:
> On Jul 9, 3:06 pm, Adam Beneschan <a...@irvine.com> 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 >= 0.0, and I need to
> > compute Floor(X) as an Integer. 'Truncation and 'Floor aren't defined
> > for fixed-point types; the type conversion Integer(X) rounds; and
> > Integer(X-0.5) fails if X=0.0.
>
> > How do others do this? Or do I have to resort to an IF statement (or
> > conditional expression in Ada 2012) to handle the different cases?
>
> > -- 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. No 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
next prev parent reply other threads:[~2010-07-09 21:52 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-09 19:06 Truncating a fixed-point value Adam Beneschan
2010-07-09 20:34 ` Simon Wright
2010-07-09 23:21 ` Adam Beneschan
2010-07-10 3:13 ` Gene
2010-07-11 19:43 ` Gene
2010-07-10 8:12 ` Simon Wright
2010-07-09 20:39 ` Gene
2010-07-09 21:20 ` Jeffrey R. Carter
2010-07-09 21:52 ` Adam Beneschan [this message]
2010-07-13 11:26 ` Jacob Sparre Andersen
2010-07-09 22:58 ` Shark8
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox