waltermilner

Untitled

Dec 22nd, 2016
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. public class Test {
  2.  
  3.   final static double EPS = 1E-8; // tolerance
  4.  
  5.   static double erf(double x) {
  6.     double temp = 0;
  7.     double term;
  8.     int sign = 1;
  9.     int n = 0;
  10.     double pow = x;
  11.     int fact = 1;
  12.     do {
  13.       term = sign * pow / ((2 * n + 1) * fact);
  14.       temp += term;
  15.       sign *= -1;
  16.       pow = pow * x * x;
  17.       n++;
  18.       fact *= n;
  19.     } while (Math.abs(term) > EPS);
  20.  
  21.     temp = temp * 2 / Math.sqrt(Math.PI);
  22.     return temp;
  23.   }
  24.  
  25.   public static void main(String argd[]) {
  26.     double x = 0.5; // test - erf(x) is 0.52050
  27.     double y = erf(x);
  28.     System.out.println(y); // 0.5204998778007922
  29.  
  30.   }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment