pi program

Russel Winder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 25 01:52:29 PDT 2015


On Fri, 2015-09-25 at 06:02 +0000, Nicholas Wilson via Digitalmars-d
-learn wrote:
> On Friday, 25 September 2015 at 05:50:58 UTC, Charanjit Singh 
> wrote:
> > import std.stdio;
> > import std.math;
> > void main()
> > 
> > {
> >     float sum,pi,t;
> >      int n=1;
> >     sum=0;
> >     while (n<100 )
> >     {
> >     t=1/( n*n);
> >     n=n+1;
> >     sum=sum+t;
> > 
> > 
> >    }
> >     writeln("value of PI=  " , (sum*6)^^.5);
> >  that is pi program as
> > (pi^2)/6=   1/1+  1/4  +  1/9  +  1/16  +  1/25
> > but output of my program is 2.44

This looks a bit like K&R C transliterated to D. Much better to write
more idiomatic D…

> t=1/( n*n); //<----
> Is doing integer division so is zero for n > 1 hence sqrt(1*6) = 
> 2.449...
> 
> change that to a
> t=1.0/( n*n);

That certainly solves the immediate problem.

Taking the original code and making it more D-like, you get something
like:

    double while_loop() {
      auto n = 1;
      auto sum = 0.0;
      while (n < 100) {
        sum += 1.0 / (n * n);
        n++;
      }
      return sum;
    }

but this is bounded iteration not unbounded iteration, so let us use a
far more idiomatic form:

    double foreach_loop() {
      auto sum = 0.0;
      foreach (n; 1..100) {
        sum += 1.0 / (n * n);
      }
      return sum;
    }

but this is still very explicit iteration and we have moved on to the
era of implicit iteration and declarative rather than imperative
expression:

    double reduce_loop() {
      return reduce!"1.0 / (a * a)"(iota(1, 100));
    }

Unfortunately I have clearly done something silly here as:

    double take_root(double x) { return (x * 6)^^0.5; }

    void main() {
      writeln("while =  " , take_root(while_loop()));
      writeln("foreach =  " , take_root(foreach_loop()));
      writeln("reduce =  " , take_root(reduce_loop()));
    }

results in:

    while =  3.13198
    foreach =  3.13198
    reduce =  2.44949

If we worry about the string form and instead try:

double reduce_string_loop() {
  return reduce!"1.0 / (a * a)"(iota(1, 100));
}

double reduce_function_loop() {
  return reduce!((n) => 1.0 / (n * n))(iota(1, 100));
}

then we end up with:


/usr/include/dmd/phobos/std/algorithm/iteration.d(2565): Error: template pi_sumInverseSquares.reduce_function_loop.__lambda1 cannot deduce function from argument types !()(int, int), candidates are:
pi_sumInverseSquares.d(28):        pi_sumInverseSquares.reduce_function_loop.__lambda1
/usr/include/dmd/phobos/std/meta.d(546): Error: template instance pi_sumInverseSquares.reduce_function_loop.F!(__lambda1) error instantiating
/usr/include/dmd/phobos/std/algorithm/iteration.d(2473):        instantiated from here: staticMap!(ReduceSeedType, __lambda1)
pi_sumInverseSquares.d(28):        instantiated from here: reduce!(Result)
pi_sumInverseSquares.d(28): Error: template std.algorithm.iteration.reduce cannot deduce function from argument types !((n) => 1.00000 / (n * n))(Result), candidates are:
/usr/include/dmd/phobos/std/algorithm/iteration.d(2447):        std.algorithm.iteration.reduce(fun...) if (fun.length >= 1)
Failed: ["dmd", "-v", "-o-", "pi_sumInverseSquares.d", "-I."]

which is sort of annoying.

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150925/6c2fc403/attachment-0001.sig>


More information about the Digitalmars-d-learn mailing list