comp.lang.ada
 help / color / mirror / Atom feed
* Beginner issue..
@ 2013-06-16  6:54 Marcus F
  2013-06-16  7:00 ` Marcus F
  2013-06-16  7:30 ` Dirk Heinrichs
  0 siblings, 2 replies; 8+ messages in thread
From: Marcus F @ 2013-06-16  6:54 UTC (permalink / raw)


Hi!

I'm Marcus, I'm completely new to ADA but not to programming.
I'm looking at a simple tutorial guide to pick up some basics of ADA, but I ran into an issue that I don't understand.

Here's the trivial example I followed:

with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
 
procedure Compute is

 procedure Double(Item : in out Integer) is
 begin -- procedure Double.
   Item := Item * 2;
 end Double;

 X : Integer := 1;   -- Local variable X of type Integer.

begin -- procedure Compute
 loop
  Put(X);
  New_Line;
  Double(X);
 end loop;
end Compute;


Now, I expect X to double once per loop, but when I build and run, I just get 0's scrolling down my screen.

Could someone help out a newbie?


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

* Re: Beginner issue..
  2013-06-16  6:54 Beginner issue Marcus F
@ 2013-06-16  7:00 ` Marcus F
  2013-06-16  7:21   ` Dmitry A. Kazakov
  2013-06-16  7:30 ` Dirk Heinrichs
  1 sibling, 1 reply; 8+ messages in thread
From: Marcus F @ 2013-06-16  7:00 UTC (permalink / raw)


On Sunday, June 16, 2013 1:54:29 AM UTC-5, Marcus F wrote:
> Hi!
> 
> 
> 
> I'm Marcus, I'm completely new to ADA but not to programming.
> 
> I'm looking at a simple tutorial guide to pick up some basics of ADA, but I ran into an issue that I don't understand.
> 
> 
> 
> Here's the trivial example I followed:
> 
> 
> 
> with Ada.Text_IO, Ada.Integer_Text_IO;
> 
> use Ada.Text_IO, Ada.Integer_Text_IO;
> 
>  
> 
> procedure Compute is
> 
> 
> 
>  procedure Double(Item : in out Integer) is
> 
>  begin -- procedure Double.
> 
>    Item := Item * 2;
> 
>  end Double;
> 
> 
> 
>  X : Integer := 1;   -- Local variable X of type Integer.
> 
> 
> 
> begin -- procedure Compute
> 
>  loop
> 
>   Put(X);
> 
>   New_Line;
> 
>   Double(X);
> 
>  end loop;
> 
> end Compute;
> 
> 
> 
> 
> 
> Now, I expect X to double once per loop, but when I build and run, I just get 0's scrolling down my screen.
> 
> 
> 
> Could someone help out a newbie?

Let me rephrase that, I just removed the loop part, and the variable and Double seems fine, and displays the values I expect, but why would it just show a single line of 0's when in a loop?


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

* Re: Beginner issue..
  2013-06-16  7:00 ` Marcus F
@ 2013-06-16  7:21   ` Dmitry A. Kazakov
  2013-06-16  7:52     ` Marcus F
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry A. Kazakov @ 2013-06-16  7:21 UTC (permalink / raw)


On Sun, 16 Jun 2013 00:00:33 -0700 (PDT), Marcus F wrote:

> On Sunday, June 16, 2013 1:54:29 AM UTC-5, Marcus F wrote:
>> 
>> I'm looking at a simple tutorial guide to pick up some basics of ADA, but
>> I ran into an issue that I don't understand.

OK, this is a GNAT Ada compiler pitfall almost anybody runs into.

When you compile an Ada program with GNAT, NEVER EVER forget to turn the
run-time integer overflow checks on. E.g. in your case, you should compile
your program as follows:

   gnatmake -gnato compute.adb

The switch -gnato instructs GNAT to check integer overflows, which is what
happens. After that the output would look like:

          1
          2
          4
          8
         16
         32
         64
        128
        256
        512
       1024
       2048
       4096
       8192
      16384
      32768
      65536
     131072
     262144
     524288
    1048576
    2097152
    4194304
    8388608
   16777216
   33554432
   67108864
  134217728
  268435456
  536870912
 1073741824

raised CONSTRAINT_ERROR : compute.adb:8 overflow check failed

Without checks on, you get an overflow. For a number of power of two the
overflow results in 0. 0*2=0, which is what you observe on the screen.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

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

* Re: Beginner issue..
  2013-06-16  6:54 Beginner issue Marcus F
  2013-06-16  7:00 ` Marcus F
@ 2013-06-16  7:30 ` Dirk Heinrichs
  1 sibling, 0 replies; 8+ messages in thread
