comp.lang.ada
 help / color / mirror / Atom feed
From: Gautier <Gautier.deMontmollin@Maths.UniNe.CH>
To: Andre Ratel <aratel@total.net>
Subject: Re: Ada vs Delphi?
Date: 1999/08/12
Date: 1999-08-12T00:00:00+00:00	[thread overview]
Message-ID: <37B27FE8.6E09569E@Maths.UniNe.CH> (raw)
In-Reply-To: 37b12a3f.43564067@news.total.net

> {$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/




  reply	other threads:[~1999-08-12  0:00 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-08-06  0:00 Ada vs Delphi? Andre Ratel
1999-08-06  0:00 ` Steve Doiel
1999-08-09  0:00   ` Paul Groves
1999-08-08  0:00     ` Steve Doiel
1999-08-10  0:00       ` Ray Blaak
1999-08-10  0:00         ` Steve Doiel
1999-08-14  0:00           ` Andre Ratel
1999-08-09  0:00     ` Aidan Skinner
1999-08-09  0:00     ` Gautier
1999-08-11  0:00       ` Andre Ratel
1999-08-12  0:00         ` Gautier [this message]
1999-08-14  0:00           ` Andre Ratel
1999-08-14  0:00             ` Gautier
1999-08-16  0:00               ` Gautier
1999-08-15  0:00             ` Steve Doiel
1999-08-17  0:00             ` Robert I. Eachus
1999-08-12  0:00         ` Gautier
1999-08-09  0:00     ` Robert Dewar
1999-08-11  0:00       ` Andre Ratel
1999-08-11  0:00         ` Robert Dewar
1999-08-14  0:00           ` Andre Ratel
1999-08-17  0:00             ` Robert I. Eachus
1999-08-11  0:00         ` Robert Dewar
1999-08-11  0:00           ` David Botton
1999-08-14  0:00           ` Andre Ratel
1999-08-11  0:00         ` Ted Dennison
1999-08-14  0:00           ` Andre Ratel
1999-08-16  0:00             ` Ted Dennison
1999-08-09  0:00     ` Robert Dewar
1999-08-10  0:00     ` Brian Rogoff
1999-08-11  0:00       ` Scientific calculations (was Re: Ada vs Delphi? ) Vladimir Olensky
1999-08-11  0:00         ` Robert Dewar
1999-08-11  0:00         ` Robert Dewar
1999-08-11  0:00           ` Vladimir Olensky
1999-08-13  0:00             ` Gautier
1999-08-13  0:00             ` Robert Dewar
1999-08-13  0:00               ` Brian Rogoff
1999-08-15  0:00               ` Vladimir Olensky
1999-08-15  0:00                 ` Vladimir Olensky
1999-08-11  0:00     ` Ada vs Delphi? Andre Ratel
1999-08-11  0:00       ` Ada vs Component Pascal (was: Ada vs Delphi?) Ted Dennison
1999-08-11  0:00       ` Ada vs Delphi? Robert Dewar
1999-08-14  0:00         ` Andre Ratel
1999-08-06  0:00 ` William Starner
1999-08-06  0:00   ` William Starner
1999-08-07  0:00   ` tmoran
1999-08-07  0:00     ` Aidan Skinner
1999-08-07  0:00       ` Gautier
1999-08-07  0:00 ` Gautier
replies disabled

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