comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: Q: Trouble creating array and discriminated type
Date: Sat, 21 Jan 2017 07:23:23 +0000
Date: 2017-01-21T07:23:23+00:00	[thread overview]
Message-ID: <lymvekkhas.fsf@pushface.org> (raw)
In-Reply-To: 83409c51-59d3-4205-9eeb-5467de09f069@googlegroups.com

b.mcguinness747@gmail.com writes:

> First, gnatmake complains about two constant arrays I am trying to
> create.  I have:
>
>   subtype Int2 is Integer range -32768..32767;
>   type    Real is digits 16;
>
> ...
>
>     a_napl_t : constant array (0..677,0..13) of Int2 := (
>       ( 0,  0,  0,  0,  0,  0,  0,  8,-16,  4,  5,  0,  0,  0),
>       ( 0,  0,  0,  0,  0,  0,  0, -8, 16, -4, -5,  0,  0,  2),
>
> ...
>     ));

Addressing the first problem only (for the moment) --

It's more normal Ada to let the compiler work out the size of the array
from the size of the data, something like this:

=============OX===============
with Ada.Text_IO; use Ada.Text_IO;

procedure B_M_1 is

   subtype Int2 is Integer range -32768 .. 32767;
   type    Real is digits 6; -- shorter string representation, see at end

   --  Declare an unconstrained array type, indexed from 0 (you
   --  probably don't want to use Integer here, because initialization
   --  with an aggregate will result in an object with indices running
   --  from Integer'First, -2**31, leading to confusion)
   type Napl_Data is array (Natural range <>, Natural range <>) of Int2;

   A_Napl_T : constant Napl_Data :=
     (
      ( 0,  0,  0,  0,  0,  0,  0,  8,-16,  4,  5,  0,  0,  0),
      ( 0,  0,  0,  0,  0,  0,  0, -8, 16, -4, -5,  0,  0,  2)
     );

   --  This time, make the indices run from 1 (many Ada programmers
   --  think this more natural :-)
   type Cpl_Data is array (Positive range <>, Positive range <>) of Real;

   A_Cpl_T : constant Cpl_Data :=
     (
      ( 1440.0,          0.0,          0.0,          0.0),
      (   56.0,       -117.0,        -42.0,        -40.0),
      (  125.0,        -43.0,          0.0,        -54.0)
     );

begin
   --  Print out the ranges (NB 'Img is a GNAT extension, not standard Ada)
   Put_Line ("A_Napl_T range: " & A_Napl_T'First (1)'Img
               & " .. " & A_Napl_T'Last (1)'Img
               & ", " & A_Napl_T'First (2)'Img
               & " .. " & A_Napl_T'Last (2)'Img);
   Put_Line ("A_Cpl_T range: " & A_Cpl_T'First (1)'Img
               & " .. " & A_Cpl_T'Last (1)'Img
               & ", " & A_Cpl_T'First (2)'Img
               & " .. " & A_Cpl_T'Last (2)'Img);

   --  Print out the values (of the shorter array!)
   for J in A_Cpl_T'Range (1) loop
      for K in A_Cpl_T'Range (2) loop
         Put (A_Cpl_T (J, K)'Img & " ");
      end loop;
      New_Line;
   end loop;
end B_M_1;
=============OX===============

which gives

$ ./b_m_1 
A_Napl_T range:  0 ..  1,  0 ..  13
A_Cpl_T range:  1 ..  3,  1 ..  4
 1.44000E+03  0.00000E+00  0.00000E+00  0.00000E+00 
 5.60000E+01 -1.17000E+02 -4.20000E+01 -4.00000E+01 
 1.25000E+02 -4.30000E+01  0.00000E+00 -5.40000E+01 

  parent reply	other threads:[~2017-01-21  7:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-20 22:55 Q: Trouble creating array and discriminated type b.mcguinness747
2017-01-20 23:48 ` gautier_niouzes
2017-01-21  7:23 ` Simon Wright [this message]
2017-01-21  9:41 ` Jeffrey R. Carter
2017-01-21 17:05 ` Stephen Leake
2017-01-22  5:12   ` Robert Eachus
2017-01-23  8:02   ` G.B.
2017-01-23  8:19     ` Simon Wright
2017-01-21 18:55 ` b.mcguinness747
replies disabled

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