comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Question about base types
Date: 1997/01/30
Date: 1997-01-30T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023280003001970055250001@news.ni.net> (raw)
In-Reply-To: E4oE3q.1tq@world.std.com


In article <E4oE3q.1tq@world.std.com>, bobduff@world.std.com (Robert A
Duff) wrote:


>>type <anonomous parent of T> is System.Min_Int .. System.Max_Int;
>>subtype T is <anonomous parent of T> range 1 .. 10;
>>
>>Do I have this correct?
>
>Not exactly.  The above declaration declares a type.  Like all types, it
>has no name.  It has a "base range", which is chosen by the
>implementation.  The base range is NOT necessarily System.Min_Int
>.. System.Max_Int.  The RM requires that the base range include all the
>values in 1..10, and that it be symmetric about zero (or be symmetric
>plus one extra negative value).  So the compiler could choose -10..10 as
>the base range, or -11..10, or -1000..1000, or -1001..1000, etc.  The
>expectation is that the compiler will choose something efficient, so
>it's usually -2**15..2**15-1, or -2**31..2**31-1, or something like
>that.  The base range determines when overflow happens -- that is, the
>range used for intermediate results.  In particular, if the correct
>answer is in the base range, then the result of the expression is that
>correct answer.  Otherwise, the result is either the correct answer, or
>you get an overflow (and Constraint_Error is raised).

Can you point out to me where in the RM95 it states that the range of
T'Base must be symmetric around 0?

Can you point out to me where the RM95 states that the "portable range" of
T (our example here) need only be -10 .. 10?

This is sort of a bummer.  I had hoped that T'Base would give me something
nearer to System.Max_Int.  (But maybe you'll give me a reason why it won't
matter.)

Here's what I want to do.  Suppose I have a floating point heading type,
and I want to override its primitive add operator so that it behaves like a
modulo number, as you'd expect headings to do:

type Heading is digits 6 range 0.0 .. 360.0;

function "+" (L, R : Heading) return Heading;

function "-" (L, R : Heading) return Heading;

So that 

H : Heading := 359.0 + 2.0;   -- H has the value 1.0

H : Heading := 1.0 - 2.0;  -- H has the value 359.0

Can I do this implementation below, legally and portably?

function "+" (L, R : Heading) return Heading is
   Sum : constant Heading'Base := L + R;
begin
   if Sum > Heading'Last then
      return Sum - Heading'Last;
   else 
      return Sum;
   end if;
end;

Will the assignment to Sum ever cause Constraint_Error?

I could generalize this to be a generic:

generic
   type T is digits <>;
function Generic_Heading_Add (L, R : T) return T;

function Generic_Heading_Add (L, R : T) return T is
   Sum : constant T'Base := L + R;
begin
   if Sum > T'Last then
      return Sum - T'Last;
   else
      return Sum;
   end if;
end;

That way, I could use this any time I needed some sort of heading type

type Ownship_Heading is digits 4 range 0.0 .. 360.0;

function "+"  is new Generic_Heading_Add (Ownship_Heading);

So that I get this special "+" as the primitive op.

(I could generalize the algorithm even more, so I could use it for
Longitude types, too.)

The question is: what range of T'Base case I assume?  Am I guaranteed that
I won't get Constraint_Errors during the calculation of the  sum?  (Assume
the range of the actual type is 0 .. 360.)

Still confused...

matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  reply	other threads:[~1997-01-30  0:00 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-01-26  0:00 Question about base types Matthew Heaney
1997-01-27  0:00 ` Robert A Duff
1997-01-30  0:00   ` Matthew Heaney [this message]
1997-01-30  0:00     ` Matthew Heaney
1997-01-30  0:00     ` Robert Dewar
1997-01-30  0:00       ` Matthew Heaney
     [not found]     ` <EACHUS.97Feb3221558@spectre.mitre.org>
     [not found]       ` <dewar.855063927@merv>
     [not found]         ` <EACHUS.97Feb6145918@spectre.mitre.org>
     [not found]           ` <dewar.855276722@merv>
1997-02-07  0:00             ` Mats Weber
1997-02-08  0:00               ` Robert Dewar
1997-02-10  0:00                 ` Mats Weber
1997-02-11  0:00                   ` Robert Dewar
     [not found]         ` <32FB45D4.2160@watson.ibm.com>
1997-02-10  0:00           ` Robert Dewar
1997-02-08  0:00   ` Robert Dewar
1997-02-09  0:00     ` Matthew Heaney
1997-02-09  0:00       ` Robert Dewar
1997-02-09  0:00         ` Matthew Heaney
1997-02-10  0:00           ` Robert Dewar
1997-02-10  0:00       ` Larry Kilgallen
1997-02-11  0:00   ` Robert I. Eachus
1997-02-12  0:00     ` Robert Dewar
1997-01-27  0:00 ` Mats Weber
1997-01-28  0:00   ` Robert A Duff
1997-01-29  0:00   ` Robert Dewar
1997-01-28  0:00 ` Robert I. Eachus
1997-01-28  0:00   ` Mats Weber
1997-01-29  0:00 ` Robert I. Eachus
1997-01-30  0:00   ` Robert A Duff
replies disabled

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