Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- final static double EPS = 1E-8; // tolerance
- static double erf(double x) {
- double temp = 0;
- double term;
- int sign = 1;
- int n = 0;
- double pow = x;
- int fact = 1;
- do {
- term = sign * pow / ((2 * n + 1) * fact);
- temp += term;
- sign *= -1;
- pow = pow * x * x;
- n++;
- fact *= n;
- } while (Math.abs(term) > EPS);
- temp = temp * 2 / Math.sqrt(Math.PI);
- return temp;
- }
- public static void main(String argd[]) {
- double x = 0.5; // test - erf(x) is 0.52050
- double y = erf(x);
- System.out.println(y); // 0.5204998778007922
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment