0

In Java, I am dividing by zero, which obviously does not produce a number.

public class tempmain {
    public static void main(String args[])
    {
        float a=3/0;
        System.out.println(a);
    }
}

I expect the output to be "NaN", but it shows the following error:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at tempmain.main(tempmain.java:6)

Is there any way (apart from if(denominator==0)) by which NaN can be displayed?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Pranit Bankar
  • 453
  • 1
  • 7
  • 21

3 Answers3

7

You are getting the unexpected ArithmeticException because of the way that your numeric literals 3 and 0 are treated. They are interpreted as integers, despite the fact that you assign a to be a float. Java will interpret 3 as an integer, 3f as a float and 3.0 or 3d as a double. The division is performed as integer division and the result cast to a float. To see another interesting other effect of this - try float a = 1/3 and inspect a - you will find it is 0.0, rather than 0.3333.... as you might expect.

To get rid of the Exception - change

float a = 3/0;

for

float a = 3f/0;

As others have noted in comments and answers, if you then print a, you will see Infinity rather than NaN. If you really want to print NaN in this case, you can test a for being infinite i.e.

if (Float.isInfinite(a)) {
     System.out.println("I want to print Infinity as NaN");
}

but I'd recommend against it as highly misleading - but I suspect that's not what you meant anyway

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
4

First, dividing by zero in floating point numbers does not give NaN, it gives infinity. Second, you're dividing integers, not floats.

float a = 3.0f/0.0f;
System.out.println(a);  // Prints Infinity
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
2

What about:

public class tempmain {

public static void main(String args[])
{
    try {
       float a=3/0;
       System.out.println(a);
    } catch (ArithmeticException x) {
       System.out.println("Not Evaluable");
    }
}

}

There is also this option:

{
...
//if (a != a) {        // these two are equivalent
if (Float.isNaN(a)) {
  System.out.println("NaN");
  }
}

See: Float.NaN == Float.NaN

Community
  • 1
  • 1
HappyCactus
  • 1,935
  • 16
  • 24
  • 1
    You can do that comparison, but it is very inefficient. Better of using `Float.isNaN()` or `Double.isNaN()` – Ascalonian Jan 08 '15 at 15:28
  • 2
    @Ascalonian Why should that be inefficient? this is the code of isNaN: `public static boolean isNaN(float f) { return f != f; }` – Absurd-Mind Jan 08 '15 at 15:39
  • @Absurd-Mind good find. I was just told before not to use it. I guess I should have checked myself to confirm. Thanks for figuring that out! – Ascalonian Jan 08 '15 at 15:42
  • The reason why you should use `isNaN()` instead of `a != a`, is that the former is explicit and can also be understood by someone not aware of the `NaN != NaN` concept. – Absurd-Mind Jan 09 '15 at 09:44
  • I totally agree with you. – HappyCactus Jan 09 '15 at 12:01