From: Dirk Heinrichs @ 2013-06-16  7:30 UTC (permalink / raw)


Marcus F wrote:

> Now, I expect X to double once per loop, but when I build and run, I just
> get 0's scrolling down my screen.

Adding to what Dmitry already wrote: You don't see just 0's. You actually 
see the number sequence from Dmitry's reply, followed by 0's, but it's so 
fast that you don't recognize. You can verify this by running

./compute|less

Bye...

	Dirk
-- 
Dirk Heinrichs <dirk.heinrichs@altum.de>
Tel: +49 (0)2471 209385 | Mobil: +49 (0)176 34473913
GPG Public Key C2E467BB | Jabber: dirk.heinrichs@altum.de

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

* Re: Beginner issue..
  2013-06-16  7:21   ` Dmitry A. Kazakov
@ 2013-06-16  7:52     ` Marcus F
  2013-06-16  7:55       ` Marcus F
  0 siblings, 1 reply; 8+ messages in thread
From: Marcus F @ 2013-06-16  7:52 UTC (permalink / raw)


On Sunday, June 16, 2013 2:21:55 AM UTC-5, Dmitry A. Kazakov wrote:
> On Sun, 16 Jun 2013 00:00:33 -0700 (PDT), Marcus F wrote:
> 
> 
> 
> > On Sunday, June 16, 2013 1:54:29 AM UTC-5, Marcus F wrote:
> 
> >> 
> 
> >> I'm looking at a simple tutorial guide to pick up some basics of ADA, but
> 
> >> I ran into an issue that I don't understand.
> 
> 
> 
> OK, this is a GNAT Ada compiler pitfall almost anybody runs into.
> 
> 
> 
> When you compile an Ada program with GNAT, NEVER EVER forget to turn the
> 
> run-time integer overflow checks on. E.g. in your case, you should compile
> 
> your program as follows:
> 
> 
> 
>    gnatmake -gnato compute.adb
> 
> 
> 
> The switch -gnato instructs GNAT to check integer overflows, which is what
> 
> happens. After that the output would look like:
> 
> 
> 
>           1
> 
>           2
> 
>           4
> 
>           8
> 
>          16
> 
>          32
> 
>          64
> 
>         128
> 
>         256
> 
>         512
> 
>        1024
> 
>        2048
> 
>        4096
> 
>        8192
> 
>       16384
> 
>       32768
> 
>       65536
> 
>      131072
> 
>      262144
> 
>      524288
> 
>     1048576
> 
>     2097152
> 
>     4194304
> 
>     8388608
> 
>    16777216
> 
>    33554432
> 
>    67108864
> 
>   134217728
> 
>   268435456
> 
>   536870912
> 
>  1073741824
> 
> 
> 
> raised CONSTRAINT_ERROR : compute.adb:8 overflow check failed
> 
> 
> 
> Without checks on, you get an overflow. For a number of power of two the
> 
> overflow results in 0. 0*2=0, which is what you observe on the screen.
> 
> 
> 
> -- 
> 
> Regards,
> 
> Dmitry A. Kazakov
> 
> http://www.dmitry-kazakov.de

Thank you Dmitry!

Now I just need to figure out how to turn on the Integer Overflow checks..
(I'm using AdaGIDE, just hitting save, build, run)

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

* Re: Beginner issue..
  2013-06-16  7:52     ` Marcus F
@ 2013-06-16  7:55       ` Marcus F
  2013-06-16  8:49         ` Dmitry A. Kazakov
  0 siblings, 1 reply; 8+ messages in thread
From: Marcus F @ 2013-06-16  7:55 UTC (permalink / raw)


