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,c7f5c70275787af8 X-Google-Attributes: gid103376,public From: Gautier Subject: Re: Ada vs Delphi? Date: 1999/08/12 Message-ID: <37B27FE8.6E09569E@Maths.UniNe.CH>#1/1 X-Deja-AN: 511770989 Content-Transfer-Encoding: 7bit References: <37ab421a.5414989@news.total.net> <37ab8bd1.0@news.pacifier.com> <37ae1fc8.653954@news.clara.net> <37AE980F.15E6A15C@maths.unine.ch> <37b12a3f.43564067@news.total.net> To: Andre Ratel Content-Type: text/plain; charset=us-ascii MIME-Version: 1.0 Newsgroups: comp.lang.ada Date: 1999-08-12T00:00:00+00:00 List-Id: > {$RANGECHECK ON} > > var > n1, n2: shortint; {range: -128 .. 127} > N: integer; {range: 2147483648 .. 2147483647} > > begin > n1:= 120; > n2:= 120; > N:= n1 + n2; > Writeln(N); > end; > >>>>>>>>>> > The result was, appropriately 240. > Now, if I know that n1 and n2 are both in the range -128..127 > while their sum is outside this range, I don't understand what's > wrong with assigning (n1 + n2) to an integer. Neither Delphi nor you can guess if the result of an expression like (n1 + n2) alone will be in some range. You just were lucky because Borland has choosen to convert your shortints *before* the addition! Let's see what happens in Turbo Pascal 6 (16 bit): {$R+} var n1, n2: shortint; {range: -128 .. 127} i1,i2: integer; {range: -32768..32767} N: longint; {range: -2147483648 .. 2147483647} begin n1:= 120; n2:= 120; N:= n1 + n2; Writeln(N); i1:= 32000; i2:= 32000; N:= i1 + i2; Writeln(N); end. The 1st result is 240: Borland has converted the signed 8-bit n1,n2 to 16-bit, then added them, converted to a 32-bit one (N). The second is catastrophic: -1536 with range checks ON ! What happened ? The addition is a normal 16-bit one. BUT it's an UNSIGNED one (0..65536), so not catched by range check: 64000 Then the thing (now again understood as a SIGNED thing: -1536) is converted to the signed longint !! The problem with these automatic castings is double, since Borland chooses the signed/unsigned and the 8/16/32/64 bits representation for you! In Ada you can handle ranges and bits independently: e.g. a 32-bit subtype of range -127..128, etc. etc. In the same subtype family (e.g. integer, natural, positive) you can write n1+n2 safely, without explicit conversion, as there is no bad surprise with hidden type conversions; ranges will be checked correctly - provided you didn't suppress them at compile time... -- Gautier -------- http://members.xoom.com/gdemont/