From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.68.202.99 with SMTP id kh3mr2725693pbc.8.1400097468185; Wed, 14 May 2014 12:57:48 -0700 (PDT) X-Received: by 10.50.120.1 with SMTP id ky1mr893423igb.7.1400097468089; Wed, 14 May 2014 12:57:48 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!newsfeed.fsmpi.rwth-aachen.de!feeder.erje.net!us.feeder.erje.net!news.glorb.com!c1no6736967igq.0!news-out.google.com!qf4ni926igc.0!nntp.google.com!c1no6736956igq.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 14 May 2014 12:57:47 -0700 (PDT) In-Reply-To: <50562e0a-3dfa-44c4-9aaa-70cbe304b54b@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=KSa2aQoAAACOxnC0usBJYX8NE3x3a1Xq NNTP-Posting-Host: 66.126.103.122 References: <50562e0a-3dfa-44c4-9aaa-70cbe304b54b@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <40c7405d-c4c2-4163-a430-01052b769866@googlegroups.com> Subject: Re: Bug or feature? From: Adam Beneschan Injection-Date: Wed, 14 May 2014 19:57:48 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:19827 Date: 2014-05-14T12:57:47-07:00 List-Id: On Wednesday, May 14, 2014 12:06:52 PM UTC-7, Laurent wrote: > Hi >=20 > While studying recursion I tried to develop an iterative version of the f= ibonacci sequence. >=20 > It works but on N>47 the function outputs negative numbers,buffer overflo= w. No difference between my iterative version and the recursive one given i= n the book (recursion is quite slow...). >=20 > Build a small test program which throws a Constraint error. So why does t= he test functions correctly where as the fibonacci program doesn't? When you ran the small test program, what value did you put in for N? When= I compile with GNAT and don't use -gnato, I find that the program gives me= an exception when N=3D6 but not when N=3D5. I think it matters whether (N= * 2_000_000_000) mod 32 produces a value with the high bit set or not. If= it's set, then it looks like a negative number. In any event, if you're using GNAT, it doesn't check for overflow automatic= ally; you have to use -gnato to get it to check that arithmetic operations = don't overflow. Your Buffer_Overflow test is not really a valid test, beca= use it's possible that the operation N * 2_000_000_000 will overflow, and t= he overflow won't be checked, but the result will appear to be a negative n= umber, which will violate the range checks on Positive. It does appear that GNAT may not always check the range properly when a fun= ction whose result is Positive tries to return a negative value due to over= flow. That would explain why the recursive version doesn't get an exceptio= n. -- Adam