#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
namespace c
{
const double pi = 3.14159265358979323846;
const double rad2pi = std::sqrt(2*pi);
const int numIterations =100;
}
double getPercentage(int val, int mean, int stddev)
{
double dif = (val - mean) / (double)(stddev);
return std::exp(-0.5*dif*dif) / (c::rad2pi * stddev);
}
// tition
double getNormalDistributionPhi(double x)
{ //reference: http://e...content-available-to-author-only...a.org/wiki/Normal_distribution#Cumulative_distribution_function
//code copied from pascal from Wikipedia and translated to c++. I have not made any checks for errors.
//Please give the code a second look.
//Original pascal code from wikipedia:
//begin
// sum:=x;
// value:=x;
// for i:=1 to 100 do
// begin
// value:=(value*x*x/(2*i+1));
// sum:=sum+value;
// end;
// result:=0.5+(sum/sqrt(2*pi))*exp(-(x*x)/2);
//end;
double value=x;
double sum=x;
for (int i=1; i<c::numIterations; i++)
{ value*=x*x/(2*i+1);
sum+=sum;
}
return 0.5+(sum/std::sqrt(2*c::pi))*std::exp(-(x*x)/2);
}
double getPercentageNormalDistribution(double val, double mean, double stddev) //all arguments should be floating point, int is no good!
{ return getNormalDistributionPhi((val-mean)/stddev);
}
// JL Borges
double phi( double x, double mu, double sigma )
{ return 0.5 + 0.5 * ( std::erf( (x-mu) / ( std::sqrt(2.0) * sigma ) ) ) ; }
double getPercentageAlt(int val, int mean, int stddev)
{
return phi( val + 0.5, mean, stddev ) - phi( val - 0.5, mean, stddev );
}
int main()
{
int mean = 20;
int stddev = 2;
int drawcount = 40;
cout << "mean : " << mean << "\n";
cout << "stddev : " << stddev << "\n\n";
for(int i = 0; i < drawcount; ++i)
{
cout << setw(2) << i << " -> " << fixed << getPercentage(i,mean,stddev) << '\t' // ne555
<< fixed << getPercentageAlt(i,mean,stddev) << '\t' // JLBorges
<< fixed << getPercentageNormalDistribution(i,mean,stddev) << '\n'; // tition
}
cout.flush();
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxpb21hbmlwPgoKdXNpb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