
// Copyright 2019 Shlomi Steinberg
// 

#include <stdexcept>
#include "glm.hpp"

/*
 *	Computes approximation of the error function with 1.5*10^(-7) error bound.
 *	From C. Hastings Jr., Approximations for digital computers. Princeton Univ. Press, Princeton, N.J., 1955
 *	http://people.math.sfu.ca/~cbm/aands/page_299.htm
 */
template <typename T>
auto erf(const T x) {
	const auto p  = T( .3275911);
	const T a[5] = {
		T( .254829592),
		T(-.284496739),
		T( 1.42141374),
		T(-1.453152027),
		T( 1.061405429),
	};

	const auto t = T(1) / (T(1) + p*glm::abs(x));
	const auto t2 = t*t;
	const auto t3 = t2*t;
	const auto t4 = t2*t2;
	const auto t5 = t3*t2;

	return glm::sign(x) * (T(1) - (a[0]*t + a[1]*t2 + a[2]*t3 + a[3]*t4 + a[4]*t5) * glm::exp(-x*x));
}
