Four Loko Breakfast
Four Loko Breakfast

Reputation: 29

Why does dividing by zero result in 9.223372E+18?

I'm making an Android calculator and I get 9.223372E+18 when I divide by zero. why wouldn't it show NaN or crash? I think that it results in 9.223372E+18 because that's the largest possible double value, since I'm dividing by zero and using a double data type. Since it will confuse the user, how do I get around this?


Respond to Comments

Hi guys, thanks for all your responses. I appreciate it. I posted my code below.

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

}

Upvotes: 1

Views: 1859

Answers (2)

Paul Boddington
Paul Boddington

Reputation: 37645

9223372036854775807 (roughly 9.223372E+18) is the largest possible long value. The only way I can see this coming from dividing by zero is if you have a variable of type long. For example, the following code prints 9223372036854775807.

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

When you divide a long by a double, the long is converted to a double first and the result is a double. Any positive double divided by 0.0 gives Double.POSITIVE_INFINITY, so 1/0.0 is Double.POSITIVE_INFINITY. Since a has type long, this is converted back to a long, giving the largest possible long value.

However, we would need to see your code to understand why you have variables of type long.

Edit

Having seen your code, I now realise that the problem is not a variable of type long but your use of Math.round. This is defined as follows:

Returns the closest long to the argument, with ties rounding up.

Math.round therefore rounds Double.PositiveInfinity down to 9223372036854775807. A better approach to displaying the result to 2 decimal places would be something like this.

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

This will print "Error" if the user tries to divide by zero.

Upvotes: 5

MikeDub
MikeDub

Reputation: 5283

When calculating your value, use a detection mechanism to look for that number and show an error of 'cannot divide by zero'.

Either that or see if there is an exception handler that handles division by zero. Check here for an example of what exception to throw:

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

or here: Java division by zero doesnt throw an ArithmeticException - why?

OR as deyur mentioned, yes you can always check if one of the arguments is zero and handle it that way.

If you are still having issues beyond this, share the core code snippets so we can help.

Upvotes: 0

Related Questions