/** * See if value is outside two other values. * * @param x Value to check * @param x1 Value at one end of interval. * @param x2 Value at other end of interval. * @return Return 0 if x is between x1 and x2, * -1 if outside and closer to x1, 1 if outside and closer to x2. */ public int outside(final double x, final double x1, final double x2) { int i = 0; if (between(x, x1, x2)) { i = 0; } else if (between(x1, x, x2)) { i = -1; } else if (between(x2, x, x1)) { i = 1; } return i; }
/** * Check whether first number is strictly greater than second. * * @param r1 First number to check * @param r2 Second number to check * @return true if r1>r2 */ public boolean gt(final double r1, final double r2) { return cmp(r1, r2) > 0; }
Almost a = new Almost(); assert (a.between(1., 0., 2.)); assert (a.between(-1., 0., -2.)); assert (a.between(-1., -0.5, -2.)); assert (a.outside(1., 0., 2.) ==0); assert (a.outside(1., 0.5, 2.) ==0); assert (a.outside(-1., 0., -2.) ==0); assert (a.outside(-1., -0.5, -2.) ==0); assert (a.cmp(1., 0.) > 0); assert (a.cmp(0., 1.) < 0); assert (a.cmp(1., 1.) == 0); assert (a.cmp(0., 0.) == 0); assert (a.equal(3.,3.)); assert (a.equal(0.,0.)); assert (a.zero(0.)); assert (a.zero(a.getMinValue()/2.)); assert (!a.zero(a.getMinValue()*2.)); assert (1. != 1.+a.getEpsilon()); assert (1. != 1.-a.getEpsilon()); assert (0. != a.getMinValue()); assert (a.equal(1., 1.+a.getEpsilon()/2.)); assert (!a.equal(1., 1.+a.getEpsilon()*2.1)); assert (a.equal(1., 1.000000000001)); assert (a.getMinValue()/2.>0.); assert (a.equal(0., a.getMinValue()/2.)); assert (a.between(1., 1.000000000001, 2.)); assert (a.between(-1., -1.000000000001, -2.));
/** test Object methods */ public void testAlmostObjectMethod() { Almost af1= new Almost(10*MathPlus.FLT_EPSILON, 100*Float.MIN_VALUE); Almost af2 = new Almost(10*MathPlus.FLT_EPSILON); Almost af3 = new Almost(); Almost ad = new Almost(10*MathPlus.DBL_EPSILON, 100*Double.MIN_VALUE); assert af1.equals(af2): af1+" "+af2; assert af1.equals(af3): af1+" "+af3; assert af2.equals(af3): af2+" "+af3; assert af1.hashCode() == af2.hashCode(): af1+" "+af2; assert af1.hashCode() == af3.hashCode(): af1+" "+af3; assert af2.hashCode() == af3.hashCode(): af2+" "+af3; assert af1.toString().equals(af2.toString()): af1.toString()+" "+af2.toString(); assert af1.toString().equals(af3.toString()): af1.toString()+" "+af3.toString(); assert af2.toString().equals(af3.toString()): af2.toString()+" "+af3.toString(); for (Almost af: new Almost[] {af1, af2, af3}) { assert !af.equals(ad); assert af.hashCode() != ad.hashCode(); assert !af.toString().equals(ad.toString()); } }
Almost a = new Almost(0.001,0.000001); assert (a.hashCodeOf(0.00000001,100) == 0); assert (a.hashCodeOf(0.99999999,100) == 1); assert (a.hashCodeOf(1.00000001,100) == 1); assert (a.hashCodeOf(123456789L,100) == 123456789L); assert (a.hashCodeOf(3.1415,4) == a.hashCodeOf(3.1415926,4)); assert (a.hashCodeOf(3.1415,5) != a.hashCodeOf(3.1415926,5)); assert (a.hashCodeOf(-3.1415,4) == a.hashCodeOf(-3.1415926,4)); assert (a.hashCodeOf(-3.1415,5) != a.hashCodeOf(-3.1415926,5)); assert (a.hashCodeOf(314.15,4) == a.hashCodeOf(314.15926,4)); assert (a.hashCodeOf(314.15,5) != a.hashCodeOf(314.15926,5)); assert (a.hashCodeOf(-314.15,4) == a.hashCodeOf(-314.15926,4)); assert (a.hashCodeOf(-314.15,5) != a.hashCodeOf(-314.15926,5)); assert (a.hashCodeOf(0.0031415,4) == a.hashCodeOf(0.0031415926,4)); assert (a.hashCodeOf(0.0031415,5) != a.hashCodeOf(0.0031415926,5)); a = new Almost(0.0001); assert (a.equal(0.0031415,0.0031415926)); assert (a.hashCodeOf(0.0031415) ==
/** test handling of nans */ public void testNaNs() { try { Almost.FLOAT.equal(3,Float.NaN); assert false;} catch (IllegalArgumentException e) {} try { Almost.FLOAT.equal(0,Float.NaN); assert false;} catch (IllegalArgumentException e) {} try { Almost.FLOAT.equal(3,Double.NaN); assert false;} catch (IllegalArgumentException e) {} try { Almost.FLOAT.equal(0,Double.NaN); assert false;} catch (IllegalArgumentException e) {} }
double bb = b.dot(b); checkNaN(bb); assert ! Almost.FLOAT.zero(bb) : "Cannot test with zero-magnitude b"; assert !Almost.FLOAT.zero(dFb) : "zero magnitude test: dFb is zero"; assert !Almost.FLOAT.zero(Adb) : "zero magnitude test: Adb is zero"; checkNaN(dFb); checkNaN(Adb); boolean matches = false; while (! matches && significantDigits > 0) { Almost almost = new Almost (significantDigits); matches = almost.equal(dFb, Adb); if (!matches) { --significantDigits;
final double x3, final double f3) throws BadParabolaException, IllegalArgumentException { if (!Almost.FLOAT.le(x1, x2) || !Almost.FLOAT.le(x2, x3)) { throw new BadParabolaException ("Violates x1 <= x2 <= x3: x1=" + x1 + " x2=" + x2 + " x3=" + x3); if (Almost.FLOAT.equal(x1, x2)) { if (Almost.FLOAT.equal(x2, x3)) { if (!Almost.FLOAT.le(f2, f1) || !Almost.FLOAT.le(f2, f3)) { throw new BadParabolaException ("Violates f(x2) <= f(x1), f(x2) <= f(x3)" + ": f1=" + f1 + " f2=" + f2 + " f3=" + f3); double xm = Almost.FLOAT.divide((x2 - x1), (x3 - x1), 0.0); if (xm < 0.001 || xm > 0.999) { throw new BadParabolaException ("Parabola is badly sampled x1=" + x1 + " x2=" + x2 + " x3=" + x3); final double a = Almost.FLOAT.divide(((f3 - f1) * xm - (f2 - f1)), (xm - xm * xm), 0.0); final double b = f3 - f1 - a; if (Almost.FLOAT.ge(a * b, 0.0) || 0.5 * Math.abs(b) > Math.abs(a)) { throw new BadParabolaException ("Poor numerical conditioning a=" + a + " b=" + b); xm = Almost.FLOAT.divide(-0.5 * b, a, -1.0); if (xm < 0.0 || xm > 1.0) { throw new BadParabolaException
ls.add(out[j], in[j]); Almost almost = new Almost(); double a, b; assert almost.equal(a+b, ls.get(new double[]{a,b})[0]): a+"+"+b+"!="+ls.get(new double[]{a,b})[0] ; assert almost.equal(a+b, ls.get(new double[]{a,b})[0]) : a+"+"+b+"!="+ls.get(new double[]{a,b})[0]; assert almost.equal(a+b, ls.get(new double[]{a,b})[0]) : a+"+"+b+"!="+ls.get(new double[]{a,b})[0]; assert almost.equal(a+b, ls.get(new double[]{a,b})[0]) : a+"+"+b+"!="+ls.get(new double[]{a,b})[0]; assert almost.equal(a+b, ls.get(new double[]{a,b})[0]) : a+"+"+b+"!="+ls.get(new double[]{a,b})[0];
/** * Safely take the reciprocal of a number. * * @param value Value to take reciprocal of. * @return reciprocal of value */ public double reciprocal(final double value) { return divide(1.0, value, 0.0); }
if (Almost.FLOAT.zero(bb)) { LOG.fine("Gradient of quadratic is negligible. Not solving"); final Vect result = VectUtil.cloneZero(b); final double pq = p.dot(q); checkNaN(pq); beta = Almost.FLOAT.divide(pq, pu, 0); if (beta < -5.0) { beta = -5.0; pu = p.dot(u); checkNaN(pu); if (Almost.FLOAT.zero(pg) || Almost.FLOAT.zero(pu)) { break SOLVE;
/** * Return a hashcode consistent with imprecise floating-point equality. * Integers and Longs require exact equality. * The precision is consistent with the value of epsilon. * * @param number number to get hashcode from * @return hashcode */ public int hashCodeOf(final Number number) { double epsilon = _epsilon; int digits = 0; while (epsilon < 0.99) { epsilon *= 10.0; ++digits; } return hashCodeOf(number, digits); }
@Override public boolean equals(final Object object) { if (this == object) { return true; } else if (object == null || getClass() != object.getClass() || hashCode() != object.hashCode()) { return false; } final Almost other = (Almost) object; return _epsilon == other._epsilon && _minValue == other._minValue; }
if (Almost.FLOAT.zero(pp)) { perturbation.dispose(); transformQuadratic.dispose();
/** * See if two vectors are the same. Useful for test code. * * @param v1 First vector * @param v2 Second vector * @return true if vectors appear to be the same, within * floating precision. */ public static boolean areSame(final VectConst v1, final VectConst v2) { final double aa = v1.dot(v1); final double ab = v1.dot(v2); final double bb = v2.dot(v2); // LOG.info("aa="+aa+" ab="+ab+" bb="+bb); return ALMOST_DOT.equal(aa, bb) && ALMOST_DOT.equal(aa, ab) && ALMOST_DOT.equal(ab, bb); }
LOG.fine("result = "+result); assert (new Almost(4)).equal(1., result.getData()[0]):"result="+result; assert (new Almost(5)).equal(4., result.getData()[1]):"result="+result; assert (new Almost(4)).equal(1., result.getData()[0]):"result="+result; assert (new Almost(5)).equal(4., result.getData()[1]):"result="+result; model.dispose(); result.dispose(); assert (new Almost(4)).equal(1., result.getData()[0]):"result="+result; assert (new Almost(5)).equal(4., result.getData()[1]):"result="+result; assert dampAll[0] > dampPerturb[0]; assert dampAll[1] < dampPerturb[1]; LOG.fine("model = "+model); LOG.fine("result = "+result); assert (new Almost(3)).equal(1., result.getData()[0]):"result="+result; assert (new Almost(4)).equal(4., result.getData()[1]):"result="+result; model.dispose(); result.dispose(); LOG.fine("result = "+result); assert (new Almost(4)).equal(1., result.getData()[0]):"result="+result; assert (new Almost(5)).equal(4., result.getData()[1]):"result="+result; model.dispose(); result.dispose();
@Override public void multiplyInverseCovariance() { _value = Almost.FLOAT.divide(_value, _variance, 0.0); }