From: Adam Beneschan <adam@irvine.com>
Subject: Re: Weird behavior with modular type
Date: Tue, 7 Feb 2012 09:53:25 -0800 (PST)
Date: 2012-02-07T09:53:25-08:00 [thread overview]
Message-ID: <b4fa0455-8ec7-4eda-acc1-034773722d1a@n12g2000yqb.googlegroups.com> (raw)
In-Reply-To: op.v9bjzvhzule2fv@douda-yannick
On Feb 7, 9:24 am, Yannick Duchêne (Hibou57)
<yannick_duch...@yahoo.fr> wrote:
> Hi all,
>
> I expect the test to be shortened enough.
>
> The following compiles and run without an error:
>
> with Ada.Text_IO;
> procedure Bug is
>
> type Modular_Type is mod 256;
> subtype Index_Type is Natural range 1 .. 3;
>
> E : Modular_Type := 255;
> I : Index_Type := 3;
> B : Boolean;
>
> begin
> B := E < 10 ** I;
> Ada.Text_IO.Put_Line (Boolean'Image (B));
> end Bug;
>
> It prints "FALSE"
>
> Now, the following, with added "Natural'Pos (...)", did not even compile:
>
> with Ada.Text_IO;
> procedure Bug is
>
> type Modular_Type is mod 256;
> subtype Index_Type is Natural range 1 .. 3;
>
> E : Modular_Type := 255;
> I : Index_Type := 3;
> B : Boolean;
>
> begin
> B := E < Natural'Pos (10 ** I);
> Ada.Text_IO.Put_Line (Boolean'Image (B));
> end Bug;
>
> That's counter intuitive. I would expect the first to behave as the
> second, with either a failure to compile or range error at run-time.
>
> Someone remember a pointer to the most relevant part in the RM? I may be a
> bit tired, could not find it.
>
> Seems in the first case, the left-side of "E < 10 ** I" has a modulo
> applied, or else, I see no way to explain the result, "FALSE". But why
> would this modulo be applied with a subtype of Natural and not with an
> Universal_Integer?
>
> I don't like it (and must be idiot in some way, don't you think?).
In the first example, 10 ** I is a modular operation; the left
argument of ** is a modular type (whose value is 10), and the right
argument is of type Natural. (The right argument of the predefined
"**" is always of type Integer [sometimes subtype Natural], unlike
most other predefined binary operations which have the same type for
the left and right arguments. Maybe that's what's confusing you.)
Anyway, since 10 ** I is an operation on a modular type, it wraps
around. In the second example, 10 ** I is an operation on Natural,
because it's the expected type when you have Natural'Pos; here, the
left argument is an Integer value of type 10. So Natural'Pos (10 **
I) has value 1024, and it's an error to convert it to a modular type
when it's out of range.
-- Adam
next prev parent reply other threads:[~2012-02-07 18:21 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-07 17:24 Weird behavior with modular type Yannick Duchêne (Hibou57)
2012-02-07 17:49 ` Jeffrey Carter
2012-02-07 18:06 ` Adam Beneschan
2012-02-07 18:21 ` Yannick Duchêne (Hibou57)
2012-02-08 6:53 ` J-P. Rosen
2012-02-08 15:29 ` Adam Beneschan
2012-02-09 3:37 ` Randy Brukardt
2012-02-07 18:15 ` Yannick Duchêne (Hibou57)
2012-02-07 17:53 ` Adam Beneschan [this message]
2012-02-07 17:54 ` Gautier write-only
2012-02-07 18:23 ` Yannick Duchêne (Hibou57)
2012-02-09 3:40 ` Randy Brukardt
2012-02-07 18:02 ` Adam Beneschan
2012-02-07 18:32 ` Georg Bauhaus
2012-02-07 18:13 ` Yannick Duchêne (Hibou57)
2012-02-07 18:15 ` Adam Beneschan
2012-02-07 18:35 ` Yannick Duchêne (Hibou57)
2012-02-07 18:40 ` Adam Beneschan
2012-02-07 19:22 ` Yannick Duchêne (Hibou57)
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox