#include #include #ifdef UNIT_TEST_NORMAL_DISTRIBUTION #include #include #include #endif #include "NormalDistribution.h" NormalDistribution::NormalDistribution(int numberOfPoints) { assert(numberOfPoints >= 7); _max = numberOfPoints - 1; _mean = _max / 2; _stdDev = sqrt(numberOfPoints)/2; _points = new double[numberOfPoints]; // The following statement worked fine where N was 1000. But you couldn't // go much higher before you exceeded the bounds of a double precision // number. If N was 10000 then you'd have all 0's. //_points[0] = pow(0.5, _max); // Instead we start with 1 and divide by 2 each step along the way. _points[0] = 1.0; for (int i = 1; i < numberOfPoints; i++) { _points[i] = 0.0; } for (int row = 1; row < numberOfPoints; row++) { for (int i = row; i; i--) { _points[i] = (_points[i] + _points[i - 1]) / 2.0; } _points[0] /= 2.0; } for (int i = 1; i < numberOfPoints; i++) { _points[i] += _points[i - 1]; } #ifdef UNIT_TEST_NORMAL_DISTRIBUTION for (int i = 0; i < numberOfPoints; i++) { //std::cout< _max) { _location = _max; } int lower = (int)floor(_location); double higherPart = _location - lower; if ((lower == _max) || (higherPart == 0)) { return _points[lower]; } return _points[lower] * (1 - higherPart) + _points[lower + 1] * higherPart; } #ifdef UNIT_TEST_NORMAL_DISTRIBUTION NormalDistribution d(10000); void showProbability(double from, double to) { std::cout<<"probability("< limits; typedef std::numeric_limits llimits; int main(int argc, char *argv[]) { std::cout<<"probability(0)="< smallish { -1e+99, limits::lowest(), -limits::infinity(), limits::signaling_NaN(), -limits::signaling_NaN() }; for (double small : smallish) showProbability(small, 0); for (double small : smallish) showProbability(small, -4); std::cout<<"probability(-4,-3)="< bigish { 1e+99, limits::max(), limits::infinity(), limits::signaling_NaN(), -limits::signaling_NaN() }; for (double big : bigish) showProbability(4, big); for (double big : bigish) showProbability(0, big); std::cout<<"probability(0,0)="<\n" <<"limits::max()="<\n" <<"llimits::max()="<