On Sunday, June 16, 2013 2:52:57 AM UTC-5, Marcus F wrote:
> On Sunday, June 16, 2013 2:21:55 AM UTC-5, Dmitry A. Kazakov wrote:
> 
> > On Sun, 16 Jun 2013 00:00:33 -0700 (PDT), Marcus F wrote:
> 
> > 
> 
> > 
> 
> > 
> 
> > > On Sunday, June 16, 2013 1:54:29 AM UTC-5, Marcus F wrote:
> 
> > 
> 
> > >> 
> 
> > 
> 
> > >> I'm looking at a simple tutorial guide to pick up some basics of ADA, but
> 
> > 
> 
> > >> I ran into an issue that I don't understand.
> 
> > 
> 
> > 
> 
> > 
> 
> > OK, this is a GNAT Ada compiler pitfall almost anybody runs into.
> 
> > 
> 
> > 
> 
> > 
> 
> > When you compile an Ada program with GNAT, NEVER EVER forget to turn the
> 
> > 
> 
> > run-time integer overflow checks on. E.g. in your case, you should compile
> 
> > 
> 
> > your program as follows:
> 
> > 
> 
> > 
> 
> > 
> 
> >    gnatmake -gnato compute.adb
> 
> > 
> 
> > 
> 
> > 
> 
> > The switch -gnato instructs GNAT to check integer overflows, which is what
> 
> > 
> 
> > happens. After that the output would look like:
> 
> > 
> 
> > 
> 
> > 
> 
> >           1
> 
> > 
> 
> >           2
> 
> > 
> 
> >           4
> 
> > 
> 
> >           8
> 
> > 
> 
> >          16
> 
> > 
> 
> >          32
> 
> > 
> 
> >          64
> 
> > 
> 
> >         128
> 
> > 
> 
> >         256
> 
> > 
> 
> >         512
> 
> > 
> 
> >        1024
> 
> > 
> 
> >        2048
> 
> > 
> 
> >        4096
> 
> > 
> 
> >        8192
> 
> > 
> 
> >       16384
> 
> > 
> 
> >       32768
> 
> > 
> 
> >       65536
> 
> > 
> 
> >      131072
> 
> > 
> 
> >      262144
> 
> > 
> 
> >      524288
> 
> > 
> 
> >     1048576
> 
> > 
> 
> >     2097152
> 
> > 
> 
> >     4194304
> 
> > 
> 
> >     8388608
> 
> > 
> 
> >    16777216
> 
> > 
> 
> >    33554432
> 
> > 
> 
> >    67108864
> 
> > 
> 
> >   134217728
> 
> > 
> 
> >   268435456
> 
> > 
> 
> >   536870912
> 
> > 
> 
> >  1073741824
> 
> > 
> 
> > 
> 
> > 
> 
> > raised CONSTRAINT_ERROR : compute.adb:8 overflow check failed
> 
> > 
> 
> > 
> 
> > 
> 
> > Without checks on, you get an overflow. For a number of power of two the
> 
> > 
> 
> > overflow results in 0. 0*2=0, which is what you observe on the screen.
> 
> > 
> 
> > 
> 
> > 
> 
> > -- 
> 
> > 
> 
> > Regards,
> 
> > 
> 
> > Dmitry A. Kazakov
> 
> > 
> 
> > http://www.dmitry-kazakov.de
> 
> 
> 
> Thank you Dmitry!
> 
> 
> 
> Now I just need to figure out how to turn on the Integer Overflow checks..
> 
> (I'm using AdaGIDE, just hitting save, build, run)

I just looked around the settings, and the box for Integer Overflow Checks, is checked for the Debug setting, and I'm using Debug mode.


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

* Re: Beginner issue..
  2013-06-16  7:55       ` Marcus F
@ 2013-06-16  8:49         ` Dmitry A. Kazakov
  2013-06-16 18:32           ` Marcus F
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry A. Kazakov @ 2013-06-16  8:49 UTC (permalink / raw)


On Sun, 16 Jun 2013 00:55:51 -0700 (PDT), Marcus F wrote:

> I just looked around the settings, and the box for Integer Overflow
> Checks, is checked for the Debug setting, and I'm using Debug mode.

I didn't use AdaGIDE for eons. Have you changed the source file in order to
force recompiling?

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


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

* Re: Beginner issue..
  2013-06-16  8:49         ` Dmitry A. Kazakov
@ 2013-06-16 18:32           ` Marcus F
  0 siblings, 0 replies; 8+ messages in thread
From: Marcus F @ 2013-06-16 18:32 UTC (permalink / raw)


On Sunday, June 16, 2013 3:49:52 AM UTC-5, Dmitry A. Kazakov wrote:
> On Sun, 16 Jun 2013 00:55:51 -0700 (PDT), Marcus F wrote:
> 
> 
> 
> > I just looked around the settings, and the box for Integer Overflow
> 
> > Checks, is checked for the Debug setting, and I'm using Debug mode.
> 
> 
> 
> I didn't use AdaGIDE for eons. Have you changed the source file in order to
> 
> force recompiling?
> 
> 
> 
> -- 
> 
> Regards,
> 
> Dmitry A. Kazakov
> 
> http://www.dmitry-kazakov.de

I did, but the checkboxes were marked from the start, so it should have been used anyway.

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

end of thread, other threads:[~2013-06-16 18:32 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-06-16  6:54 Beginner issue Marcus F
2013-06-16  7:00 ` Marcus F
2013-06-16  7:21   ` Dmitry A. Kazakov
2013-06-16  7:52     ` Marcus F
2013-06-16  7:55       ` Marcus F
2013-06-16  8:49         ` Dmitry A. Kazakov
2013-06-16 18:32           ` Marcus F
2013-06-16  7:30 ` Dirk Heinrichs

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