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);
  }
示例#2
0
 /** @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();
   }
 }
示例#3
0
    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);
    }
示例#4
0
  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);
    }
  }
示例#5
0
    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);
    }