From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d57302f2954365e1 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Question about base types Date: 1997/01/30 Message-ID: #1/1 X-Deja-AN: 213194359 references: content-type: text/plain; charset=ISO-8859-1 organization: Estormza Software mime-version: 1.0 newsgroups: comp.lang.ada Date: 1997-01-30T00:00:00+00:00 List-Id: In article , bobduff@world.std.com (Robert A Duff) wrote: >>type is System.Min_Int .. System.Max_Int; >>subtype T is 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 (818) 985-1271