comp.lang.ada
 help / color / mirror / Atom feed
* Simple loop with a strange output
@ 2013-05-23 13:53 Luca Cappelletti
  2013-05-23 13:55 ` Luca Cappelletti
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Luca Cappelletti @ 2013-05-23 13:53 UTC (permalink / raw)


Hello,

I'm using GNAT 4.6 via Debian 7

I've just compiled this simple code coming from a tutorial where I've just a little bit changed to fit my test:


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

    procedure Raddoppia(Elemento: in out Integer) is
    begin
    
        Elemento := Elemento * 2;
    
    end Raddoppia;

X : Integer;

begin

    Put_Line("start counting");
    New_Line;

    X := 1; 
    while X < 1009999999 loop
        Put(X);
        New_Line;
        Raddoppia(X);
    end loop;

end Hello;

that produce the expected:

$./hello
Ciao a tutti sto usando Put_Line direttamente con la clausola use

          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


now that strange error:

if you change the code using only the simple "loop - end loop" or change the number into the while from 1009999999 to 1099999999

my output will be an infinite sequence of zero

$./hello
0
0
0
0
...
0
...



do you know what's happening?

thank in advance,

Luca

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

* Re: Simple loop with a strange output
  2013-05-23 13:53 Simple loop with a strange output Luca Cappelletti
@ 2013-05-23 13:55 ` Luca Cappelletti
  2013-05-23 14:37 ` Adam Beneschan
  2013-05-23 20:41 ` Dirk Heinrichs
  2 siblings, 0 replies; 10+ messages in thread
From: Luca Cappelletti @ 2013-05-23 13:55 UTC (permalink / raw)


Il giorno giovedì 23 maggio 2013 15:53:19 UTC+2, Luca Cappelletti ha scritto:
> Hello,
> 
> 
> 
> I'm using GNAT 4.6 via Debian 7
> 
> 
> 
> I've just compiled this simple code coming from a tutorial where I've just a little bit changed to fit my test:
> 
> 
> 
> 
> 
> with Ada.Text_IO, Ada.Integer_Text_IO;
> 
> use Ada.Text_IO, Ada.Integer_Text_IO;
> 
> procedure Hello is
> 
> 
> 
>     procedure Raddoppia(Elemento: in out Integer) is
> 
>     begin
> 
>     
> 
>         Elemento := Elemento * 2;
> 
>     
> 
>     end Raddoppia;
> 
> 
> 
> X : Integer;
> 
> 
> 
> begin
> 
> 
> 
>     Put_Line("start counting");
> 
>     New_Line;
> 
> 
> 
>     X := 1; 
> 
>     while X < 1009999999 loop
> 
>         Put(X);
> 
>         New_Line;
> 
>         Raddoppia(X);
> 
>     end loop;
> 
> 
> 
> end Hello;
> 
> 
> 
> that produce the expected:
> 
> 
> 
> $./hello
> 
> Ciao a tutti sto usando Put_Line direttamente con la clausola use
> 

sorry there was a paste error from 2 text files the right output was:

$ ./hello
Start counting

          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

thanks

Luca

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

* Re: Simple loop with a strange output
  2013-05-23 13:53 Simple loop with a strange output Luca Cappelletti
  2013-05-23 13:55 ` Luca Cappelletti
@ 2013-05-23 14:37 ` Adam Beneschan
  2013-05-23 14:41   ` Adam Beneschan
  2013-05-23 20:41 ` Dirk Heinrichs
  2 siblings, 1 reply; 10+ messages in thread
From: Adam Beneschan @ 2013-05-23 14:37 UTC (permalink / raw)


On Thursday, May 23, 2013 6:53:19 AM UTC-7, Luca Cappelletti wrote:

> do you know what's happening?

Overflow.

The largest possible value of an "integer" type on this platform is (2**31)-1.  So when X hits 2**30 and then you double it, the actual value, 2**31, won't fit in an integer.  If you use the right flags when compiling (I don't remember what they are), this will cause a Constraint_Error to be raised.  But since you didn't use those flags, the result will be whatever the processor returns, which will be -2**31.  Since this is less than 1099999999, it will loop back and try to double it again; now when you double it the processor will return 0, and then you get stuck in an infinite loop.

                          -- Adam

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

