Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 25, 2015 pi program | ||||
---|---|---|---|---|
| ||||
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 |
September 25, 2015 Re: pi program | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charanjit Singh | 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);
|
September 25, 2015 Re: pi program | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson Attachments:
| 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@ekiga.net 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 |
September 25, 2015 Re: pi program | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder |
> 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)";
}
|
September 25, 2015 Re: pi program | ||||
---|---|---|---|---|
| ||||
Posted in reply to mzfhhhh Attachments:
| On Fri, 2015-09-25 at 09:14 +0000, mzfhhhh via Digitalmars-d-learn wrote: > > 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)"; > } 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. -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 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 |
September 25, 2015 Re: pi program | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | 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...
|
September 25, 2015 Re: pi program | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | 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.
|
September 26, 2015 Re: Reduce parameters [was pi program] | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin Attachments:
| 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? -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 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 |
September 26, 2015 Re: Reduce parameters [was pi program] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | 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 |
September 28, 2015 Re: Reduce parameters [was pi program] | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin Attachments:
| 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. > 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 -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 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 |
Copyright © 1999-2021 by the D Language Foundation