comp.lang.ada
 help / color / mirror / Atom feed
* operands of membership tests
@ 1997-10-26  0:00 Matthew Heaney
  1997-10-26  0:00 ` Robert Dewar
  1997-10-27  0:00 ` Tucker Taft
  0 siblings, 2 replies; 4+ messages in thread
From: Matthew Heaney @ 1997-10-26  0:00 UTC (permalink / raw)



A membership never raises Constraint_Error, but evaluation of its operands
might.  Suppose I do this

with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Fixed is

   type Fixed is delta 1.0 range 0.0 .. 256.0;
   for Fixed'Small use 1.0;

begin

  Put (Boolean'Image (33_000.0 in Fixed));

end;

Let's suppose 33_000.0 isn't even in the base range of Fixed.

Can evaluation of the literal 33_000.0 raise Constraint_Error?

In RM83 11.6 (9), the note states that an implementation is allowed to
replace the expression with a constant value (in my example, False), even
though the operand is outside the base range of the type.  What's the story
in Ada 95?

What's the more general case, for any relational operator?  Under what
circumstances does evaluation of (numeric) literals raise Constraint_Error? 
Under what circumstances can an implementation remove an expression it can
evaluate statically, containing (static) operands that are outside the base
range of the type?

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: operands of membership tests
  1997-10-26  0:00 operands of membership tests Matthew Heaney
@ 1997-10-26  0:00 ` Robert Dewar
  1997-10-26  0:00   ` Matthew Heaney
  1997-10-27  0:00 ` Tucker Taft
  1 sibling, 1 reply; 4+ messages in thread
From: Robert Dewar @ 1997-10-26  0:00 UTC (permalink / raw)



<<Let's suppose 33_000.0 isn't even in the base range of Fixed>>

Then this is obviously statically illegal, end of discussion





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

* Re: operands of membership tests
  1997-10-26  0:00 ` Robert Dewar
@ 1997-10-26  0:00   ` Matthew Heaney
  0 siblings, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1997-10-26  0:00 UTC (permalink / raw)



In article <dewar.877921177@merv>, dewar@merv.cs.nyu.edu (Robert Dewar) wrote:

><<Let's suppose 33_000.0 isn't even in the base range of Fixed>>
>
>Then this is obviously statically illegal, end of discussion

Perhaps I mistated my question; please forgive me.  The code below compiles
cleanly (using GNAT 3.10), and produces the following output:

33_000 > T'Base'Last: TRUE
33_000 in T: FALSE      
33_000 in T'Base: FALSE                                       

Since this code is "statically legal," then it begs another question: What
is the definition of "base range"?  I assumed that if 33_000 were greater
than T'Base'Last, then that meant it wasn't in the base range.  Is this
assumption incorrect?

Sometimes a (static) literal, outside the base range of the type, is
flagged as illegal at compile-time.  Yet in this example, we have a literal
outside the base range of the type, that isn't flagged.  My question is
this.  What are the circumstances under which a static literal expression
is illegal, because it's outside the base range of the type?  


-- STX
with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Mem is

   type Fixed is delta 1.0 range 0.0 .. 256.0;
   for Fixed'Small use 1.0;

begin

   Put ("33_000 > T'Base'Last: ");
   Put (Boolean'Image (33_000.0 > Fixed'Base'Last));
   New_Line;

   Put ("33_000 in T: ");
   Put (Boolean'Image (33_000.0 in Fixed));
   New_Line;

   Put ("33_000 in T'Base: ");
   Put (Boolean'Image (33_000.0 in Fixed'Base));
   New_Line;

end;
-- ETX

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

* Re: operands of membership tests
  1997-10-26  0:00 operands of membership tests Matthew Heaney
  1997-10-26  0:00 ` Robert Dewar
@ 1997-10-27  0:00 ` Tucker Taft
  1 sibling, 0 replies; 4+ messages in thread
From: Tucker Taft @ 1997-10-27  0:00 UTC (permalink / raw)



Matthew Heaney (mheaney@ni.net) wrote:

: A membership never raises Constraint_Error, but evaluation of its operands
: might.  Suppose I do this

: with Ada.Text_IO; use Ada.Text_IO;
: procedure Test_Fixed is

:    type Fixed is delta 1.0 range 0.0 .. 256.0;
:    for Fixed'Small use 1.0;

: begin

:   Put (Boolean'Image (33_000.0 in Fixed));

: end;

: ...
: Let's suppose 33_000.0 isn't even in the base range of Fixed.

: Can evaluation of the literal 33_000.0 raise Constraint_Error?

Not in this case, because Fixed is a static subtype, so the membership
test is static (in Ada 95, not Ada 83).
On the other hand, if the membership test had not been static,
then 33_000.0 would be illegal at compile-time, per RM95 4.9(35).

: In RM83 11.6 (9), the note states that an implementation is allowed to
: replace the expression with a constant value (in my example, False), even
: though the operand is outside the base range of the type.  What's the story
: in Ada 95?

The compiler is required to evaluate a membership test statically in Ada 95
if the operand and the target subtype are both static (see 4.9(11).

: What's the more general case, for any relational operator?  Under what
: circumstances does evaluation of (numeric) literals raise Constraint_Error? 
: Under what circumstances can an implementation remove an expression it can
: evaluate statically, containing (static) operands that are outside the base
: range of the type?

Evaluation of numeric literals, per se, doesn't raise Constraint_Error.
However, they are illegal if outside the base range of the expected
type when used in a non-static operation.  When used in a static
expression, overflow checks are not performed.  See RM95 4.9(33-35).

: --------------------------------------------------------------------
: Matthew Heaney
: Software Development Consultant
: <mailto:matthew_heaney@acm.org>
: (818) 985-1271

--
-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Burlington, MA  USA




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

end of thread, other threads:[~1997-10-27  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-26  0:00 operands of membership tests Matthew Heaney
1997-10-26  0:00 ` Robert Dewar
1997-10-26  0:00   ` Matthew Heaney
1997-10-27  0:00 ` Tucker Taft

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