## digitalmars.D.learn - pi program

Charanjit Singh <er_charan hotmail.com> writes:
```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
```
Sep 24 2015
Nicholas Wilson <iamthewilsonator hotmail.com> writes:
```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

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);
```
Sep 24 2015
Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
```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=20
wrote:
import std.stdio;
import std.math;
void main()
=20
{
float sum,pi,t;
int n=3D1;
sum=3D0;
while (n<100 )
{
t=3D1/( n*n);
n=3Dn+1;
sum=3Dsum+t;
=20
=20
}
writeln("value of PI=3D  " , (sum*6)^^.5);
that is pi program as
(pi^2)/6=3D   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=E2=80=A6

t=3D1/( n*n); //<----
Is doing integer division so is zero for n > 1 hence sqrt(1*6) =3D=20
2.449...
=20
change that to a
t=3D1.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 =3D 1;
auto sum =3D 0.0;
while (n < 100) {
sum +=3D 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 =3D 0.0;
foreach (n; 1..100) {
sum +=3D 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 =3D  " , take_root(while_loop()));
writeln("foreach =3D  " , take_root(foreach_loop()));
writeln("reduce =3D  " , take_root(reduce_loop()));
}

results in:

while =3D  3.13198
foreach =3D  3.13198
reduce =3D  2.44949

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

double reduce_function_loop() {
return reduce!((n) =3D> 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 fr=
om argument types !()(int, int), candidates are:
pi_sumInverseSquares.d(28):        pi_sumInverseSquares.reduce_function_loo=
p.__lambda1
/usr/include/dmd/phobos/std/meta.d(546): Error: template instance pi_sumInv=
erseSquares.reduce_function_loop.F!(__lambda1) error instantiating
/usr/include/dmd/phobos/std/algorithm/iteration.d(2473):        instantiate=
d 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) =3D> 1.00000 / (n * n))(Re=
sult), candidates are:
/usr/include/dmd/phobos/std/algorithm/iteration.d(2447):        std.algorit=
hm.iteration.reduce(fun...) if (fun.length >=3D 1)
Failed: ["dmd", "-v", "-o-", "pi_sumInverseSquares.d", "-I."]

which is sort of annoying.

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
```
Sep 25 2015
mzfhhhh <mzfhhhh foxmail.com> writes:
``` 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));
}

it should be write :
double reduce_loop() {
//return iota(1, 100).map!"1.0 / (a * a)".sum;
return iota(1, 100).reduce!"a + 1.0 / (b * b)";
}
```
Sep 25 2015
Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
```On Fri, 2015-09-25 at 09:14 +0000, mzfhhhh via Digitalmars-d-learn
wrote:
=20
double reduce_string_loop() {
return reduce!"1.0 / (a * a)"(iota(1, 100));
}
=20
double reduce_function_loop() {
return reduce!((n) =3D> 1.0 / (n * n))(iota(1, 100));
}
=20

=20
it should be write :
double reduce_loop() {
//return iota(1, 100).map!"1.0 / (a * a)".sum;
return iota(1, 100).reduce!"a + 1.0 / (b * b)";
}

Aha, bingo, spot on. Thanks. Amended now to:

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

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

which both work as they should. I am sure I will be able to find a
reason why I missed that reduce takes a function of two parameters not
one.

Interesting question on style is whether to use function application or
method call:

reduce!"a + 1.0 / (b * b)"(iota(1, 100))

vs.

iota(1, 100).reduce!"a + 1.0 / (b * b)"

The debate may well turn into a bikeshed one, but it would be good to
know what the opinions are.=20

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
```
Sep 25 2015
John Colvin <john.loughran.colvin gmail.com> writes:
```On Friday, 25 September 2015 at 12:51:17 UTC, Russel Winder wrote:
On Fri, 2015-09-25 at 09:14 +0000, mzfhhhh via
Digitalmars-d-learn wrote:
[...]

Aha, bingo, spot on. Thanks. Amended now to:

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

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

which both work as they should. I am sure I will be able to
find a reason why I missed that reduce takes a function of two
parameters not one.

Interesting question on style is whether to use function
application or method call:

reduce!"a + 1.0 / (b * b)"(iota(1, 100))

vs.

iota(1, 100).reduce!"a + 1.0 / (b * b)"

The debate may well turn into a bikeshed one, but it would be
good to know what the opinions are.

I vastly prefer the UFCS version, but unfortunately reduce has
its arguments the wrong way around for that if you use the
version that takes a seed...
```
Sep 25 2015
Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
```On Fri, 2015-09-25 at 12:54 +0000, John Colvin via Digitalmars-d-learn
wrote:
=20

[=E2=80=A6]
I vastly prefer the UFCS version, but unfortunately reduce has=20
its arguments the wrong way around for that if you use the=20
version that takes a seed...

In which case the reduce parameter list is wrong, this is a bug and
should be fixed. Is there a bug report for this I can connect with?

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
```
Sep 25 2015
John Colvin <john.loughran.colvin gmail.com> writes:
```On Saturday, 26 September 2015 at 06:28:22 UTC, Russel Winder
wrote:
On Fri, 2015-09-25 at 12:54 +0000, John Colvin via
Digitalmars-d-learn wrote:

[…]
I vastly prefer the UFCS version, but unfortunately reduce has
its arguments the wrong way around for that if you use the
version that takes a seed...

In which case the reduce parameter list is wrong, this is a bug
and should be fixed. Is there a bug report for this I can
connect with?

It's been argued about a lot.

https://issues.dlang.org/show_bug.cgi?id=8755
https://github.com/D-Programming-Language/phobos/pull/861
https://github.com/D-Programming-Language/phobos/pull/1955
https://github.com/D-Programming-Language/phobos/pull/2033
```
Sep 26 2015
Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
```On Sat, 2015-09-26 at 10:46 +0000, John Colvin via Digitalmars-d-learn
wrote:
[=E2=80=A6]

I guess the summary is: it's a breaking change, so do it. No we can't
do that it's a breaking change.=20

Seems lame given all the other breaking changes that have been. Sad
given that reduce is probably the single most important operation in
parallel programming.

It's been argued about a lot.
=20
https://issues.dlang.org/show_bug.cgi?id=3D8755
https://github.com/D-Programming-Language/phobos/pull/861
https://github.com/D-Programming-Language/phobos/pull/1955
https://github.com/D-Programming-Language/phobos/pull/2033

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
```
Sep 28 2015
John Colvin <john.loughran.colvin gmail.com> writes:
```On Monday, 28 September 2015 at 11:04:56 UTC, Russel Winder wrote:
On Sat, 2015-09-26 at 10:46 +0000, John Colvin via
Digitalmars-d-learn wrote:
[…]

I guess the summary is: it's a breaking change, so do it. No we
can't do that it's a breaking change.

Seems lame given all the other breaking changes that have been.
Sad given that reduce is probably the single most important
operation in parallel programming.

My thoughts exactly, even though it was partly me that pointed
out the breaking changes...

I still think we should add fold[lr]? just to fix the ordering.
```
Sep 28 2015
Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
```On Mon, 2015-09-28 at 11:37 +0000, John Colvin via Digitalmars-d-learn
wrote:
=20

[=E2=80=A6]
My thoughts exactly, even though it was partly me that pointed=20
out the breaking changes...

Curses, if no-one had pointed out it was breaking maybe no-one would
have noticed, and just made the change?

I still think we should add fold[lr]? just to fix the ordering.

Whatever happens in std.algorithm must also happen in std.parallelism.
reduce in std.parallelism was constructed to be in harmony with
std.algorithm and not UFCS.

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
```
Sep 28 2015
Meta <jared771 gmail.com> writes:
```On Friday, 25 September 2015 at 12:51:17 UTC, Russel Winder wrote:
Aha, bingo, spot on. Thanks. Amended now to:

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

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

which both work as they should. I am sure I will be able to
find a reason why I missed that reduce takes a function of two
parameters not one.

Interesting question on style is whether to use function
application or method call:

reduce!"a + 1.0 / (b * b)"(iota(1, 100))

vs.

iota(1, 100).reduce!"a + 1.0 / (b * b)"

The debate may well turn into a bikeshed one, but it would be
good to know what the opinions are.

The main difference is that "method call" style is more amenable
to chaining (and IMO, it looks cleaner as you don't have nesting
parentheses.
```
Sep 25 2015
Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
```On Fri, 2015-09-25 at 18:45 +0000, Meta via Digitalmars-d-learn wrote:
[=E2=80=A6]
=20
The main difference is that "method call" style is more amenable=20
to chaining (and IMO, it looks cleaner as you don't have nesting=20
parentheses.

I guess coming from Clojure I was less worried about Lisp-style code.

I'll try to be more Python/Java/Scala-ish ;-)

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder ekiga.n=
et
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
```
Sep 28 2015