* Re: Simple loop with a strange output
  2013-05-23 14:37 ` Adam Beneschan
@ 2013-05-23 14:41   ` Adam Beneschan
  2013-05-23 16:09     ` Luca Cappelletti
  0 siblings, 1 reply; 10+ messages in thread
From: Adam Beneschan @ 2013-05-23 14:41 UTC (permalink / raw)


On Thursday, May 23, 2013 7:37:24 AM UTC-7, Adam Beneschan wrote:
> On Thursday, May 23, 2013 6:53:19 AM UTC-7, Luca Cappelletti wrote:
 
> > do you know what's happening?
 
> Overflow.
> 
> The largest possible value of an "integer" type on this platform is (2**31)-1.  So when X hits 2**30 and then you double it, the actual value, 2**31, won't fit in an integer.  If you use the right flags when compiling (I don't remember what they are), 

OK, if you compile with -gnato, it will do the check, and now the program will die on a Constraint_Error instead of getting into an infinite loop.

                            -- Adam



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

* Re: Simple loop with a strange output
  2013-05-23 14:41   ` Adam Beneschan
@ 2013-05-23 16:09     ` Luca Cappelletti
  2013-05-23 16:40       ` Simon Wright
  0 siblings, 1 reply; 10+ messages in thread
From: Luca Cappelletti @ 2013-05-23 16:09 UTC (permalink / raw)


Il giorno giovedì 23 maggio 2013 16:41:24 UTC+2, Adam Beneschan ha scritto:
> On Thursday, May 23, 2013 7:37:24 AM UTC-7, Adam Beneschan wrote:
> 
> > On Thursday, May 23, 2013 6:53:19 AM UTC-7, Luca Cappelletti wrote:
> 
>  
> 
> > > do you know what's happening?
> 
>  
> 
> > Overflow.
> 
> > 
> 
> > The largest possible value of an "integer" type on this platform is (2**31)-1.  So when X hits 2**30 and then you double it, the actual value, 2**31, won't fit in an integer.  If you use the right flags when compiling (I don't remember what they are), 
> 
> 
> 
> OK, if you compile with -gnato, it will do the check, and now the program will die on a Constraint_Error instead of getting into an infinite loop.
> 

Yes this is the new output:

$ gnatmake -gnato hello.adb 
gcc-4.4 -c -gnato hello.adb
gnatbind -x hello.ali
gnatlink hello.ali
$ ./hello
Start counting

          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 : hello.adb:9 overflow check failed

do you know why the overflow checking is off by default?
I mean I expect gnat force me to understand my errors via it's own debug messages instead works closed how gcc do for C leaving developer into the dark

thank you very much

Luca


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

* Re: Simple loop with a strange output
  2013-05-23 16:09     ` Luca Cappelletti
@ 2013-05-23 16:40       ` Simon Wright
  2013-05-23 21:34         ` Randy Brukardt
  0 siblings, 1 reply; 10+ messages in thread
From: Simon Wright @ 2013-05-23 16:40 UTC (permalink / raw)


Luca Cappelletti <luca.cappelletti@gmail.com> writes:

> do you know why the overflow checking is off by default?

The explanation is here:
http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Controlling-Run_002dTime-Checks.html#index-g_t_0040option_007b_002dgnato_007d-_0028_0040command_007bgcc_007d_0029-595

or here, if that link got broken: http://goo.gl/ffIjp

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

* Re: Simple loop with a strange output
  2013-05-23 13:53 Simple loop with a strange output Luca Cappelletti
  2013-05-23 13:55 ` Luca Cappelletti
  2013-05-23 14:37 ` Adam Beneschan
@ 2013-05-23 20:41 ` Dirk Heinrichs
  2 siblings, 0 replies; 10+ messages in thread
From: Dirk Heinrichs @ 2013-05-23 20:41 UTC (permalink / raw)


Luca Cappelletti wrote:

> if you change the code using only the simple "loop - end loop" or change
> the number into the while from 1009999999 to 1099999999
> 
> my output will be an infinite sequence of zero
> 
> $./hello
> 0
> 0
> 0
> 0
> ...
> 0
> ...

Did you leave something out in your output? I get:

% ./hello
start counting

          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
-2147483648
          0
          0
          0
          0
          0

And this is what I would expect, taking into account Adams and Simons 
replies.

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] 10+ messages in thread

* Re: Simple loop with a strange output
  2013-05-23 16:40       ` Simon Wright
@ 2013-05-23 21:34         ` Randy Brukardt
  2013-05-24  7:21           ` Simon Wright
  2013-05-24 12:46           ` Britt
  0 siblings, 2 replies; 10+ messages in thread
From: Randy Brukardt @ 2013-05-23 21:34 UTC (permalink / raw)


