comp.lang.ada
 help / color / mirror / Atom feed
From: Nick Roberts <nick.roberts@acm.org>
Subject: Re: New limited range type?
Date: Sat, 08 Nov 2003 21:58:19 +0000
Date: 2003-11-08T21:58:19+00:00	[thread overview]
Message-ID: <bojou0$1f4o0i$1@ID-25716.news.uni-berlin.de> (raw)
In-Reply-To: <20619edc.0311071005.15e6b9a8@posting.google.com>

Mike Silva wrote:

> "Martin Dowie" <martin.dowie@btopenworld.com> wrote in message news:<bogf20$q1$1@titan.btinternet.com>...
> 
>>Ok, as we're in "my favourite thing I'd like in Ada" season, here's mine...
>>
>>I'd like to be able to declare integer/float/fixed/decimal types that are
>>limited, at both ends, and never raise exceptions, e.g.
>>
>>   type Safe_Degrees is
>>      digits 6 limited range -180.0 .. +180.0;  -- new use of 'limited'
> 
> .....
> 
> If the details could be worked out I'd sure like to see it.  It would
> answer a *very* common need in the "real world" that Ada was designed
> to address.

I have an alternative proposal: a new language-defined attribute Conform, 
which would be applicable to any fixed-point subtype S, and would be a 
function with the profile:

    function S'Conform (X: in universal_real) return S;

If X lies within the range of S, the function returns S. If X > S'Last, the 
function returns S'Last. If X < S'First, the function returns S'First. The 
function has the convention intrinsic.

To recast Martin's example, we would have:

    declare
       type Working_Degrees is
          digits 6 range -100*180.0 .. +100*180.0;  -- normal fixed-point
       subtype Normal_Degrees is Working_Degrees range -180.0 .. +180.0;
       A, B, C : Normal_Degrees;
    begin
       B := 170.0;
       C := 50.0;
       A := Normal_Degrees'Conform(B + C);
       -- A equals +180.0 after this statement
       ...
    end;

Note how I define a fixed point type to provide a big enough base range (in 
this case 50 complete turns either way) for all calculations, and then a 
subtype of it to define the required confined or normalised range.

I think the explicit use of the Conform attribute aids readability. The 
reviewer will not be kept in the dark that something unusual is going on. 
In addition, there can be no ambiguity as to where, in a complex 
expression, a normalisation takes place. Finally, the change to the 
standard is lesser (merely a new attribute).

-- 
Nick Roberts




  reply	other threads:[~2003-11-08 21:58 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-07 15:51 New limited range type? Martin Dowie
2003-11-07 15:57 ` Hyman Rosen
2003-11-07 16:20   ` Martin Dowie
2003-11-07 16:36     ` Stephane Richard
2003-11-07 16:59     ` Hyman Rosen
2003-11-07 18:18       ` Martin Dowie
2003-11-07 20:11         ` Larry Hazel
2003-11-07 22:40           ` Martin Dowie
2003-11-07 23:31             ` Larry Hazel
2003-11-12  8:22       ` Jean-Pierre Rosen
2003-11-12 15:00         ` Hyman Rosen
2003-11-12 15:34           ` Robert I. Eachus
2003-11-12 17:37             ` tmoran
2003-11-07 18:19   ` Mike Silva
2003-11-07 19:03   ` Marin David Condic
2003-11-07 18:05 ` Mike Silva
2003-11-08 21:58   ` Nick Roberts [this message]
2003-11-09 14:34     ` Martin Dowie
2003-11-11  5:13       ` Nick Roberts
2003-11-11 10:18         ` Martin Dowie
2003-11-11 10:35         ` Martin Dowie
2003-11-11 10:41           ` Lutz Donnerhacke
2003-11-11 12:37             ` Marin David Condic
2003-11-11 14:19             ` Martin Dowie
2003-11-11 12:34           ` Marin David Condic
replies disabled

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