Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

java - Sum of two long values returns negative value

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...