"Simon Wright" <simon@pushface.org> wrote in message 
news:lyppwh8x9y.fsf@pushface.org...
> Luca Cappelletti <luca.cappelletti@gmail.com> writes:
>
>> do you know why the overflow checking is off by default?
>
> The explanation is here:
> http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Controlling-Run_002dTime-Checks.html#index-g_t_0040option_007b_002dgnato_007d-_0028_0040command_007bgcc_007d_0029-595
>
> or here, if that link got broken: http://goo.gl/ffIjp

That's the official line, but I think if you asked most people at AdaCore 
today, they'd tell you that it was a mistake.

But mistakes like this one aren't really fixable, because of all of the 
existing code that would be at risk of breaking if they changed the default. 
(That code would technically be wrong, but I doubt that would be much 
consolation if it meant that the train refused to stop.)

This sort of thing is going to happen in any compiler that's been around a 
while. There are certainly defaults in Janus/Ada that I wouldn't make the 
default today, but breaking existing projects isn't really an option, so 
such changes hardly ever are made.

                                              Randy.


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

* Re: Simple loop with a strange output
  2013-05-23 21:34         ` Randy Brukardt
@ 2013-05-24  7:21           ` Simon Wright
  2013-05-24 12:46           ` Britt
  1 sibling, 0 replies; 10+ messages in thread
From: Simon Wright @ 2013-05-24  7:21 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:

> "Simon Wright" <simon@pushface.org> wrote in message 
> news:lyppwh8x9y.fsf@pushface.org...
>> Luca Cappelletti <luca.cappelletti@gmail.com> writes:
>>
>>> do you know why the overflow checking is off by default?
>>
>> The explanation is here:
>> http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Controlling-Run_002dTime-Checks.html#index-g_t_0040option_007b_002dgnato_007d-_0028_0040command_007bgcc_007d_0029-595
>>
>> or here, if that link got broken: http://goo.gl/ffIjp
>
> That's the official line, but I think if you asked most people at
> AdaCore today, they'd tell you that it was a mistake.
>
> But mistakes like this one aren't really fixable, because of all of
> the existing code that would be at risk of breaking if they changed
> the default.  (That code would technically be wrong, but I doubt that
> would be much consolation if it meant that the train refused to stop.)
>
> This sort of thing is going to happen in any compiler that's been
> around a while. There are certainly defaults in Janus/Ada that I
> wouldn't make the default today, but breaking existing projects isn't
> really an option, so such changes hardly ever are made.

And most of us would agree that it was a mistake, too.

All my projects (free, and the ones I used to be paid for) use -gnato by
default. They don't use -fstack-check, because it wasn't reliable when I
was working out the project settings (and I could have really used it
when I needed a 500K stack to process some XML!).

I seriously doubt that any project where there was a possibility of a
train failing to stop would do something as risky as changing the
compiler without a thorough risk analysis. I wasn't allowed to, and
there were no trains involved (just point-defence missiles!)


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

* Re: Simple loop with a strange output
  2013-05-23 21:34         ` Randy Brukardt
  2013-05-24  7:21           ` Simon Wright
@ 2013-05-24 12:46           ` Britt
  1 sibling, 0 replies; 10+ messages in thread
From: Britt @ 2013-05-24 12:46 UTC (permalink / raw)


On Thursday, May 23, 2013 5:34:47 PM UTC-4, Randy Brukardt wrote:

> That's the official line, but I think if you asked most people
> at AdaCore today, they'd tell you that it was a mistake. But
> mistakes like this one aren't really fixable, because of all
> of the existing code that would be at risk of breaking if they
> changed the default.

I also view it was a mistake.  I think there may have been an opportunity to correct it by making -gnato (overflow checking) implicit when the compiler is in Ada 2012 mode. Then a new switch would be needed to turn it off. This approach would have had little risk of breaking much existing code.

- Britt


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

end of thread, other threads:[~2013-05-24 12:46 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-23 13:53 Simple loop with a strange output Luca Cappelletti
2013-05-23 13:55 ` Luca Cappelletti
2013-05-23 14:37 ` Adam Beneschan
2013-05-23 14:41   ` Adam Beneschan
2013-05-23 16:09     ` Luca Cappelletti
2013-05-23 16:40       ` Simon Wright
2013-05-23 21:34         ` Randy Brukardt
2013-05-24  7:21           ` Simon Wright
2013-05-24 12:46           ` Britt
2013-05-23 20:41 ` Dirk Heinrichs

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