为什么除以零导致9.223372E + 18?

时间:2015-03-23 22:09:47

标签: java android double

我正在制作Android计算器,当我除以零时,我得到9.223372E + 18。为什么不显示NaN或崩溃?我认为它导致9.223372E + 18,因为这是最大可能的双值,因为我除以零并使用双数据类型。由于它会让用户感到困惑,我该如何解决这个问题?


回应评论

大家好,感谢您的回复。我很感激。我在下面发布了我的代码。

public class CFM extends ActionBarActivity {
Double cfm, ac, volume;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cfm);

            EditText e1 = (EditText) findViewById(R.id.editText1);
            EditText e2 = (EditText) findViewById(R.id.editText2);
            TextView t1 = (TextView) findViewById(R.id.editText3);
            t1.setEnabled(false);
            t1.setFocusable(false);

            e1.addTextChangedListener(new TextWatcher() {

                public void afterTextChanged(Editable s) {
                }

                public void beforeTextChanged(CharSequence s, int start,
                                              int count, int after) {
                }

                public void onTextChanged(CharSequence s, int start,
                                          int before, int count) {
                    EditText e1 = (EditText)findViewById(R.id.editText1);
                    EditText e2 = (EditText)findViewById(R.id.editText2);
                    volume = Double.parseDouble(e1.getText().toString());
                    cfm = Double.parseDouble(e2.getText().toString());
                    TextView t1 = (TextView) findViewById(R.id.editText3);
                    ac = cfm * 60 / volume;
                    t1.setText(Double.toString((double) Math.round(ac * 100) / 100));
                }
            });


            e2.addTextChangedListener(new TextWatcher() {

                public void afterTextChanged(Editable s) {
                }

                public void beforeTextChanged(CharSequence s, int start,
                                              int count, int after) {
                }

                public void onTextChanged(CharSequence s, int start,
                                          int before, int count) {
                    EditText e1 = (EditText)findViewById(R.id.editText1);
                    EditText e2 = (EditText)findViewById(R.id.editText2);
                    volume = Double.parseDouble(e1.getText().toString());
                    cfm = Double.parseDouble(e2.getText().toString());
                    TextView t1 = (TextView) findViewById(R.id.editText3);
                    ac = cfm * 60 / volume;
                    t1.setText(Double.toString((double) Math.round(ac * 100) / 100));
                }
            });

}

2 个答案:

答案 0 :(得分:5)

9223372036854775807(大约9.223372E+18)是最大可能的long值。我可以看到除以零的唯一方法是,如果你有long类型的变量。例如,以下代码打印9223372036854775807

long a = 1;
a /= 0.0; 
System.out.println(a);

当您将long除以double时,long首先转换为double,结果为double。任何积极的double除以0.0都会得到Double.POSITIVE_INFINITY,因此1/0.0Double.POSITIVE_INFINITY。由于a的类型为long,因此会将其转换回long,从而获得尽可能大的long值。

但是,我们需要查看您的代码,以了解为什么您拥有long类型的变量。

修改

看过你的代码后,我现在意识到问题不是long类型的变量,而是你使用Math.round。其定义如下:

  

返回与参数最接近的长度,并将关系向上舍入。

Math.round因此将Double.PositiveInfinity舍入为9223372036854775807。将结果显示为2位小数的更好方法就是这样。

String result = Double.isInfinite(ac) || Double.isNaN(ac) ? "Error"
            : new BigDecimal(ac).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
t1.setText(result);

如果用户尝试除以零,则会打印"Error"

答案 1 :(得分:0)

计算数值时,使用检测机制查找该数字并显示“不能除以零”的错误。

或者看看是否存在处理除零的异常处理程序。点击此处查看要抛出的异常的示例:

How should I throw a divide by zero exception in Java without actually dividing by zero?

或此处:Java division by zero doesnt throw an ArithmeticException - why?

或者如deyur所提到的那样,是的,您可以随时检查其中一个参数是否为零并以此方式处理。

如果您仍然遇到此问题,请分享核心代码段,以便我们提供帮助。