<commit_before>package calculator;

import java.util.Stack;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author <NAME>
 */
public class Calculator
{
    private static final Map<Character, Integer> precedence = new HashMap<>(6);

    // operators -> precedence
    static
    {
        precedence.put('+', 0);
        precedence.put('-', 0);
        precedence.put('*', 1);
        precedence.put('/', 1);
        precedence.put('%', 1);
        precedence.put('^', 2);
    }

    // holds tokens from expression
    private static final List<String> tokens = new ArrayList<>();

    // precision of result
    private static final int precision = 10;


    public static String calculate(String expression) throws SyntaxException
    {
        if (expression.isEmpty())
        {
            return expression;
        }

        Stack<BigDecimal> stk = new Stack<>();

        convert(expression);

        MathContext context = new MathContext(precision);
        BigDecimal op1, op2;

        for (String token : tokens)
        {
            switch (token)
            {
                case "+":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.add(op2));
                    break;

                case "-":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.subtract(op2));
                    break;

                case "*":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.multiply(op2));
                    break;

                case "/":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.divide(op2));
                    break;

                case "%":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.remainder(op2));
                    break;

                case "^":
                    double d2 = stk.pop().doubleValue();
                    double d1 = stk.pop().doubleValue();

                    stk.push(new BigDecimal(Math.pow(d1, d2)));
                    break;

                // is number
                default:
                    stk.push(new BigDecimal(token));
                    break;
            }
        }
<commit_msg>Handled ArithmeticException<commit_after>package calculator;

import java.util.Stack;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author <NAME>
 */
public class Calculator
{
    private static final Map<Character, Integer> precedence = new HashMap<>(6);

    // operators -> precedence
    static
    {
        precedence.put('+', 0);
        precedence.put('-', 0);
        precedence.put('*', 1);
        precedence.put('/', 1);
        precedence.put('%', 1);
        precedence.put('^', 2);
    }

    // holds tokens from expression
    private static final List<String> tokens = new ArrayList<>();

    // precision of result
    private static final int precision = 10;


    public static String calculate(String expression) throws SyntaxException
    {
        if (expression.isEmpty())
        {
            return expression;
        }

        Stack<BigDecimal> stk = new Stack<>();

        convert(expression);

        MathContext context = new MathContext(precision);
        BigDecimal op1, op2;

        for (String token : tokens)
        {
            switch (token)
            {
                case "+":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.add(op2));
                    break;

                case "-":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.subtract(op2));
                    break;

                case "*":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.multiply(op2));
                    break;

                case "/":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.divide(op2, precision, RoundingMode.HALF_UP));
                    break;

                case "%":
                    op2 = stk.pop();
                    op1 = stk.pop();
                    stk.push(op1.remainder(op2));
                    break;

                case "^":
                    double d2 = stk.pop().doubleValue();
                    double d1 = stk.pop().doubleValue();

                    stk.push(new BigDecimal(Math.pow(d1, d2)));
                    break;

                // is number
                default:
                    stk.push(new BigDecimal(token));
                    break;
            }
        }

{'fraction_non_alphanumeric': 0.12039408866995074, 'fraction_numerical': 0.013793103448275862, 'mean_word_length': 1.2136938508504143, 'pattern_counts': {'":': 12, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4926436', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1319, 'n_words': 297}