Exemple #1
0
 public IValue log(INumber x, INumber base) {
   try {
     return x.toReal().log(base.toReal(), values.getPrecision());
   } catch (ArithmeticException ae) {
     throw RuntimeExceptionFactory.arithmeticException(ae.getMessage(), null, null);
   }
 }
Exemple #2
0
 public IValue pow(INumber x, IInteger y) {
   try {
     return x.toReal().pow(y);
   } catch (ArithmeticException ae) {
     throw RuntimeExceptionFactory.arithmeticException(ae.getMessage(), null, null);
   }
 }
 /** Разделить первый аргумент на второй */
 public void divide(double a, double b) {
   try {
     this.result = a / b;
   } catch (ArithmeticException exc) {
     System.out.println(exc.getMessage());
   }
 }
Exemple #4
0
 public IValue nroot(INumber x, IInteger y) {
   try {
     return x.toReal().nroot(y, values.getPrecision());
   } catch (ArithmeticException ae) {
     throw RuntimeExceptionFactory.arithmeticException(ae.getMessage(), null, null);
   }
 }
Exemple #5
0
 public double divide(double a, double b) {
   try {
     return a / b;
   } catch (ArithmeticException exception) {
     System.err.println(exception.getMessage());
   }
   return Double.POSITIVE_INFINITY;
 }
Exemple #6
0
 public int invoke(LuaState luaState) throws LuaRuntimeException {
   try {
     @SuppressWarnings("unused")
     int a = 0 / 0;
   } catch (ArithmeticException e) {
     throw new LuaRuntimeException(e.getMessage(), e);
   }
   return 0;
 }
 public static void tryCatchDummy() throws Throwable {
   try {
     throw new Exception("Dummy exception");
   } catch (ArithmeticException ex) {
     throw new IOException(ex.getMessage());
   } catch (IOException ex) {
     throw new Exception(ex);
   } catch (Exception ex) {
     throw new Exception(ex);
   }
 }
Exemple #8
0
 public IValue scale(INumber x) {
   try {
     if (x.getType().isIntegerType()) {
       IInteger k = (IInteger) x;
       return values.integer(k.toReal().scale());
     }
     if (x.getType().isRationalType()) {
       IRational k = (IRational) x;
       return values.integer(k.toReal().scale());
     }
     return values.integer(((IReal) x).scale());
   } catch (ArithmeticException ae) {
     throw RuntimeExceptionFactory.arithmeticException(ae.getMessage(), null, null);
   }
 }
Exemple #9
0
 public static void main(String[] args) {
   try {
     System.out.println(3 / 0);
     System.out.println("Print me"); // this line not reached
   } catch (ArithmeticException ae) {
     System.out.println("Invalid input...terminating program.....");
     System.out.println(
         "Error :"
             + ae.getMessage()); // if you dont know the reason you can use it to get the error
     // message
   } catch (ArrayIndexOutOfBoundsException aio) {
     System.out.println("Arrya Limits crossed");
   }
   System.out.println("Program Completed");
   System.out.println(5 + 2);
 }
  public static void main(String args[]) {
    Scanner teclado = new Scanner(System.in);

    System.out.print("Digite o numerador: ");
    String xs = teclado.nextLine();

    System.out.print("Digite o denominador: ");
    String ys = teclado.nextLine();

    teclado.close();

    int x = Integer.parseInt(xs), y = Integer.parseInt(ys);

    try {
      int divisao = divide(x, y);
      System.out.println("Resultado da divisao: " + divisao);
    } catch (ArithmeticException erro) {
      System.err.println("Erro: " + erro.getMessage());
    }
  }
Exemple #11
0
  public Function(String formula, ViewPort port, double step) {
    this.formula = formula;
    this.xmin = BigDecimal.valueOf(port.get_xmin());
    this.xmax = BigDecimal.valueOf(port.get_xmax());

    Parser parser = new Parser();
    BigDecimal run = xmin;
    while (run.compareTo(xmax) <= 0) {
      try {
        this.add_valuepair(run, parser.parse_x(formula, String.valueOf(run)));
        run = run.add(BigDecimal.valueOf(step));
      } catch (ArithmeticException e) {
        run = run.add(BigDecimal.valueOf(step));
        System.out.println(
            "Arithmetik Exception wahrscheinlich Division durch 0 übersprungen."
                + e.getMessage()
                + e.getLocalizedMessage());
        continue;
      }
    }
  }
Exemple #12
0
 /**
  * Evaluates this binary add arithmetic function.
  *
  * @return the result of the evaluation.
  * @throws EvaluationException if the function is not evaluable or if an arithmetic error occurs
  *     during the evaluation.
  * @see pddl4j.exp.fexp.OpExp#isEvaluable()
  */
 public Number evaluate() throws EvaluationException {
   if (!this.isGround())
     throw new EvaluationException("arithmetic function " + this.toString() + " not ground");
   try {
     Number arg1 = null;
     if (this.getArg1().getTermID().equals(TermID.ARITHMETIC_FUNCTION)) {
       OpExp func = (OpExp) this.getArg1();
       arg1 = func.evaluate();
     } else if (this.getArg1().getTermID().equals(TermID.NUMBER)) {
       arg1 = (Number) this.getArg1();
     } else {
       throw new EvaluationException(
           "arithmetic function "
               + this.toString()
               + ": argument "
               + this.getArg1()
               + " is not evaluable");
     }
     Number arg2 = null;
     if (this.getArg2().getTermID().equals(TermID.ARITHMETIC_FUNCTION)) {
       OpExp func = (OpExp) this.getArg2();
       arg2 = func.evaluate();
     } else if (this.getArg2().getTermID().equals(TermID.NUMBER)) {
       arg2 = (Number) this.getArg2();
     } else {
       throw new EvaluationException(
           "arithmetic function "
               + this.toString()
               + ": argument "
               + this.getArg2()
               + " is not evaluable");
     }
     return new Number(arg1.getValue() + arg2.getValue());
   } catch (ArithmeticException e) {
     throw new EvaluationException(
         "arithmetic function " + this.toString() + ": " + e.getMessage(), e);
   }
 }
