开始计算

@param formula 表达式

@return {@link BigDecimal}

@throws Exception 异常
15002
private static BigDecimal doCalculate(String formula) throws Exception {
        Stack<BigDecimal> stack = new Stack<>();
        BigDecimal result = BigDecimal.valueOf(0);
        BigDecimal number = BigDecimal.valueOf(0);
        char sign = '+';
        boolean isDecimal = false;
        BigDecimal decimalSign = new BigDecimal(0.1);

        for (int i = 0; i < formula.length(); i++) {
            char c = formula.charAt(i);
            if (Character.isDigit(c)) {
                BigDecimal num = new BigDecimal(c - '0');
                number = isDecimal ? number.add(num.multiply(decimalSign)) : num.add(number.multiply(BigDecimal
                        .valueOf(10)));
                decimalSign = decimalSign.multiply(BigDecimal.valueOf(0.1));
            } else if (c == '.') {
                isDecimal = true;
                decimalSign = BigDecimal.valueOf(0.1);
            } else if (c == '(') {
                BigDecimal[] res = calculateInlineFormula(i + 1, formula);
                number = number.compareTo(BigDecimal.valueOf(0)) == 0 ? res[0] : number.multiply(res[0]);
                i = res[1].intValue();
            }
            boolean isNotDigit = (!Character.isDigit(c) && c != '.' && c != '(') || i == formula.length() - 1;
            if (isNotDigit) {
                if (sign == '+') {
                    stack.push(number);
                } else if (sign == '-') {
                    stack.push(number.multiply(BigDecimal.valueOf(-1)));
                } else if (sign == '*') {
                    stack.push(number.multiply(stack.pop()));
                } else if (sign == '/') {
                    stack.push(stack.pop().divide(number, getPrecision(), BigDecimal.ROUND_HALF_UP));
                }
                sign = c;
                isDecimal = false;
                number = BigDecimal.valueOf(0);
            }
        }
        for (BigDecimal i : stack) {
            result = result.add(i);
        }
        return result.setScale(getPrecision(), BigDecimal.ROUND_HALF_UP);
    }
579288
public BigDecimal calculate(BigDecimal x, MathContext mathContext) {
		BigDecimal acceptableError = ONE.movePointLeft(mathContext.getPrecision() + 1);

		PowerIterator powerIterator = createPowerIterator(x, mathContext);
		
		BigDecimal sum = BigDecimal.ZERO;
		BigDecimal step;
		int i = 0;
		do {
			BigRational factor = getFactor(i);
			BigDecimal xToThePower  = powerIterator.getCurrentPower();
			powerIterator.calculateNextPower();
			step = factor.getNumerator().multiply(xToThePower, mathContext).divide(factor.getDenominator(), mathContext);
			i++;

			if (calculateInPairs) {
				xToThePower  = powerIterator.getCurrentPower();
				powerIterator.calculateNextPower();
				factor = getFactor(i);
				BigDecimal step2 = factor.getNumerator().multiply(xToThePower, mathContext).divide(factor.getDenominator(), mathContext);
				step = step.add(step2, mathContext);
				i++;
			}

			sum = sum.add(step, mathContext);
			//System.out.println(sum + " " + step);
		} while (step.abs().compareTo(acceptableError) > 0);
		
		return sum.round(mathContext);
	}
404415
public BigDecimal getBigDecimal(int index) throws JSONException {
		Object object = this.get(index);
		try {
			return new BigDecimal(object.toString());
		} catch (Exception e) {
			throw new JSONException("JSONArray[" + index +
					"] could not convert to BigDecimal.");
		}
	}
224257
private static Object toInt( BigDecimal value)
    {
    Object number;
        
    try
      {
      number =
        value.scale() == 0
        ? Integer.valueOf( value.intValueExact())
        : null;
      }
    catch( Exception e)
      {
      number = null;
      }
        
    return number;
    }
145837
public Formula formula(final FormulaFactory f) {
    if (this.operator != FType.AND && this.operator != FType.OR)
      throw new IllegalStateException("Illegal operator for formula list formula construction: " + this.operator);
    if (this.formula == null) {
      if (this.operator == FType.AND)
        this.formula = f.and(this.formulas);
      else
        this.formula = f.or(this.formulas);
    }
    return this.formula;
  }
475464
public Formula encode(final Formula formula) {
    switch (this.config().algorithm) {
      case FACTORIZATION:
        if (this.factorization == null)
          this.factorization = new CNFFactorization();
        return formula.transform(this.factorization);
      case TSEITIN:
        if (this.tseitin == null || this.currentAtomBoundary != this.config().atomBoundary) {
          this.currentAtomBoundary = this.config().atomBoundary;
          this.tseitin = new TseitinTransformation(this.config().atomBoundary);
        }
        return formula.transform(this.tseitin);
      case PLAISTED_GREENBAUM:
        if (this.plaistedGreenbaum == null || this.currentAtomBoundary != this.config().atomBoundary) {
          this.currentAtomBoundary = this.config().atomBoundary;
          this.plaistedGreenbaum = new PlaistedGreenbaumTransformation(this.config().atomBoundary);
        }
        return formula.transform(this.plaistedGreenbaum);
      case BDD:
        if (this.bddCnfTransformation == null)
          this.bddCnfTransformation = new BDDCNFTransformation();
        return formula.transform(this.bddCnfTransformation);
      case ADVANCED:
        if (this.factorizationHandler == null) {
          this.factorizationHandler = new AdvancedFactorizationHandler();
          this.advancedFactorization = new CNFFactorization(this.factorizationHandler);
        }
        this.factorizationHandler.reset(this.config().distributionBoundary, this.config().createdClauseBoundary);
        return this.advancedEncoding(formula);
      default:
        throw new IllegalStateException("Unknown CNF encoding algorithm: " + this.config().algorithm);
    }
  }
269062
public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
		return convert(BigDecimal.class, value, defaultValue);
	}
1042416
public static Backbone computePositive(final Formula formula) {
        return compute(formula, formula.variables(), BackboneType.ONLY_POSITIVE);
    }
849033
private static String matchDoneBigDecimal(String bigDecimal) {
        // 对科学计数法进行处理
        boolean flg = Pattern.matches("^-?\\d+(\\.\\d+)?(E-?\\d+)?$", bigDecimal);
        if (flg) {
            BigDecimal bd = new BigDecimal(bigDecimal);
            bigDecimal = bd.toPlainString();
        }
        return bigDecimal;
    }
65448
public static Backbone computeNegative(final Collection<Formula> formulas) {
        return compute(formulas, allVariablesInFormulas(formulas), BackboneType.ONLY_NEGATIVE);
    }
384302
private static BigDecimal getBigDecimalOrNull(Object value) {
            if ( !(value instanceof Number || value instanceof String) ) {
                return null;
            }
            if ( !BigDecimal.class.isAssignableFrom( value.getClass() ) ) {
                if ( value instanceof Long || value instanceof Integer || value instanceof Short || value instanceof Byte ||
                     value instanceof AtomicLong || value instanceof AtomicInteger ) {
                    value = new BigDecimal( ((Number) value).longValue(), MathContext.DECIMAL128 );
                } else if ( value instanceof BigInteger ) {
                    value = new BigDecimal( ((BigInteger) value).toString(), MathContext.DECIMAL128 );
                } else if ( value instanceof String ) {
                    // we need to remove leading zeros to prevent octal conversion
                    value = new BigDecimal( ((String) value).replaceFirst("^0+(?!$)", ""), MathContext.DECIMAL128 );
                } else {
                    value = new BigDecimal( ((Number) value).doubleValue(), MathContext.DECIMAL128 );
                }
            }
            return (BigDecimal) value;
        }
1060922
private static Object toNumber( String value)
    {
    Object number;

    try
      {
      number = toExternalNumber( new BigDecimal( value));
      }
    catch( Exception e)
      {
      number = null;
      }

    return number;
    }
239152
public synchronized BigDecimal getBigDecimal(int parameterIndex,
            int scale) throws SQLException {

        if (scale < 0) {
            throw Util.outOfRangeArgument();
        }

        // boucherb@users 20020502 - added conversion
        BigDecimal bd = (BigDecimal) getColumnInType(parameterIndex,
            Type.SQL_DECIMAL);

        if (bd != null) {
            bd = bd.setScale(scale, BigDecimal.ROUND_DOWN);
        }

        return bd;
    }
542211
public final BigDecimal getDecimal(final int pos, final int precision, final int scale) {
        final int intg = precision - scale;
        final int frac = scale;
        final int intg0 = intg / DIG_PER_INT32;
        final int frac0 = frac / DIG_PER_INT32;
        final int intg0x = intg - intg0 * DIG_PER_INT32;
        final int frac0x = frac - frac0 * DIG_PER_INT32;

        final int binSize = intg0 * SIZE_OF_INT32 + dig2bytes[intg0x] + frac0 * SIZE_OF_INT32 + dig2bytes[frac0x];
        if (pos + binSize > limit || pos < 0) {
            throw new IllegalArgumentException("limit excceed: " + (pos < 0 ? pos : (pos + binSize)));
        }
        return getDecimal0(origin + pos, intg, frac, // NL
            intg0,
            frac0,
            intg0x,
            frac0x);
    }
636805
protected BigDecimal coerceToBigDecimal(Value value) {
		
		if (!(value instanceof NumberValue))
			throw new CommandExecutionException("Value is not a number");
		return ((NumberValue) value).get();
	}
887684
public void add(final Formula formula) {
        final Formula cnf = formula.cnf();
        switch (cnf.type()) {
            case TRUE:
                break;
            case FALSE:
            case LITERAL:
            case OR:
                this.addClause(generateClauseVector(cnf), null);
                break;
            case AND:
                for (final Formula op : cnf) {
                    this.addClause(generateClauseVector(op), null);
                }
                break;
            default:
                throw new IllegalStateException("Unexpected formula type in CNF: " + cnf.type());
        }
    }
691727
public BigDecimal toBigDecimal(Object val) {
    if (val instanceof BigDecimal) {
      return roundBigDecimal((BigDecimal) val);
    }
    else if (val == null) {
      return BigDecimal.ZERO;
    }
    else if (val instanceof String) {
      String string = ((String) val).trim();
      if ("".equals(string)) {
        return BigDecimal.ZERO;
      }
      return roundBigDecimal(new BigDecimal(string, getMathContext()));
    }
    else if (val instanceof Double) {
      if (!Double.isNaN(((Double) val).doubleValue())) {
        return roundBigDecimal(new BigDecimal(val.toString(), getMathContext()));
      }
      else {
        return BigDecimal.ZERO;
      }
    }
    else if (val instanceof Number) {
      return roundBigDecimal(new BigDecimal(val.toString(), getMathContext()));
    }
    else if (val instanceof Character) {
      int i = ((Character) val).charValue();
      return new BigDecimal(i);
    }

    throw new ArithmeticException("BigDecimal coercion: " + val.getClass().getName() + ":(" + val + ")");
  }