public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { double dn; try { ValueEval ve1 = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex); dn = OperandResolver.coerceValueToDouble(ve1); } catch (EvaluationException e1) { // all errors in the second arg translate to #VALUE! return ErrorEval.VALUE_INVALID; } // weird Excel behaviour on second arg if (dn < 1.0) { // values between 0.0 and 1.0 result in #NUM! return ErrorEval.NUM_ERROR; } // all other values are rounded up to the next integer int k = (int) Math.ceil(dn); double result; try { double[] ds = ValueCollector.collectValues(arg0); if (k > ds.length) { return ErrorEval.NUM_ERROR; } result = _isLarge ? StatsLib.kthLargest(ds, k) : StatsLib.kthSmallest(ds, k); NumericFunction.checkValue(result); } catch (EvaluationException e) { return e.getErrorEval(); } return new NumberEval(result); }
/** @return the de-referenced criteria arg (possibly {@link ErrorEval}) */ private static ValueEval evaluateCriteriaArg(ValueEval arg, int srcRowIndex, int srcColumnIndex) { try { return OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex); } catch (EvaluationException e) { return e.getErrorEval(); } }
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) { try { OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex); return ErrorEval.NA; } catch (EvaluationException e) { int result = translateErrorCodeToErrorTypeValue(e.getErrorEval().getErrorCode()); return new NumberEval(result); } }
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { double dn; try { ValueEval ve1 = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex); dn = OperandResolver.coerceValueToDouble(ve1); } catch (EvaluationException e1) { // all errors in the second arg translate to #VALUE! return ErrorEval.VALUE_INVALID; } if (dn < 0 || dn > 1) { // has to be percentage return ErrorEval.NUM_ERROR; } double result; try { double[] ds = ValueCollector.collectValues(arg0); int N = ds.length; if (N == 0 || N > 8191) { return ErrorEval.NUM_ERROR; } double n = (N - 1) * dn + 1; if (n == 1d) { result = StatsLib.kthSmallest(ds, 1); } else if (n == N) { result = StatsLib.kthLargest(ds, 1); } else { int k = (int) n; double d = n - k; result = StatsLib.kthSmallest(ds, k) + d * (StatsLib.kthSmallest(ds, k + 1) - StatsLib.kthSmallest(ds, k)); } NumericFunction.checkValue(result); } catch (EvaluationException e) { return e.getErrorEval(); } return new NumberEval(result); }
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { // verify that we have enough data if (args.length != 3) { return ErrorEval.VALUE_INVALID; } // declare doubles for values double principal, rate, years, result; try { // extract values as ValueEval ValueEval v1 = OperandResolver.getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex()); ValueEval v2 = OperandResolver.getSingleValue(args[1], ec.getRowIndex(), ec.getColumnIndex()); ValueEval v3 = OperandResolver.getSingleValue(args[2], ec.getRowIndex(), ec.getColumnIndex()); // get data as doubles principal = OperandResolver.coerceValueToDouble(v1); rate = OperandResolver.coerceValueToDouble(v2); years = OperandResolver.coerceValueToDouble(v3); result = calculateMortgagePayment(principal, rate, years); System.out.println("Result = " + result); checkValue(result); } catch (EvaluationException e) { return e.getErrorEval(); } return new NumberEval(result); }
public boolean matches(ValueEval x) { double testValue; if (x instanceof StringEval) { // if the target(x) is a string, but parses as a number // it may still count as a match, only for the equality operator switch (getCode()) { case CmpOp.EQ: case CmpOp.NONE: break; case CmpOp.NE: // Always matches (inconsistent with above two cases). // for example '<>123' matches '123', '4', 'abc', etc return true; default: // never matches (also inconsistent with above three cases). // for example '>5' does not match '6', return false; } StringEval se = (StringEval) x; Double val = OperandResolver.parseDouble(se.getStringValue()); if (val == null) { // x is text that is not a number return false; } return _value == val.doubleValue(); } else if ((x instanceof NumberEval)) { NumberEval ne = (NumberEval) x; testValue = ne.getNumberValue(); } else if ((x instanceof BlankEval)) { switch (getCode()) { case CmpOp.NE: // Excel counts blank values in range as not equal to any value. See Bugzilla 51498 return true; default: return false; } } else { return false; } return evaluate(Double.compare(testValue, _value)); }
/** When the second argument is a string, many things are possible */ private static I_MatchPredicate createGeneralMatchPredicate(StringEval stringEval) { String value = stringEval.getStringValue(); CmpOp operator = CmpOp.getOperator(value); value = value.substring(operator.getLength()); Boolean booleanVal = parseBoolean(value); if (booleanVal != null) { return new BooleanMatcher(booleanVal.booleanValue(), operator); } Double doubleVal = OperandResolver.parseDouble(value); if (doubleVal != null) { return new NumberMatcher(doubleVal.doubleValue(), operator); } ErrorEval ee = parseError(value); if (ee != null) { return new ErrorMatcher(ee.getErrorCode(), operator); } // else - just a plain string with no interpretation. return new StringMatcher(value, operator); }