comp.lang.ada
 help / color / mirror / Atom feed
* a bug in code shown in ada2012 rational or is it me?
@ 2012-06-20 10:09 Nasser M. Abbasi
  2012-06-20 10:33 ` Egil Høvik
  2012-06-20 10:44 ` Egil Høvik
  0 siblings, 2 replies; 5+ messages in thread
From: Nasser M. Abbasi @ 2012-06-20 10:09 UTC (permalink / 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



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

* Re: a bug in code shown in ada2012 rational or is it me?
  2012-06-20 10:09 a bug in code shown in ada2012 rational or is it me? Nasser M. Abbasi
@ 2012-06-20 10:33 ` Egil Høvik
  2012-06-20 10:44 ` Egil Høvik
  1 sibling, 0 replies; 5+ messages in thread
From: Egil Høvik @ 2012-06-20 10:33 UTC (permalink / raw)
  Cc: nma

>   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);


Should be: 
       
   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);



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

* Re: a bug in code shown in ada2012 rational or is it me?
  2012-06-20 10:09 a bug in code shown in ada2012 rational or is it me? Nasser M. Abbasi
  2012-06-20 10:33 ` Egil Høvik
@ 2012-06-20 10:44 ` Egil Høvik
  2012-06-20 14:03   ` Nasser M. Abbasi
  1 sibling, 1 reply; 5+ messages in thread
From: Egil Høvik @ 2012-06-20 10:44 UTC (permalink / raw)
  Cc: nma

On Wednesday, June 20, 2012 12:09:00 PM UTC+2, Nasser M. Abbasi wrote:
>   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);

Should be:

   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);



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

* Re: a bug in code shown in ada2012 rational or is it me?
  2012-06-20 10:44 ` Egil Høvik
@ 2012-06-20 14:03   ` Nasser M. Abbasi
  2012-06-21 20:10     ` Randy Brukardt
  0 siblings, 1 reply; 5+ messages in thread
From: Nasser M. Abbasi @ 2012-06-20 14:03 UTC (permalink / raw)


On 6/20/2012 5:44 AM, Egil H�vik wrote:
> On Wednesday, June 20, 2012 12:09:00 PM UTC+2, Nasser M. Abbasi wrote:
>>    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);
>
> Should be:
>
>     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);
>

thank you. I verified that I get Pi now with the above change.

May be someone at Adacore can update the pdf file on that web site,
as it is not good to have this typo in the code given it is an
Ada 2012 document.

--Nasser




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

* Re: a bug in code shown in ada2012 rational or is it me?
  2012-06-20 14:03   ` Nasser M. Abbasi
@ 2012-06-21 20:10     ` Randy Brukardt
  0 siblings, 0 replies; 5+ messages in thread
From: Randy Brukardt @ 2012-06-21 20:10 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1104 bytes --]

I forwarded this thread to John Barnes, so it ought to get fixed when he 
gets around to revising the existing part of the Rationale. FYI, I sent him 
a list of 51 bugs I found when I created (the not yet finished) HTML 
version, so it's not quite a foolproof document at this time. :-)

                                    Randy.

"Nasser M. Abbasi" <nma@12000.org> wrote in message 
news:jrsl7g$33m$1@speranza.aioe.org...
> On 6/20/2012 5:44 AM, Egil H�vik wrote:
>> On Wednesday, June 20, 2012 12:09:00 PM UTC+2, Nasser M. Abbasi wrote:
>>>    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);
>>
>> Should be:
>>
>>     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);
>>
>
> thank you. I verified that I get Pi now with the above change.
>
> May be someone at Adacore can update the pdf file on that web site,
> as it is not good to have this typo in the code given it is an
> Ada 2012 document.
>
> --Nasser
> 





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

end of thread, other threads:[~2012-06-21 20:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-20 10:09 a bug in code shown in ada2012 rational or is it me? Nasser M. Abbasi
2012-06-20 10:33 ` 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

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