comp.lang.ada
 help / color / mirror / Atom feed
From: "Nasser M. Abbasi" <nma@12000.org>
Subject: a bug in code shown in ada2012 rational or is it me?
Date: Wed, 20 Jun 2012 05:09:00 -0500
Date: 2012-06-20T05:09:00-05:00	[thread overview]
Message-ID: <jrs7ft$uem$1@speranza.aioe.org> (raw)

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



             reply	other threads:[~2012-06-20 10:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-20 10:09 Nasser M. Abbasi [this message]
2012-06-20 10:33 ` a bug in code shown in ada2012 rational or is it me? Egil Høvik
2012-06-20 10:44 ` Egil Høvik
2012-06-20 14:03   ` Nasser M. Abbasi
2012-06-21 20:10     ` Randy Brukardt
replies disabled

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