Exemple #13
0
  // ======================================================================
  // [FUNC] Accepts input key from the user.
  public void inputKey(String key) {
    if (hasError) {
      if (key == CXX || key == GRP) {
      } else if (key == HEX || key == DEC || key == OCT || key == BIN) initFields();
      else {
        beep();
        return;
      }
    } else if (lastKey == EQU && key != BSP) {
      clear();
    }

    switch (key) {
      case GRP:
        groupDigits = !groupDigits;
        if (hasError) return;
        break;
      case BIN:
      case OCT:
      case DEC:
      case HEX:
        prmScreenText = dec2rad(rad2dec(prmScreenText, numMode), key);
        numMode = key;
        clearPrmScreen = true;
        break;
      case MCX:
        memValue = "0";
        clearPrmScreen = true;
        break;
      case MRX:
        if (memValue != "0") prmScreenText = dec2rad(memValue, numMode);
        else {
          prmScreenText = "0";
          lastKey = DG0;
          return;
        }
        clearPrmScreen = true;
        break;
      case MSX:
        memValue = rad2dec(prmScreenText, numMode);
        clearPrmScreen = true;
        break;
      case MPX:
        memValue =
            new BigDecimal(rad2dec(prmScreenText, numMode))
                .add(new BigDecimal(memValue))
                .toPlainString();
        clearPrmScreen = true;
        break;
      case MMX:
        memValue =
            new BigDecimal(rad2dec(memValue, numMode))
                .subtract(new BigDecimal(prmScreenText))
                .toPlainString();
        clearPrmScreen = true;
        break;
      case BSP:
        if (clearPrmScreen) {
          beep();
          return;
        } else if (prmScreenText.length() > 1)
          prmScreenText = prmScreenText.substring(0, prmScreenText.length() - 1);
        else if (prmScreenText != "0") prmScreenText = "0";
        else if (expr[0].hasItems()) pop();
        else beep();
        break;
      case CEX:
        prmScreenText = "0";
        break;
      case CXX:
        this.initFields();
        break;
      case AVG:
        if (nset.size() > 0) {
          BigDecimal bd = BigDecimal.ZERO;
          for (int i = 0; i < nset.size(); i++) bd = bd.add((BigDecimal) nset.get(i));
          prmScreenText =
              dec2rad(
                  bd.divide(new BigDecimal(nset.size()), 32, BigDecimal.ROUND_HALF_EVEN)
                      .stripTrailingZeros()
                      .toPlainString(),
                  numMode);
        } else throwError("Invalid Operation: Empty set");
        clearPrmScreen = true;
        break;
      case SUM:
        BigDecimal bd = BigDecimal.ZERO;
        for (int i = 0; i < nset.size(); i++) bd = bd.add((BigDecimal) nset.get(i));
        prmScreenText = dec2rad(bd.toPlainString(), numMode);
        clearPrmScreen = true;
        if (prmScreenText == "0") {
          lastKey = DG0;
          return;
        }
        break;
      case LST:
        if (prmScreenText != "0") nset.add(new BigDecimal(rad2dec(prmScreenText, numMode)));
        else beep();
        clearPrmScreen = true;
        break;
      case CLS:
        nset.clear();
        break;
      case DG0:
        if (clearPrmScreen) {
          prmScreenText = "0";
          clearPrmScreen = false;
        } else if (prmScreenText != "0") prmScreenText += key;
        else if (lastKey == DG0) beep();
        break;
      case DG1:
      case DG2:
      case DG3:
      case DG4:
      case DG5:
      case DG6:
      case DG7:
      case DG8:
      case DG9:
      case DGA:
      case DGB:
      case DGC:
      case DGD:
      case DGE:
      case DGF:
        if (clearPrmScreen || prmScreenText == DG0) {
          prmScreenText = key;
          clearPrmScreen = false;
        } else prmScreenText += key;
        break;
      case DOT:
        if (clearPrmScreen || prmScreenText == DG0) {
          prmScreenText = DG0 + DOT;
          clearPrmScreen = false;
        } else if (prmScreenText.indexOf(DOT) < 0) prmScreenText += key;
        else beep();
        break;
      case BR1:
      case BR2:
      case SRT:
      case CRT:
      case REC:
      case SQR:
      case CUB:
      case FCT:
      case SIN:
      case COS:
      case TAN:
      case LOG:
      case NLG:
      case INT:
      case NEG:
      case POW:
      case MUL:
      case DIV:
      case MOD:
      case ADD:
      case SUB:
        if (prmScreenText != "0" || isDigit(lastKey)) push(prmScreenText);
        if (key.equals(BR1)) push(Expression.BRO);
        else if (key.equals(BR2)) push(Expression.BRC);
        else if (key.equals(SRT)) push(Expression.SRT);
        else if (key.equals(CRT)) push(Expression.CRT);
        else if (key.equals(REC)) push(Expression.REC);
        else if (key.equals(SQR)) push(Expression.SQR);
        else if (key.equals(CUB)) push(Expression.CUB);
        else if (key.equals(FCT)) push(Expression.FCT);
        else if (key.equals(SIN)) push(Expression.SIN);
        else if (key.equals(COS)) push(Expression.COS);
        else if (key.equals(TAN)) push(Expression.TAN);
        else if (key.equals(LOG)) push(Expression.LOG);
        else if (key.equals(NLG)) push(Expression.NLG);
        else if (key.equals(INT)) push(Expression.INT);
        else if (key.equals(NEG)) push(Expression.NEG);
        else if (key.equals(POW)) push(Expression.POW);
        else if (key.equals(MUL)) push(Expression.MUL);
        else if (key.equals(DIV)) push(Expression.DIV);
        else if (key.equals(MOD)) push(Expression.MOD);
        else if (key.equals(ADD)) push(Expression.ADD);
        else if (key.equals(SUB)) push(Expression.SUB);
        prmScreenText = "0";
        clearPrmScreen = false;
        break;
      case EQU:
        if (prmScreenText != "0" || isDigit(lastKey) || !expr[0].hasItems()) push(prmScreenText);
        try {
          prmScreenText = stripZeros(expr[0].eval().toPlainString());
          if (numMode == BIN) prmScreenText = dec2rad(prmScreenText, BIN);
          else if (numMode == OCT) prmScreenText = dec2rad(prmScreenText, OCT);
          else if (numMode == HEX) prmScreenText = dec2rad(prmScreenText, HEX);
          clearPrmScreen = true;
        } catch (SyntaxErrorException e) {
          throwError("Syntax Error: " + e.getMessage());
        } catch (InvalidInputException e) {
          throwError("Input Error: " + e.getMessage());
        } catch (UnknownOperatorException e) {
          throwError("Unknown Operator: " + e.getMessage());
        } catch (ArithmeticException e) {
          throwError("Math Error: " + e.getMessage());
        } catch (Exception e) {
          throwError("Application Error: " + e.getMessage());
        }
        break;
    }

    lastKey = key;
  }
  private static Map<String, ExchangeRate> requestExchangeRates(
      final URL url,
      float dogeBtcConversion,
      final String userAgent,
      final String source,
      final String... fields) {
    final long start = System.currentTimeMillis();

    HttpURLConnection connection = null;
    Reader reader = null;

    try {
      connection = (HttpURLConnection) url.openConnection();

      connection.setInstanceFollowRedirects(false);
      connection.setConnectTimeout(Constants.HTTP_TIMEOUT_MS);
      connection.setReadTimeout(Constants.HTTP_TIMEOUT_MS);
      connection.addRequestProperty("User-Agent", userAgent);
      connection.addRequestProperty("Accept-Encoding", "gzip");
      connection.connect();

      final int responseCode = connection.getResponseCode();
      if (responseCode == HttpURLConnection.HTTP_OK) {
        final String contentEncoding = connection.getContentEncoding();

        InputStream is = new BufferedInputStream(connection.getInputStream(), 1024);
        if ("gzip".equalsIgnoreCase(contentEncoding)) is = new GZIPInputStream(is);

        reader = new InputStreamReader(is, Constants.UTF_8);
        final StringBuilder content = new StringBuilder();
        final long length = Io.copy(reader, content);

        final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();

        final JSONObject head = new JSONObject(content.toString());
        for (final Iterator<String> i = head.keys(); i.hasNext(); ) {
          final String currencyCode = i.next();
          if (!"timestamp".equals(currencyCode)) {
            final JSONObject o = head.getJSONObject(currencyCode);

            for (final String field : fields) {
              final String rate = o.optString(field, null);

              if (rate != null) {
                try {
                  BigDecimal btcRate = new BigDecimal(GenericUtils.toNanoCoins(rate, 0));
                  BigInteger dogeRate =
                      btcRate.multiply(BigDecimal.valueOf(dogeBtcConversion)).toBigInteger();

                  if (dogeRate.signum() > 0) {
                    rates.put(currencyCode, new ExchangeRate(currencyCode, dogeRate, source));
                    break;
                  }
                } catch (final ArithmeticException x) {
                  log.warn(
                      "problem fetching {} exchange rate from {} ({}): {}",
                      currencyCode,
                      url,
                      contentEncoding,
                      x.getMessage());
                }
              }
            }
          }
        }

        log.info(
            "fetched exchange rates from {} ({}), {} chars, took {} ms",
            url,
            contentEncoding,
            length,
            System.currentTimeMillis() - start);

        return rates;
      } else {
        log.warn("http status {} when fetching {}", responseCode, url);
      }
    } catch (final Exception x) {
      log.warn("problem fetching exchange rates from " + url, x);
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (final IOException x) {
          // swallow
        }
      }

      if (connection != null) connection.disconnect();
    }

    return null;
  }