comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: Why no 'Floor for fixed point types
Date: Sat, 25 Oct 2003 04:29:47 GMT
Date: 2003-10-25T04:29:47+00:00	[thread overview]
Message-ID: <3F99FBE1.1030601@comcast.net> (raw)
In-Reply-To: bnc6j8$v8sd4$1@ID-25716.news.uni-berlin.de

Nick Roberts wrote:

> For a fixed point type Tf -- assuming it has a delta which is less than 
> 1 -- you can write:
> 
>    Scale: constant := 1/Tf'Small;
> 
>    type Ti is range Tf'First*Scale .. Tf'Last*Scale;
> 
>    function Scaled_Floor (X: in Tf) return Ti is
>    begin
>       return Ti(X*Scale) - Ti(X*Scale) mod Ti(Scale);
>    end;
> 
> The basic idea is that with fixed point types, a 'floor' function will 
> inevitably need to be scaled, and the integer type it returns needs to 
> be customised to the fixed point type (its range and delta) and the 
> chosen scale. I've shown a scale of 1/Tf'Small, but the chances are that 
> the scale you will require in a certain situation will be different (it 
> might be unity).

Ignore this.  To quote RM 3.5.9(8): "The set of values of a fixed point 
type comprise the integral multiples of a number called the small of the 
type."  So the effect of all the code above can be better accomplished by:

   function Mantissa(X: in Tf) return Ti is
   begin return Ti(X/Tf'Small); end Mantissa;

-- Technically you could use the constant named Scale above, but there
-- are some potential rounding issues.  Writing it this way allows the
-- compiler to convert the division to a multiplication that gives the
-- same result, if it can.

-- Of course, any decent compiler should be able to recognize this
-- special case and do nothing.  If your compiler doesn't recognize this
-- you could rewrite Mantissa as an instance of Unchecked_Conversion.



-- 
                                                     Robert I. Eachus

"Quality is the Buddha. Quality is scientific reality. Quality is the 
goal of Art. It remains to work these concepts into a practical, 
down-to-earth context, and for this there is nothing more practical or 
down-to-earth than what I have been talking about all along...the repair 
of an old motorcycle."  -- from Zen and the Art of Motorcycle 
Maintenance by Robert Pirsig




  reply	other threads:[~2003-10-25  4:29 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-23 20:09 Why no 'Floor for fixed point types Duncan Sands
2003-10-23 22:06 ` Robert I. Eachus
2003-10-24 16:00   ` Stephen Leake
2003-10-24 18:13     ` Duncan Sands
2003-10-23 23:10 ` Martin Dowie
2003-10-24 21:46 ` Nick Roberts
2003-10-25  4:29   ` Robert I. Eachus [this message]
2003-10-25 20:42     ` Nick Roberts
2003-10-25 22:40       ` Robert I. Eachus
2003-10-27 18:59         ` Randy Brukardt
2003-10-28  1:19           ` Robert I. Eachus
2003-10-28 18:23             ` Nick Roberts
2003-10-28 18:34               ` Stephane Richard
2003-10-29 19:26               ` Randy Brukardt
2003-10-30  4:55                 ` Robert I. Eachus
2003-10-28 18:10         ` Nick Roberts
2003-10-27 18:49       ` Randy Brukardt
2003-10-28 18:32         ` Nick Roberts
2003-10-29 19:29           ` Randy Brukardt
2003-10-30 23:41             ` Nick Roberts
2003-10-31 22:25               ` Randy Brukardt
2003-11-06  2:41                 ` Nick Roberts
replies disabled

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