简体   繁体   English

两个长值之和返回负值

[英]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?为什么我得到这个负数,我如何得到正确的 output 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:我的输入和 output:

   Input: 9000000000000000123 9000000000000000987 +
   Expected Output: 18000000000000000000
   Current Output: -446744073709550506

You got negative value because you exceeded the maximum value a long can hold.你得到了负值,因为你超过了一个long可以持有的最大值。 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.当您尝试将小于最小值的数字分配给long变量时,也是如此。 To understand this, you can look at the output of the following program:要理解这一点,可以看下面程序的output:

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: 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 eg根据您的要求,您需要BigInteger例如

BigInteger bi = new BigInteger("18000000000000000000");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM