fork 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+=value;
  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. double getPercentageAlt2(int val, int mean, int stddev)
  50. {
  51. return getPercentageNormalDistribution( ((double) val) + 0.5, mean, stddev ) -
  52. getPercentageNormalDistribution( ((double) val) - 0.5, mean, stddev );
  53. }
  54.  
  55.  
  56. // JL Borges
  57. double phi( double x, double mu, double sigma )
  58. { return 0.5 + 0.5 * ( std::erf( (x-mu) / ( std::sqrt(2.0) * sigma ) ) ) ; }
  59.  
  60.  
  61. double getPercentageAlt(int val, int mean, int stddev)
  62. {
  63. return phi( ((double) val) + 0.5, mean, stddev ) - phi( ((double) val) - 0.5, mean, stddev );
  64. }
  65.  
  66.  
  67. int main()
  68. {
  69. int mean = 20;
  70. int stddev = 2;
  71. int drawcount = 40;
  72.  
  73. cout << "mean : " << mean << "\n";
  74. cout << "stddev : " << stddev << "\n\n";
  75.  
  76. for(int i = 0; i < drawcount; ++i)
  77. {
  78. cout << setw(2) << i << " -> " << fixed << getPercentage(i,mean,stddev) << '\t' // ne555
  79. << fixed << getPercentageAlt(i,mean,stddev) << '\t' // JLBorges
  80. << fixed << getPercentageAlt2(i,mean,stddev) << '\n'; // tition
  81. }
  82.  
  83. cout.flush();
  84. }
  85.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
mean    : 20
stddev  : 2

 0 -> 0.000000	0.000000	-0.000000
 1 -> 0.000000	0.000000	-0.000000
 2 -> 0.000000	0.000000	-0.000000
 3 -> 0.000000	0.000000	0.000000
 4 -> 0.000000	0.000000	0.000000
 5 -> 0.000000	0.000000	0.000000
 6 -> 0.000000	0.000000	0.000000
 7 -> 0.000000	0.000000	0.000000
 8 -> 0.000000	0.000000	0.000000
 9 -> 0.000000	0.000000	0.000000
10 -> 0.000001	0.000001	0.000001
11 -> 0.000008	0.000010	0.000010
12 -> 0.000067	0.000078	0.000078
13 -> 0.000436	0.000489	0.000489
14 -> 0.002216	0.002403	0.002403
15 -> 0.008764	0.009245	0.009245
16 -> 0.026995	0.027835	0.027835
17 -> 0.064759	0.065591	0.065591
18 -> 0.120985	0.120978	0.120978
19 -> 0.176033	0.174666	0.174666
20 -> 0.199471	0.197413	0.197413
21 -> 0.176033	0.174666	0.174666
22 -> 0.120985	0.120978	0.120978
23 -> 0.064759	0.065591	0.065591
24 -> 0.026995	0.027835	0.027835
25 -> 0.008764	0.009245	0.009245
26 -> 0.002216	0.002403	0.002403
27 -> 0.000436	0.000489	0.000489
28 -> 0.000067	0.000078	0.000078
29 -> 0.000008	0.000010	0.000010
30 -> 0.000001	0.000001	0.000001
31 -> 0.000000	0.000000	0.000000
32 -> 0.000000	0.000000	0.000000
33 -> 0.000000	0.000000	0.000000
34 -> 0.000000	0.000000	0.000000
35 -> 0.000000	0.000000	0.000000
36 -> 0.000000	0.000000	0.000000
37 -> 0.000000	0.000000	0.000000
38 -> 0.000000	0.000000	-0.000000
39 -> 0.000000	0.000000	-0.000000