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.236.93.38 with SMTP id k26mr2252747yhf.20.1400094412748; Wed, 14 May 2014 12:06:52 -0700 (PDT) X-Received: by 10.140.46.7 with SMTP id j7mr41177qga.35.1400094412627; Wed, 14 May 2014 12:06:52 -0700 (PDT) Path: border1.nntp.dca3.giganews.com!backlog3.nntp.dca3.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!c1no6691633igq.0!news-out.google.com!qf4ni926igc.0!nntp.google.com!c1no6691632igq.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 14 May 2014 12:06:52 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=87.240.222.153; posting-account=sDyr7QoAAAA7hiaifqt-gaKY2K7OZ8RQ NNTP-Posting-Host: 87.240.222.153 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <50562e0a-3dfa-44c4-9aaa-70cbe304b54b@googlegroups.com> Subject: Bug or feature? From: Laurent Injection-Date: Wed, 14 May 2014 19:06:52 +0000 Content-Type: text/plain; charset=ISO-8859-1 X-Original-Bytes: 4572 Xref: number.nntp.dca.giganews.com comp.lang.ada:186408 Date: 2014-05-14T12:06:52-07:00 List-Id: Hi While studying recursion I tried to develop an iterative version of the fibonacci sequence. It works but on N>47 the function outputs negative numbers,buffer overflow. No difference between my iterative version and the recursive one given in the book (recursion is quite slow...). Build a small test program which throws a Constraint error. So why does the test functions correctly where as the fibonacci program doesn't? I have added an Exception for the buffer overflow to the iterative version to check if this solves the problem and it does. Positive is defined as : subtype Positive is Integer range 1 .. Integer'Last; So if the result gets negative it should raise an constraint error without needing to add a raise myself? Output of fibonacci: Please enter an integer (>= 1) > 50 The fibonacci sequence for N = 50 is: 1 1 2 3 5 8 13 21 34 55 . . . 1134903170 1836311903 -1323752223 <== 512559680 -811192543 <== -298632863 <== [2014-05-14 20:57:08] process terminated successfully (elapsed time: 02.29s) Output of buffer_overflow test: enter an integer: 10 raised CONSTRAINT_ERROR : buffer_overflow.adb:11 range check failed (normal 10*2*10^9 = too big for 32 bit int) [2014-05-14 20:55:07] process exited with status 1 (elapsed time: 01.51s) Thanks Laurent with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Test_Fibonacci is N : Positive; Result : Positive := 1; Buffer_Overflow : exception; function Fibonacci_Iter (N : in Positive) return Positive is -- Returns the Nth Fibonacci number, computed iteratively -- Pre : N is defined and N >= 1 -- Post: returns N! First : Positive := 1; Second : Positive := 1; begin -- Fibonacci Iteration if N = 1 or N = 2 then return Result; else for Count in 3 .. N loop Result := First + Second; First := Second; Second := Result; end loop; if Result'Valid then return Result; else raise Buffer_Overflow; end if; end if; end Fibonacci_Iter; function Fibonacci_Rec (N : in Positive) return Positive is -- Returns the Nth Fibonacci number, computed recursively -- Pre : N is defined and N >= 1 -- Post: returns N! begin -- Fibonacci Recursion if (N = 1) or (N = 2) then return 1; else return Fibonacci_Rec (N - 2) + Fibonacci_Rec (N - 1); end if; end Fibonacci_Rec; begin -- Test Fibonacci Ada.Text_IO.Put (Item => "Please enter an integer (>0) > "); Ada.Integer_Text_IO.Get (Item => N); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => "The fibonacci sequence for N =" & Positive'Image (N) & " is: " ); Ada.Text_IO.New_Line; for Count in 1..N loop --Ada.Integer_Text_IO.Put (Item => Fibonacci_Iter (N => Count), Width => 1); Ada.Integer_Text_IO.Put (Item => Fibonacci_Rec (N => Count), Width => 1); Ada.Text_IO.New_Line; --Ada.Text_IO.Put(Item => ' '); end loop; end Test_Fibonacci; ----------------------------- with Ada.Integer_Text_IO; with Ada.Text_IO; procedure Buffer_Overflow is Result : Positive; N : Positive; begin Ada.Text_IO.Put (Item => "enter an integer: "); Ada.Integer_Text_IO.Get(Item => N); Result := N * 2_000_000_000; Ada.Integer_Text_IO.Put (Item => Result, Width => 1); end Buffer_Overflow;