资源简介
ERF函数源代码,java语言写的,绝对正确
代码片段和文件信息
/*************************************************************************
* Compilation: javac MyMath.java
* Execution: java MyMath z
*
* Implements a number of mathematical functions: absolute value
* square root exponential error function and the cumulative
* Gaussian distribution. Also produces random Gaussians.
* The first three are for illustration only since they are
* already in the standard Math library.
*
* The test client reads in a command line inputs z and prints out
* erf(z) and Phi(z) to 7 significant digits where
*
* erf(z) = 2 / sqrt(pi) * integral(exp(-t*t) t = 0..z)
* Phi(z) = normal cdf
*
*
* % java MyMath 1.0
* abs(1.0) = 1.0
* exp(1.0) = 2.7182818284590455
* cosh(1.0) = 1.543080634815244
* sqrt(1.0) = 1.0
* erf(1.0) = 0.8427007877600067 // actual = 0.84270079294971486934
* Phi(1.0) = 0.8413447386043253 // actual = 0.8413447460
*
* % java MyMath -1.0
* abs(-1.0) = 1.0
* exp(-1.0) = 0.36787944117144245
* cosh(-1.0) = 1.543080634815244
* sqrt(-1.0) = NaN
* erf(-1.0) = -0.8427007877600068
* Phi(-1.0) = 0.15865526139567465
*
* % java MyMath 3.0
* abs(3.0) = 3.0
* exp(3.0) = 20.08553692318766
* cosh(3.0) = 10.067661995777765
* sqrt(3.0) = 1.7320508075688772
* erf(3.0) = 0.9999779095015785 // actual = 0.99997790950300141456
* Phi(3.0) = 0.9986501019267444
*
* % java MyMath 30
* abs(30.0) = 30.0
* exp(30.0) = 1.0686474581524467E13
* cosh(30.0) = 5.343237290762231E12
* sqrt(30.0) = 5.477225575051661
* erf(30.0) = 1.0
* Phi(30.0) = 1.0
*
* % java MyMath -30
* abs(-30.0) = 30.0
* exp(-30.0) = 9.357622968840171E-14
* cosh(-30.0) = 5.343237290762231E12
* sqrt(-30.0) = NaN
* erf(-30.0) = -1.0
* Phi(-30.0) = 0.0
*
* % java MyMath 1E-20
* abs(1.0E-20) = 1.0E-20
* exp(1.0E-20) = 1.0
* cosh(1.0E-20) = 1.0
* sqrt(1.0E-20) = 9.999999999999999E-11
* erf(1.0E-20) = -3.0000000483809686E-8 // true anser 1.13E-20
* Phi(1.0E-20) = 0.49999998499999976
*
* Reference: Chebyshev fitting formula for erf(z) from
* Numerical Recipes 6.2
*
*************************************************************************/
package com;
public class MyMath {
// absolute value
public static double abs(double x) {
if (x == 0.0) return x; // for -0 and +0
else if (x > 0.0) return x;
else return -x;
}
// exponentiation - special case for negative input improves accuracy
public static double exp(double x) {
double term = 1.0;
double sum = 1.0;
for (int N = 1; sum != sum + term; N++) {
term = term * Math.abs(x) / N;
sum = sum + term;
}
if (x >= 0) return sum;
else return 1.0 / sum;
}
// calculate square root using Newton‘s m
- 上一篇:文件输出与读入
- 下一篇:eos4j-1.0.2.jar
评论
共有 条评论