comp.lang.ada
 help / color / mirror / Atom feed
From: anon@anon.org (anon)
Subject: Re: Type of subtraction operator
Date: Fri, 29 May 2009 02:09:30 GMT
Date: 2009-05-29T02:09:30+00:00	[thread overview]
Message-ID: <utHTl.36790$d36.24148@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: 8ae800c6-4307-4dc5-bf6b-d97101ae8521@x5g2000yqk.googlegroups.com

The deal is that the GNAT compiler, like most other compilers preform some 
inline type conversion first, then calculate the value of Z as a constant since 
both X and Y value are known. And no error because in the case of Ada,  
Natural is a subtype of the Integer type.

   Z : Integer := Integer ( X ) - Integer ( Y ) ; 

   -- converted to 

   Z : Integer := -2 ; 

But, if you use the following;

   Z : Integer := Natural ( X - Y ) ; 

   --  This should expand into the following statement unless you use 
   --  pragma Suppress ( Range_Check ) ; 
   --  Z : Integer := Integer ( Natural ( X - Y ) ) ;  
   --  then to  
   Z : Integer := Integer ( Natural ( -2 ) ) ;  


you will get the CONSTRAINT_ERROR.

Note: Also, using the two statements 

   Z : Integer := Natural ( X - Y ) ;  
   pragma Suppress ( Range_Check, On => Z ) ;

will will cause a CONSTRAINT_ERROR to occur at run time. But should it, 
since Range checking for Z has been suppressed. 




In <8ae800c6-4307-4dc5-bf6b-d97101ae8521@x5g2000yqk.googlegroups.com>, Maciej Sobczak <see.my.homepage@gmail.com> writes:
>According to 4.5.3/2, each numeric type has a subtraction operator
>with this specification:
>
>function "-"(Left, Right : T) return T
>
>This means that the type of difference is the same as the type of
>operands.
>
>Which means that if T is Natural, then I should expect
>CONSTRAINT_ERROR when the result of subtraction is less than 0.
>But it does not happen:
>
>with Ada.Text_IO;
>procedure Test is
>
>   X : Natural := 5;
>   Y : Natural := 7;
>   Z : Integer := X - Y;   -- here the result is not in Natural'Range
>
>begin
>   Ada.Text_IO.Put_Line (Integer'Image (Z));
>end Test;
>
>The above happily prints -2.
>I thought initially that it is a subtype that is responsible for this,
>but defining My_Natural type as a completely new type (with
>appropriate type cast when Z is initialized) gives the same result.
>
>I conclude that the effective type of difference (the return type of
>the "-" operator) is T'Base instead of T.
>Is that right? Is this a bug in AARM or did I misunderstand something?
>
>--
>Maciej Sobczak * www.msobczak.com * www.inspirel.com
>
>Database Access Library for Ada: www.inspirel.com/soci-ada




  parent reply	other threads:[~2009-05-29  2:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-28 21:23 Type of subtraction operator Maciej Sobczak
2009-05-28 21:54 ` Adam Beneschan
2009-05-29  2:09 ` anon [this message]
2009-05-29 15:24   ` Adam Beneschan
2009-05-30  7:47     ` anon
replies disabled

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