comp.lang.ada
 help / color / mirror / Atom feed
* Type of subtraction operator
@ 2009-05-28 21:23 Maciej Sobczak
  2009-05-28 21:54 ` Adam Beneschan
  2009-05-29  2:09 ` anon
  0 siblings, 2 replies; 5+ messages in thread
From: Maciej Sobczak @ 2009-05-28 21:23 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Type of subtraction operator
  2009-05-28 21:23 Type of subtraction operator Maciej Sobczak
@ 2009-05-28 21:54 ` Adam Beneschan
  2009-05-29  2:09 ` anon
  1 sibling, 0 replies; 5+ messages in thread
From: Adam Beneschan @ 2009-05-28 21:54 UTC (permalink / raw)


On May 28, 2:23 pm, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
> 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:

Natural is not a type.  It's a subtype.  The subtraction operator is
defined on the underlying type, Integer's type, rather than the
subtype.  (Integer is also a subtype, rather than a type; see 3.5.4
(11).)  The result of the function is then converted to the subtype
Integer, and no Constraint_Error occurs since -2 is within Integer's
range.


> 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.

My_Natural is not a type either.  It's a subtype of some new integer
base type whose base range will include both negative and positive
numbers (symmetric about zero)---see 3.5.4(1), 3.5.4(9).


> I conclude that the effective type of difference (the return type of
> the "-" operator) is T'Base instead of T.

No, it's T.  4.5.3(1) refers to T as a type, not a subtype.  Your
mistake, which is natural enough (hee hee hee), was in assuming that
My_Natural is a type.  The "-" operator is defined on the base type of
which My_Natural is a subtype.

Yep, it's confusing.  It took me some time to figure all this out too.

                                   -- Adam




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Type of subtraction operator
  2009-05-28 21:23 Type of subtraction operator Maciej Sobczak
  2009-05-28 21:54 ` Adam Beneschan
@ 2009-05-29  2:09 ` anon
  2009-05-29 15:24   ` Adam Beneschan
  1 sibling, 1 reply; 5+ messages in thread
From: anon @ 2009-05-29  2:09 UTC (permalink / raw)


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




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Type of subtraction operator
  2009-05-29  2:09 ` anon
@ 2009-05-29 15:24   ` Adam Beneschan
  2009-05-30  7:47     ` anon
  0 siblings, 1 reply; 5+ messages in thread
From: Adam Beneschan @ 2009-05-29 15:24 UTC (permalink / raw)


On May 28, 7:09 pm, a...@anon.org (anon) wrote:
> 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.

Seems OK to me.  The Constraint_Error is raised by evaluating the
expression on the right-hand side of the := and thus really has
nothing to do with Z.  Anyway, as AI95-224 says, "there is no clear
definition (or agreement) on" the meaning of the On parameter of
Suppress, which is why it's now an obsolescent feature.  So while it's
possible for two people to have two (or more) interpretations of what
On=>Z means in this case, the language standard doesn't help decide
which one is right.  Furthermore, there's no requirement for Ada
implementations to actually obey Suppress pragmas; code that uses
Suppress must still expect that the exceptions may be raised anyway,
and the AARM says the pragma should be used for efficiency only.

                                         -- Adam



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Type of subtraction operator
  2009-05-29 15:24   ` Adam Beneschan
@ 2009-05-30  7:47     ` anon
  0 siblings, 0 replies; 5+ messages in thread
From: anon @ 2009-05-30  7:47 UTC (permalink / raw)


I hope you feel better tomorrow, because you must of had a bad day!

If you could not see the joke in that comment! Or was it a lesson for the 
newbees programmers to discuss in class, about pragma location.

And as for for the Suppress/Restriction pragmas and the RM. A few Adacore 
staft member have said we do it our way not the RM way! That's processing 
is trickled downward.  And "Safety and Security" towers above Efficiency.


In <890d5a12-2a1d-4ee9-a443-324f5d60bfaf@n8g2000vbb.googlegroups.com>, Adam Beneschan <adam@irvine.com> writes:
>On May 28, 7:09=A0pm, a...@anon.org (anon) wrote:
>> Note: Also, using the two statements
>>
>> =A0 =A0Z : Integer :=3D Natural ( X - Y ) ; =A0
>> =A0 =A0pragma Suppress ( Range_Check, On =3D> Z ) ;
>>
>> will will cause a CONSTRAINT_ERROR to occur at run time. But should it,
>> since Range checking for Z has been suppressed.
>
>Seems OK to me.  The Constraint_Error is raised by evaluating the
>expression on the right-hand side of the :=3D and thus really has
>nothing to do with Z.  Anyway, as AI95-224 says, "there is no clear
>definition (or agreement) on" the meaning of the On parameter of
>Suppress, which is why it's now an obsolescent feature.  So while it's
>possible for two people to have two (or more) interpretations of what
>On=3D>Z means in this case, the language standard doesn't help decide
>which one is right.  Furthermore, there's no requirement for Ada
>implementations to actually obey Suppress pragmas; code that uses
>Suppress must still expect that the exceptions may be raised anyway,
>and the AARM says the pragma should be used for efficiency only.
>
>                                         -- Adam




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-05-30  7:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-28 21:23 Type of subtraction operator Maciej Sobczak
2009-05-28 21:54 ` Adam Beneschan
2009-05-29  2:09 ` anon
2009-05-29 15:24   ` Adam Beneschan
2009-05-30  7:47     ` anon

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