-1

I have written code to complete a postfix evaluation except when I am trying to handle the case below, I continue to get a negative value which I know is incorrect. Why am I getting this negative number and how do I get the correct output of 18000000000000000000? I have posted my code below and any help would be greatly appreciated.

     public static Number postfixEvaluate(String e){
    Long number1;
    Long number2;
    Number result = new Long(0);

    Stack<Number> stack = new Stack();

    String[] tokens = e.split(" ");

        for(int j = 0; j < tokens.length; j++){
            String token = tokens[j];
            //System.out.println(tokens[j]);
        if (!"+".equals(token) && !"*".equals(token) && !"-".equals(token) && !"/".equals(token) && !"".equals(token))  {
            stack.push(Long.parseLong(token)); 

    }  else if ("".equals(token)) {
        System.out.println(token);
    } else {
            String Operator = tokens[j];
            number2 = (Long) stack.pop();
            System.out.println(number2);
            number1 = (Long) stack.pop();
            System.out.println(number1);
            if (Operator.equals("/")){
                result = number1 / number2;
                System.out.println(result);
            }
            else if(Operator.equals("*")){
                result = number1 * number2;
                System.out.println(result);
            }
            else if(Operator.equals("+")){
                result = Long.sum(number1, number2);
                System.out.println("Addition of: " + number1 + "+ " + number2 + "= " + result);
            }
            else if(Operator.equals("-")){
                result = number1 - number2;
            System.out.println(result);
            }
            else System.out.println("Illeagal symbol");
            stack.push(result);
        }
        }

                stack.pop();


    //s.pop();
    System.out.println("Postfix Evauation = " + result);

        return result;
   }

My input and output:

   Input: 9000000000000000123 9000000000000000987 +
   Expected Output: 18000000000000000000
   Current Output: -446744073709550506
  • 2
    max long in java is `9,223,372,036,854,775,808`, your sum is bigger, so you're getting an overflow. Use `BigInteger` if you need arbitrarily large numbers. Basically the same problem as [this question](https://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo) but with long instead of int. – azurefrog Oct 28 '19 at 18:45
  • Also, I'm not sure why you say you're expecting to get an output of 18000000000000000000. 9000000000000000123 + 9000000000000000987 = 18000000000000001110 according to my math. – azurefrog Oct 28 '19 at 18:51

1 Answers1

1

You got negative value because you exceeded the maximum value a long can hold. When you exceed the maximum value, it starts again from its minimum value up to the number exceeding the maximum value. The same is also true when you try to assign a number smaller than the minimum value to a long variable. To understand this, you can look at the output of the following program:

public class Main {
    public static void main(String[] args) {
        System.out.println("Long.MAX_VALUE: "+Long.MAX_VALUE);
        System.out.println("Long.MIN_VALUE: "+Long.MIN_VALUE);
        long x = Long.MAX_VALUE + 1;
        long y = Long.MIN_VALUE - 1;
        System.out.println("Long.MAX_VALUE + 1: "+x);//will be assigned the value of Long.MIN_VALUE
        System.out.println("Long.MIN_VALUE - 1: "+y);//will be assigned the value of Long.MAX_VALUE

        x = Long.MAX_VALUE + 2;
        y = Long.MIN_VALUE - 2;
        System.out.println("Long.MAX_VALUE + 2: "+x);//will be assigned the value of Long.MIN_VALUE + 1
        System.out.println("Long.MIN_VALUE - 2: "+y);//will be assigned the value of Long.MAX_VALUE - 1

    }
}

Output:

Long.MAX_VALUE: 9223372036854775807
Long.MIN_VALUE: -9223372036854775808
Long.MAX_VALUE + 1: -9223372036854775808
Long.MIN_VALUE - 1: 9223372036854775807
Long.MAX_VALUE + 2: -9223372036854775807
Long.MIN_VALUE - 2: 9223372036854775806

For your requirement, you need BigInteger e.g.

BigInteger bi = new BigInteger("18000000000000000000");
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110