public static int castToInt(String s) { if (s == null) return 0; if (s.equals("INF") || s.equals("-INF") || s.equals("NaN")) throw new ArithmeticException("'" + s + "' is too large for int."); s = s.trim(); java.lang.StringBuffer buf = new java.lang.StringBuffer(); switch (prepareNumber(buf, s)) { case 1: return Integer.parseInt(buf.toString()); case 2: return new BigDecimal(buf.toString()).intValue(); case 3: return (int)Double.parseDouble(buf.toString()); default: throw new NumberFormatException("'" + s + "' cannot be converted to int."); } }
public static long castToLong(String s) { if (s == null) return 0; if (s.equals("INF") || s.equals("-INF") || s.equals("NaN")) throw new ArithmeticException("'" + s + "' is too large for long."); s = s.trim(); java.lang.StringBuffer buf = new java.lang.StringBuffer(); switch (prepareNumber(buf, s)) { case 1: return Long.parseLong(buf.toString()); case 2: return new BigDecimal(buf.toString()).longValue(); case 3: return (long)Double.parseDouble(buf.toString()); default: throw new NumberFormatException("'" + s + "' cannot be converted to long."); } }
public static BigInteger castToBigInteger(String s) { if (s == null) return null; if (s.equals("INF") || s.equals("-INF") || s.equals("NaN")) throw new ArithmeticException("'" + s + "' is too large for integer."); s = s.trim(); java.lang.StringBuffer buf = new java.lang.StringBuffer(); switch (prepareNumber(buf, s)) { case 1: return new BigInteger(buf.toString()); case 2: case 3: return new BigDecimal(buf.toString()).toBigInteger(); default: throw new NumberFormatException("'" + s + "' cannot be converted to integer."); } }
public static BigDecimal castToBigDecimal(String s) { if (s == null) return null; if (s.equals("INF") || s.equals("-INF") || s.equals("NaN")) throw new ArithmeticException("'" + s + "' is too large for decimal."); s = s.trim(); java.lang.StringBuffer buf = new java.lang.StringBuffer(); switch (prepareNumber(buf, s)) { case 1: case 2: case 3: return new BigDecimal(buf.toString()); default: throw new NumberFormatException("'" + s + "' cannot be converted to decimal."); } }
/** * Yn double. * * @param n integer order * @param x a double value * @return the Bessel function of the second kind, of order n of the argument. * @throws ArithmeticException the arithmetic exception */ static public double yn(int n, double x) throws ArithmeticException { double by,bym,byp,tox; if(n == 0) return y0(x); if(n == 1) return y1(x); tox=2.0/x; by=y1(x); bym=y0(x); for (int j=1;j<n;j++) { byp=j*tox*by-bym; bym=by; by=byp; } return by; }
/** * Normal double. * * @param a double value * @return The area under the Gaussian probability density function, integrated from minus infinity to x: * @throws ArithmeticException the arithmetic exception */ static public double normal( double a) throws ArithmeticException { double x, y, z; x = a * SQRTH; z = Math.abs(x); if( z < SQRTH ) y = 0.5 + 0.5 * erf(x); else { y = 0.5 * erfc(z); if( x > 0 ) y = 1.0 - y; } return y; }
/** * Erf double. * * @param x the x * @return The Error function Converted to Java from<BR> Cephes Math Library Release 2.2: July, * 1992<BR> Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier<BR> Direct inquiries to 30 Frost Street, * Cambridge, MA 02140<BR> * @throws ArithmeticException the arithmetic exception */ static public double erf(double x) throws ArithmeticException { double y, z; double T[] = { 9.60497373987051638749E0, 9.00260197203842689217E1, 2.23200534594684319226E3, 7.00332514112805075473E3, 5.55923013010394962768E4 }; double U[] = { //1.00000000000000000000E0, 3.35617141647503099647E1, 5.21357949780152679795E2, 4.59432382970980127987E3, 2.26290000613890934246E4, 4.92673942608635921086E4 }; if( Math.abs(x) > 1.0 ) return( 1.0 - erfc(x) ); z = x * x; y = x * polevl( z, T, 4 ) / p1evl( z, U, 5 ); return y; }
/** * @param n integer order * @param x a double value * @return the Bessel function of the second kind, * of order n of the argument. */ static public double yn(int n, double x) throws ArithmeticException { double by,bym,byp,tox; if(n == 0) return y0(x); if(n == 1) return y1(x); tox=2.0/x; by=y1(x); bym=y0(x); for (int j=1;j<n;j++) { byp=j*tox*by-bym; bym=by; by=byp; } return by; }
static private double stirf(double x) throws ArithmeticException { double STIR[] = { 7.87311395793093628397E-4, -2.29549961613378126380E-4, -2.68132617805781232825E-3, 3.47222221605458667310E-3, 8.33333333333482257126E-2, }; double MAXSTIR = 143.01608; double w = 1.0/x; double y = Math.exp(x); w = 1.0 + w * polevl( w, STIR, 4 ); if( x > MAXSTIR ) { /* Avoid overflow in Math.pow() */ double v = Math.pow( x, 0.5 * x - 0.25 ); y = v * (v / y); } else { y = Math.pow( x, x - 0.5 ) / y; } y = SQTPI * y * w; return y; }
/** * @param a double value * @return The area under the Gaussian probability density * function, integrated from minus infinity to x: */ static public double normal( double a) throws ArithmeticException { double x, y, z; x = a * SQRTH; z = Math.abs(x); if( z < SQRTH ) y = 0.5 + 0.5 * erf(x); else { y = 0.5 * erfc(z); if( x > 0 ) y = 1.0 - y; } return y; }
/** * @param a double value * @return The Error function * <P> * <FONT size=2> * Converted to Java from<BR> * Cephes Math Library Release 2.2: July, 1992<BR> * Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier<BR> * Direct inquiries to 30 Frost Street, Cambridge, MA 02140<BR> */ static public double erf(double x) throws ArithmeticException { double y, z; double T[] = { 9.60497373987051638749E0, 9.00260197203842689217E1, 2.23200534594684319226E3, 7.00332514112805075473E3, 5.55923013010394962768E4 }; double U[] = { //1.00000000000000000000E0, 3.35617141647503099647E1, 5.21357949780152679795E2, 4.59432382970980127987E3, 2.26290000613890934246E4, 4.92673942608635921086E4 }; if( Math.abs(x) > 1.0 ) return( 1.0 - erfc(x) ); z = x * x; y = x * polevl( z, T, 4 ) / p1evl( z, U, 5 ); return y; }
/** * @param x a double value * @return the hyperbolic sine of the argument * * @throws ArithmeticException */ static public double sinh(double x) throws ArithmeticException { double a; if (x == 0.0) { return x; } a = x; if (a < 0.0) { a = Math.abs(x); } a = Math.exp(a); if (x < 0.0) { return -0.5 * (a - 1 / a); } else { return 0.5 * (a - 1 / a); } }
/** * @param x a double value * @return the hyperbolic tangent of the argument * * @throws ArithmeticException */ static public double tanh(double x) throws ArithmeticException { double a; if (x == 0.0) { return x; } a = x; if (a < 0.0) { a = Math.abs(x); } a = Math.exp(2.0 * a); if (x < 0.0) { return -(1.0 - 2.0 / (a + 1.0)); } else { return (1.0 - 2.0 / (a + 1.0)); } }
/** * Compute the hyperbolic arc sine * * @param xx a double value * * @return the hyperbolic arc sine of the argument * * @throws ArithmeticException */ static public double asinh(double xx) throws ArithmeticException { double x; int sign; if (xx == 0.0) { return xx; } if (xx < 0.0) { sign = -1; x = -xx; } else { sign = 1; x = xx; } return sign * Math.log(x + Math.sqrt(x * x + 1)); }
/** * @param n integer order * @param x a double value * @return the Bessel function of the second kind, * of order n of the argument. * * @throws ArithmeticException */ static public double yn(int n, double x) throws ArithmeticException { double by, bym, byp, tox; if (n == 0) { return y0(x); } if (n == 1) { return y1(x); } tox = 2.0 / x; by = y1(x); bym = y0(x); for (int j = 1; j < n; j++) { byp = j * tox * by - bym; bym = by; by = byp; } return by; }
/** * Compute the factorial of the argument * * @param j an integer value * * @return the factorial of the argument * * @throws ArithmeticException */ static public int fac(int j) throws ArithmeticException { int i = j; int d = 1; if (j < 0) { i = Math.abs(j); } while (i > 1) { d *= i--; } if (j < 0) { return -d; } else { return d; } }
/** * _more_ * * @param x * @return _more_ * * @throws ArithmeticException */ static private double stirf(double x) throws ArithmeticException { double STIR[] = { 7.87311395793093628397E-4, -2.29549961613378126380E-4, -2.68132617805781232825E-3, 3.47222221605458667310E-3, 8.33333333333482257126E-2, }; double MAXSTIR = 143.01608; double w = 1.0 / x; double y = Math.exp(x); w = 1.0 + w * polevl(w, STIR, 4); if (x > MAXSTIR) { /* Avoid overflow in Math.pow() */ double v = Math.pow(x, 0.5 * x - 0.25); y = v * (v / y); } else { y = Math.pow(x, x - 0.5) / y; } y = SQTPI * y * w; return y; }
/** * @param a double value * @return The area under the Gaussian probability density * function, integrated from minus infinity to x: * * @throws ArithmeticException */ static public double normal(double a) throws ArithmeticException { double x, y, z; x = a * SQRTH; z = Math.abs(x); if (z < SQRTH) { y = 0.5 + 0.5 * erf(x); } else { y = 0.5 * erfc(z); if (x > 0) { y = 1.0 - y; } } return y; }
/** * @param x double value * * @return The Error function * <P> * <FONT size=2> * Converted to Java from<BR> * Cephes Math Library Release 2.2: July, 1992<BR> * Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier<BR> * Direct inquiries to 30 Frost Street, Cambridge, MA 02140<BR> * * @throws ArithmeticException */ static public double erf(double x) throws ArithmeticException { double y, z; double T[] = { 9.60497373987051638749E0, 9.00260197203842689217E1, 2.23200534594684319226E3, 7.00332514112805075473E3, 5.55923013010394962768E4 }; double U[] = { //1.00000000000000000000E0, 3.35617141647503099647E1, 5.21357949780152679795E2, 4.59432382970980127987E3, 2.26290000613890934246E4, 4.92673942608635921086E4 }; if (Math.abs(x) > 1.0) { return (1.0 - erfc(x)); } z = x * x; y = x * polevl(z, T, 4) / p1evl(z, U, 5); return y; }
/** * Runs the test using the specified harness. * * @param harness the test harness (<code>null</code> not permitted). */ public void test(TestHarness harness) { ArithmeticException object1 = new ArithmeticException(); harness.check(object1 != null); harness.check(object1.toString(), "java.lang.ArithmeticException"); ArithmeticException object2 = new ArithmeticException("nothing happens"); harness.check(object2 != null); harness.check(object2.toString(), "java.lang.ArithmeticException: nothing happens"); ArithmeticException object3 = new ArithmeticException(null); harness.check(object3 != null); harness.check(object3.toString(), "java.lang.ArithmeticException"); }
/** * @param a double value * @return The area under the Gaussian probability density * function, integrated from minus infinity to x: */ static public double normal( double a) throws ArithmeticException { double x, y, z; x = a * SQRTH; z = Math.abs(x); if( z < SQRTH ) { y = 0.5 + 0.5 * erf(x); } else { y = 0.5 * erfc(z); if( x > 0 ) { y = 1.0 - y; } } return y; }
public static double divide(double a, double b) { if (b == 0) { throw new ArithmeticException( "Division by 0 is undefined: " + a + "/" + b); } int sign = 1; if (a < 0) { a = -a; sign = -sign; } if (b < 0) { b = -b; sign = -sign; } double result = 0; while (a >= 0) { a -= b; result++; } return (result - 1) * sign; }
/** * Cosh double. * * @param x a double value * @return the hyperbolic cosine of the argument * @throws ArithmeticException the arithmetic exception */ static public double cosh(double x) throws ArithmeticException { double a; a = x; if( a < 0.0 ) a = Math.abs(x); a = Math.exp(a); return 0.5*(a+1/a); }
/** * Sinh double. * * @param x a double value * @return the hyperbolic sine of the argument * @throws ArithmeticException the arithmetic exception */ static public double sinh(double x) throws ArithmeticException { double a; if(x == 0.0) return x; a = x; if( a < 0.0 ) a = Math.abs(x); a = Math.exp(a); if( x < 0.0 ) return -0.5*(a-1/a); else return 0.5*(a-1/a); }
/** * Tanh double. * * @param x a double value * @return the hyperbolic tangent of the argument * @throws ArithmeticException the arithmetic exception */ static public double tanh(double x) throws ArithmeticException { double a; if( x == 0.0 ) return x; a = x; if( a < 0.0 ) a = Math.abs(x); a = Math.exp(2.0*a); if(x < 0.0 ) return -( 1.0-2.0/(a+1.0) ); else return ( 1.0-2.0/(a+1.0) ); }
/** * Asinh double. * * @param xx the xx * @return the hyperbolic arc sine of the argument * @throws ArithmeticException the arithmetic exception */ static public double asinh(double xx) throws ArithmeticException { double x; int sign; if(xx == 0.0) return xx; if( xx < 0.0 ) { sign = -1; x = -xx; } else { sign = 1; x = xx; } return sign*Math.log( x + Math.sqrt(x*x+1)); }
/** * J 1 double. * * @param x a double value * @return the Bessel function of order 1 of the argument. * @throws ArithmeticException the arithmetic exception */ static public double j1(double x) throws ArithmeticException { double ax; double y; double ans1, ans2; if ((ax=Math.abs(x)) < 8.0) { y=x*x; ans1=x*(72362614232.0+y*(-7895059235.0+y*(242396853.1 +y*(-2972611.439+y*(15704.48260+y*(-30.16036606)))))); ans2=144725228442.0+y*(2300535178.0+y*(18583304.74 +y*(99447.43394+y*(376.9991397+y*1.0)))); return ans1/ans2; } else { double z=8.0/ax; double xx=ax-2.356194491; y=z*z; ans1=1.0+y*(0.183105e-2+y*(-0.3516396496e-4 +y*(0.2457520174e-5+y*(-0.240337019e-6)))); ans2=0.04687499995+y*(-0.2002690873e-3 +y*(0.8449199096e-5+y*(-0.88228987e-6 +y*0.105787412e-6))); double ans=Math.sqrt(0.636619772/ax)* (Math.cos(xx)*ans1-z*Math.sin(xx)*ans2); if (x < 0.0) ans = -ans; return ans; } }
/** * Fac int. * * @param j the j * @return the factorial of the argument * @throws ArithmeticException the arithmetic exception */ static public int fac(int j) throws ArithmeticException { int i = j; int d = 1; if(j < 0) i = Math.abs(j); while( i > 1) { d *= i--; } if(j < 0) return -d; else return d; }
/** * Stirf double. * * @param x the x * @return the double * @throws ArithmeticException the arithmetic exception */ /* Gamma function computed by Stirling's formula. * The polynomial STIR is valid for 33 <= x <= 172. Cephes Math Library Release 2.2: July, 1992 Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier Direct inquiries to 30 Frost Street, Cambridge, MA 02140 */ static private double stirf(double x) throws ArithmeticException { double STIR[] = { 7.87311395793093628397E-4, -2.29549961613378126380E-4, -2.68132617805781232825E-3, 3.47222221605458667310E-3, 8.33333333333482257126E-2, }; double MAXSTIR = 143.01608; double w = 1.0/x; double y = Math.exp(x); w = 1.0 + w * polevl( w, STIR, 4 ); if( x > MAXSTIR ) { /* Avoid overflow in Math.pow() */ double v = Math.pow( x, 0.5 * x - 0.25 ); y = v * (v / y); } else { y = Math.pow( x, x - 0.5 ) / y; } y = SQTPI * y * w; return y; }
/** * Igam double. * * @param a double value * @param x double value * @return the Incomplete Gamma function. Converted to Java from<BR> Cephes Math Library Release * 2.2: July, 1992<BR> Copyright 1984, 1987, 1989, 1992 by Stephen L. Moshier<BR> Direct inquiries to 30 Frost * Street, Cambridge, MA 02140<BR> * @throws ArithmeticException the arithmetic exception */ static public double igam(double a, double x) throws ArithmeticException { double ans, ax, c, r; if( x <= 0 || a <= 0 ) return 0.0; if( x > 1.0 && x > a ) return 1.0 - igamc(a,x); /* Compute x**a * exp(-x) / gamma(a) */ ax = a * Math.log(x) - x - lgamma(a); if( ax < -MAXLOG ) return( 0.0 ); ax = Math.exp(ax); /* power series */ r = a; c = 1.0; ans = 1.0; do { r += 1.0; c *= x/r; ans += c; } while( c/ans > MACHEP ); return( ans * ax/a ); }
/** * Pseries double. * * @param a the a * @param b the b * @param x the x * @return the double * @throws ArithmeticException the arithmetic exception */ static private double pseries( double a, double b, double x ) throws ArithmeticException { double s, t, u, v, n, t1, z, ai; ai = 1.0 / a; u = (1.0 - b) * x; v = u / (a + 1.0); t1 = v; t = u; n = 2.0; s = 0.0; z = MACHEP * ai; while( Math.abs(v) > z ) { u = (n - b) * x / n; t *= u; v = t / (a + n); s += v; n += 1.0; } s += t1; s += ai; u = a * Math.log(x); if( (a+b) < MAXGAM && Math.abs(u) < MAXLOG ) { t = gamma(a+b)/(gamma(a)*gamma(b)); s = s * t * Math.pow(x,a); } else { t = lgamma(a+b) - lgamma(a) - lgamma(b) + u + Math.log(s); if( t < MINLOG ) s = 0.0; else s = Math.exp(t); } return s; }
public static double castToDouble(BigInteger n) { BigDecimal dec = new BigDecimal( n ); if( dec.compareTo( new BigDecimal(Double.MAX_VALUE) ) > 0 ) throw new ArithmeticException("Numeric value overflow"); return dec.doubleValue(); }
public static double castToDouble(BigDecimal n) { if( n.compareTo( new BigDecimal(Double.MAX_VALUE) ) > 0 ) throw new ArithmeticException("Numeric value overflow"); return n.doubleValue(); }