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=2.0 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,23889412ffb2ab1,start X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,CP1252 Received: by 10.68.190.104 with SMTP id gp8mr22176898pbc.4.1340186945297; Wed, 20 Jun 2012 03:09:05 -0700 (PDT) Path: l9ni71976pbj.0!nntp.google.com!news1.google.com!goblin2!goblin.stu.neva.ru!aioe.org!.POSTED!not-for-mail From: "Nasser M. Abbasi" Newsgroups: comp.lang.ada Subject: a bug in code shown in ada2012 rational or is it me? Date: Wed, 20 Jun 2012 05:09:00 -0500 Organization: Aioe.org NNTP Server Message-ID: Reply-To: nma@12000.org NNTP-Posting-Host: KdJUrTuvv3Zv/s8pPxNluw.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 X-Notice: Filtered by postfilter v. 0.8.2 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Date: 2012-06-20T05:09:00-05:00 List-Id: looking at http://www.adacore.com/uploads/technical-papers/Ada2012_Rationale_Chp2_expressions.pdf page 14. I translated that Ada function to Matlab and in Mathematica and I am not getting the same value for Pi it shows. Not even close. I copied the Ada code from the PDF (hopefully all went ok) here it is -------------------------------------------- with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Long_Float_Text_IO; use Ada.Long_Long_Float_Text_IO; with Ada.Numerics.Long_Long_Elementary_Functions; use Ada.Numerics.Long_Long_Elementary_Functions; procedure Compute_Pi is function B(N: Natural) return Long_Long_Float; function A(N: Natural) return Long_Long_Float is (if N = 0 then 1.0 else (A(N�1)+B(N�1))/2.0); function B(N: Natural) return Long_Long_Float is (if N = 0 then Sqrt(0.5) else Sqrt(A(N�1)*B(N�1))); function T(N: Natural) return Long_Long_Float is (if N = 0 then 0.25 else (T(N�1)�2.0**(N�1)*A(N�1)�A(N))**2); K: constant := 5; -- for example Pi: constant Long_Long_Float := ((A(K) + B(K))**2 / (4.0*T(K)); begin Put(Pi, Exp => 0); New_Line; end Compute_Pi; ----------------------------- Here is the Matlab code translation. I tried to keep all names of variables and functions the same ---- Matlab --- function foo format long k=5; (A(k)+B(k))^2/(4*T(k)) end function r=A(n) if n==0 r=1; else r=(A(n-1)+B(n-1))/2; end end function r=B(n) if n==0 r=sqrt(0.5); else r=sqrt(A(n-1)*B(n-1)); end end function r=T(n) if n==0 r=0.25; else r=(T(n-1)-2^(n-1)*A(n-1)-A(n))^2; end end ------------------------------- And here is the Mathematica code ----------------------- b[n_]:=If[n==0,Sqrt[0.5],Sqrt[a[n-1]*b[n-1]]] a[n_]:=If[n==0,1,(a[n-1]+b[n-1])/2] t[n_]:=If[n==0,0.25,(t[n-1]-2^(n-1)*a[n-1]-a[n])^2] k=5.; (a[k]+b[k])^2/(4 t[k]) -------------------------------- Here is what I get Out[9]= 0.0000847226845502729 Same with Matlab: EDU>> foo ans = 8.472268455027291e-05 I went over it many times, and I do not see what I copied wrong. As far as I see, the translation maps to what is shown in Ada. I must have done something wrong in the translation? do you see it? may be someone who has Ada 2012 can run this function and see of it generates the value shown in the paper which is 3.14159265358979324 thanks, --Nasser