comp.lang.ada
 help / color / mirror / Atom feed
* Why this error, value not in range of subtype of "Standard.Integer"?
@ 2012-06-26 15:49 Nasser M. Abbasi
  2012-06-26 17:20 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Nasser M. Abbasi @ 2012-06-26 15:49 UTC (permalink / raw)


experts;

If I write this, I get an error as show: (notice the B
matrix declaration, that is where the problem is)

-------------- this does not work ------
with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;

procedure foo2 is
     A : constant Real_Matrix :=
              (( 1.0,  2.0,  3.0),
               ( 4.0,  5.0,  6.0),
               ( 7.0,  8.0,  9.0));
     B : Real_Matrix(1 .. 3,1 .. 3);  -- this is the problem
begin

     FOR I in A'range(1) LOOP
           FOR J in A'range(2) LOOP
               B(I,J) := 1.0;
           END LOOP;
     END LOOP;
     
end foo2;
-------------------------------

>gnatmake foo2.adb
gcc-4.6 -c foo2.adb
foo2.adb:13:17: warning: value not in range of subtype of "Standard.Integer" defined at line 8
foo2.adb:13:17: warning: "Constraint_Error" will be raised at run time
foo2.adb:13:19: warning: value not in range of subtype of "Standard.Integer" defined at line 8
foo2.adb:13:19: warning: "Constraint_Error" will be raised at run time
gnatbind -x foo2.ali
gnatlink foo2.ali

But if I change the declaration of B from
      B : Real_Matrix(1 .. 3,1 .. 3);
to
      B : Real_Matrix(A'RANGE(1), A'RANGE(2));

then Ada is happy now.

Isn't A'RANGE(1) the same as 1 .. 3 and A'RANGE(2) the same as 1 .. 3?
or is this one of those typing issues?

---------------------------------------------
with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;

procedure foo1 is
     A : constant Real_Matrix :=
              (( 1.0,  2.0,  3.0),
               ( 4.0,  5.0,  6.0),
               ( 7.0,  8.0,  9.0));
     B : Real_Matrix(A'RANGE(1), A'RANGE(2));
begin
     FOR I in A'range(1) LOOP
           FOR J in A'range(2) LOOP
               B(I,J) := 1.0;
           END LOOP;
     END LOOP;
         
end foo1;
------------------------------

(one thing I like about Ada, is that once the program compiles clean,
then it runs fine !)

thanks
--Nasser



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

* Re: Why this error, value not in range of subtype of "Standard.Integer"?
  2012-06-26 15:49 Why this error, value not in range of subtype of "Standard.Integer"? Nasser M. Abbasi
@ 2012-06-26 17:20 ` Adam Beneschan
  2012-06-26 18:00 ` Niklas Holsti
  2012-06-26 21:39 ` Robert A Duff
  2 siblings, 0 replies; 4+ messages in thread
From: Adam Beneschan @ 2012-06-26 17:20 UTC (permalink / raw)


On Tuesday, June 26, 2012 8:49:18 AM UTC-7, Nasser M. Abbasi wrote:
> experts;
> 
> If I write this, I get an error as show: (notice the B
> matrix declaration, that is where the problem is)
> 
> -------------- this does not work ------
> with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;
> 
> procedure foo2 is
>      A : constant Real_Matrix :=
>               (( 1.0,  2.0,  3.0),
>                ( 4.0,  5.0,  6.0),
>                ( 7.0,  8.0,  9.0));
>      B : Real_Matrix(1 .. 3,1 .. 3);  -- this is the problem
> begin
> 
>      FOR I in A'range(1) LOOP
>            FOR J in A'range(2) LOOP
>                B(I,J) := 1.0;
>            END LOOP;
>      END LOOP;
>      
> end foo2;
> -------------------------------
> 
> >gnatmake foo2.adb
> gcc-4.6 -c foo2.adb
> foo2.adb:13:17: warning: value not in range of subtype of "Standard.Integer" defined at line 8
> foo2.adb:13:17: warning: "Constraint_Error" will be raised at run time
> foo2.adb:13:19: warning: value not in range of subtype of "Standard.Integer" defined at line 8
> foo2.adb:13:19: warning: "Constraint_Error" will be raised at run time
> gnatbind -x foo2.ali
> gnatlink foo2.ali
> 
> But if I change the declaration of B from
>       B : Real_Matrix(1 .. 3,1 .. 3);
> to
>       B : Real_Matrix(A'RANGE(1), A'RANGE(2));
> 
> then Ada is happy now.
> 
> Isn't A'RANGE(1) the same as 1 .. 3 and A'RANGE(2) the same as 1 .. 3?
> or is this one of those typing issues?

A'Range(1) is the same as A'First(1) .. A'Last(1), which is in this case -2147483648 .. -2147483646, not 1 .. 3.  I've said more about this in my response on another thread.

                             -- Adam



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

* Re: Why this error, value not in range of subtype of "Standard.Integer"?
  2012-06-26 15:49 Why this error, value not in range of subtype of "Standard.Integer"? Nasser M. Abbasi
  2012-06-26 17:20 ` Adam Beneschan
