Mendal@SU-SIERRA.ARPA (Geoff Mendal) (07/07/86)
Here's another brain twister from the hackers at Stanford. We're trying to determine whether the following predefined integer type is valid (assume that it is the only predefined integer type): type Integer is range 1 .. 0; The real question here is whether a null range is "symmetric about zero", as required for all predefined integer types. Also at issue is whether System.Min_Int *must* be negative, and likewise System.Max_Int positive. We don't know the answer, so any help is appreciated. LRM references: 3.5(3); 3.5.4(6..7,13); 13.7.1(2..3) gom -------
lwall@sdcrdcf.UUCP (Larry Wall) (07/10/86)
In article <12220812234.39.MENDAL@su-sierra.arpa> Mendal@SU-SIERRA.ARPA (Geoff Mendal) writes: > We're trying to determine whether the following predefined > integer type is valid (assume that it is the only predefined > integer type): > > type Integer is range 1 .. 0; Maybe not, but perhaps this is: type Integer is range 0 .. -1; You see, a legal range for a pre-defined integer type is one of the two following: -n .. n -n .. n-1 But 1 .. 0 doesn't fit either of these. 0 .. -1 does, though. :-) > The real question here is whether a null range is "symmetric about > zero", as required for all predefined integer types. Also at > issue is whether System.Min_Int *must* be negative, and likewise > System.Max_Int positive. Min_Int and Max_Int are of type universal_integer, which of course contains both positive and negative numbers. However, any implicit conversion of Min_Int or Max_Int to type Integer will raise Constraint_Error! Assuming an implementation with Integer range 0..-1, you couldn't say, for instance: X: Integer; ... if (X = System.Min_Int) - illegal Interestingly enough, I think you CAN say: if (System.Max_Int >= System.Min_Int) - legal, no conversion since there is no implicit conversion. Likewise: if (X in System.Min_Int .. System.Max_Int) -- illegal? if (0 in System.Min_Int .. System.Max_Int) -- legal, and false!?!? But, if (X in Integer) -- legal! (I think) if (0 in Integer) -- illegal??? I dunno. -- Is the 0 converted? Tell me if I'm all wet. In particular, see 4.6(15) and 4.5.2(12). So, the moral of the story is, if you want your program to be portable to an implementation where Min_Int > Max_Int, be sure to guard any "dangerous" code with: if (0 in System.Min_Int .. System.Max_Int) then X := 0; -- non-portable code else Ask_Dumb_Question("Can I ask a dumb question? "); end if; Larry "tour de farce" Wall sdcrdcf!lwall