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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1de1981df403322c X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-11-08 13:58:25 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!82-43-33-75.cable.ubr01.croy.blueyonder.co.UK!not-for-mail From: Nick Roberts Newsgroups: comp.lang.ada Subject: Re: New limited range type? Date: Sat, 08 Nov 2003 21:58:19 +0000 Message-ID: References: <20619edc.0311071005.15e6b9a8@posting.google.com> NNTP-Posting-Host: 82-43-33-75.cable.ubr01.croy.blueyonder.co.uk (82.43.33.75) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 1068328704 49438738 82.43.33.75 (16 [25716]) User-Agent: Mozilla/5.0 (Windows; U; Win95; en-US; rv:1.5) Gecko/20031013 Thunderbird/0.3 X-Accept-Language: en-us, en In-Reply-To: <20619edc.0311071005.15e6b9a8@posting.google.com> Xref: archiver1.google.com comp.lang.ada:2265 Date: 2003-11-08T21:58:19+00:00 List-Id: Mike Silva wrote: > "Martin Dowie" wrote in message news:... > >>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