fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. namespace c
  8. {
  9. const double pi = 3.14159265358979323846;
  10. const double rad2pi = std::sqrt(2*pi);
  11. const int numIterations =100;
  12. }
  13.  
  14. double getPercentage(int val, int mean, int stddev)
  15. {
  16. double dif = (val - mean) / (double)(stddev);
  17. return std::exp(-0.5*dif*dif) / (c::rad2pi * stddev);
  18. }
  19.  
  20. // tition
  21. double getNormalDistributionPhi(double x)
  22. { //reference: http://e...content-available-to-author-only...a.org/wiki/Normal_distribution#Cumulative_distribution_function
  23. //code copied from pascal from Wikipedia and translated to c++. I have not made any checks for errors.
  24. //Please give the code a second look.
  25. //Original pascal code from wikipedia:
  26. //begin
  27. // sum:=x;
  28. // value:=x;
  29. // for i:=1 to 100 do
  30. // begin
  31. // value:=(value*x*x/(2*i+1));
  32. // sum:=sum+value;
  33. // end;
  34. // result:=0.5+(sum/sqrt(2*pi))*exp(-(x*x)/2);
  35. //end;
  36. double value=x;
  37. double sum=x;
  38. for (int i=1; i<c::numIterations; i++)
  39. { value*=x*x/(2*i+1);
  40. sum+=sum;
  41. }
  42. return 0.5+(sum/std::sqrt(2*c::pi))*std::exp(-(x*x)/2);
  43. }
  44.  
  45. double getPercentageNormalDistribution(double val, double mean, double stddev) //all arguments should be floating point, int is no good!
  46. { return getNormalDistributionPhi((val-mean)/stddev);
  47. }
  48.  
  49. // JL Borges
  50. double phi( double x, double mu, double sigma )
  51. { return 0.5 + 0.5 * ( std::erf( (x-mu) / ( std::sqrt(2.0) * sigma ) ) ) ; }
  52.  
  53.  
  54. double getPercentageAlt(int val, int mean, int stddev)
  55. {
  56. return phi( val + 0.5, mean, stddev ) - phi( val - 0.5, mean, stddev );
  57. }
  58.  
  59.  
  60. int main()
  61. {
  62. int mean = 20;
  63. int stddev = 2;
  64. int drawcount = 40;
  65.  
  66. cout << "mean : " << mean << "\n";
  67. cout << "stddev : " << stddev << "\n\n";
  68.  
  69. for(int i = 0; i < drawcount; ++i)
  70. {
  71. cout << setw(2) << i << " -> " << fixed << getPercentage(i,mean,stddev) << '\t' // ne555
  72. << fixed << getPercentageAlt(i,mean,stddev) << '\t' // JLBorges
  73. << fixed << getPercentageNormalDistribution(i,mean,stddev) << '\n'; // tition
  74. }
  75.  
  76. cout.flush();
  77. }
  78.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
mean    : 20
stddev  : 2

 0 -> 0.000000	0.000000	-487703127.882985
 1 -> 0.000000	0.000000	-60682679044.817200
 2 -> 0.000000	0.000000	-5864022512250.875977
 3 -> 0.000000	0.000000	-439957173753869.562500
 4 -> 0.000000	0.000000	-25618057886245076.000000
 5 -> 0.000000	0.000000	-1157200456815798016.000000
 6 -> 0.000000	0.000000	-40528718330031521792.000000
 7 -> 0.000000	0.000000	-1099821139640395104256.000000
 8 -> 0.000000	0.000000	-23106289624484681875456.000000
 9 -> 0.000000	0.000000	-375438051511709634396160.000000
10 -> 0.000001	0.000001	-4711602213059886663598080.000000
11 -> 0.000008	0.000010	-45589047768138037799682048.000000
12 -> 0.000067	0.000078	-339299932039072775097286656.000000
13 -> 0.000436	0.000489	-1935949298821206787624009728.000000
14 -> 0.002216	0.002403	-8427052949270610223273345024.000000
15 -> 0.008764	0.009245	-27774700802066163385422053376.000000
16 -> 0.026995	0.027835	-68441681107345081740895977472.000000
17 -> 0.064759	0.065591	-123137293414488602698865180672.000000
18 -> 0.120985	0.120978	-153367167087175816090090471424.000000
19 -> 0.176033	0.174666	-111573955698078002353390223360.000000
20 -> 0.199471	0.197413	0.500000
21 -> 0.176033	0.174666	111573955698078002353390223360.000000
22 -> 0.120985	0.120978	153367167087175816090090471424.000000
23 -> 0.064759	0.065591	123137293414488602698865180672.000000
24 -> 0.026995	0.027835	68441681107345081740895977472.000000
25 -> 0.008764	0.009245	27774700802066163385422053376.000000
26 -> 0.002216	0.002403	8427052949270610223273345024.000000
27 -> 0.000436	0.000489	1935949298821206787624009728.000000
28 -> 0.000067	0.000078	339299932039072775097286656.000000
29 -> 0.000008	0.000010	45589047768138037799682048.000000
30 -> 0.000001	0.000001	4711602213059886663598080.000000
31 -> 0.000000	0.000000	375438051511709634396160.000000
32 -> 0.000000	0.000000	23106289624484681875456.000000
33 -> 0.000000	0.000000	1099821139640395104256.000000
34 -> 0.000000	0.000000	40528718330031521792.000000
35 -> 0.000000	0.000000	1157200456815798016.000000
36 -> 0.000000	0.000000	25618057886245076.000000
37 -> 0.000000	0.000000	439957173753870.562500
38 -> 0.000000	0.000000	5864022512251.875977
39 -> 0.000000	0.000000	60682679045.817200