Esempio n. 1
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);
    }
Esempio n. 2
0
  public ValueEval evaluate(
      final ValueEval[] args, final int srcRowIndex, final int srcColumnIndex) {
    if (args.length == 0 || args.length > 2) {
      // Wrong number of arguments
      return ErrorEval.VALUE_INVALID;
    }

    try {
      double[] values = AggregateFunction.ValueCollector.collectValues(args[0]);
      double guess;
      if (args.length == 2) {
        guess = NumericFunction.singleOperandEvaluate(args[1], srcRowIndex, srcColumnIndex);
      } else {
        guess = 0.1d;
      }
      double result = irr(values, guess);
      NumericFunction.checkValue(result);
      return new NumberEval(result);
    } catch (EvaluationException e) {
      return e.getErrorEval();
    }
  }
Esempio n. 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;
      }
      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);
    }