@ 2012-06-26 18:00 ` Niklas Holsti
  2012-06-26 21:39 ` Robert A Duff
  2 siblings, 0 replies; 4+ messages in thread
From: Niklas Holsti @ 2012-06-26 18:00 UTC (permalink / raw)


On 12-06-26 18:49 , Nasser M. Abbasi wrote:
> experts;
>
> If I write this, I get an error as show: (notice the B
> matrix declaration, that is where the problem is)

No, the problem is in the declaration of the A matrix. Or really in the 
difference in their declarations.

> -------------- this does not work ------
> with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
>
> procedure foo2 is
> A : constant Real_Matrix :=
> (( 1.0, 2.0, 3.0),
> ( 4.0, 5.0, 6.0),
> ( 7.0, 8.0, 9.0));

The Real_Matrix type is defined in Ada.Numerics.Real_Arrays with Integer 
as the index type:

type Real_Matrix is array (Integer range <>, Integer range <>)
                                                    of Real'Base;

This means that the matrix A, which is constrained by its initial value, 
has the lower index bounds Integer'First, which in the case of 32-bit 
integers is -2147483648, and the upper index bound is Integer'First + 2, 
which is -2147483646. Far out of the range of the indices of B, 1..3.

Change the declaration of A to specify the index ranges:

    A : constant Real_Matrix (1 .. 3, 1 .. 3) :=

> But if I change the declaration of B from
> B : Real_Matrix(1 .. 3,1 .. 3);
> to
> B : Real_Matrix(A'RANGE(1), A'RANGE(2));
>
> then Ada is happy now.

Yep. Now B also has the index range Integer'First .. Integer'First + 2.

> Isn't A'RANGE(1) the same as 1 .. 3 and A'RANGE(2) the same as 1 .. 3?

That would be the case, if Real_Matrix were defined with Positive as the 
index range. But not with Integer.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



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

* Re: Why this error, value not in range of subtype of "Standard.Integer"?
  2012-06-26 15:49 Why this error, value not in range of subtype of "Standard.Integer"? Nasser M. Abbasi
  2012-06-26 17:20 ` Adam Beneschan
  2012-06-26 18:00 ` Niklas Holsti
@ 2012-06-26 21:39 ` Robert A Duff
  2 siblings, 0 replies; 4+ messages in thread
From: Robert A Duff @ 2012-06-26 21:39 UTC (permalink / raw)


"Nasser M. Abbasi" <nma@12000.org> writes:

> If I write this, I get an error as show: (notice the B
> matrix declaration, that is where the problem is)
>
> -------------- this does not work ------
> with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;
>
> procedure foo2 is
>     A : constant Real_Matrix :=
>              (( 1.0,  2.0,  3.0),
>               ( 4.0,  5.0,  6.0),
>               ( 7.0,  8.0,  9.0));
>     B : Real_Matrix(1 .. 3,1 .. 3);  -- this is the problem

Others answered your question, but:  You might find it convenient
to declare a subtype:

    subtype My_Real_Matrix is Real_Matrix(1..3, 1..3);
    A: constant My_Real_Matrix := ((1.0, ....
    B: My_Real_Matrix;

Also look into the "for ... of" syntax of Ada 2012.

- Bob



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

end of thread, other threads:[~2012-06-26 21:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-26 15:49 Why this error, value not in range of subtype of "Standard.Integer"? Nasser M. Abbasi
2012-06-26 17:20 ` Adam Beneschan
2012-06-26 18:00 ` Niklas Holsti
2012-06-26 21:39 ` Robert A Duff

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