2

Why are the results of:

double a = 0.0/0.0;
double b = 0/0.0;

= NaN

But the results of for example:

double e = 0.1/0.0;
double e = 12.0/0.0;
double f = 1.0/0.0;

= Infinity

I understand that double or float divisions are somehow a little bit different. I am pretty happy with the resulting NaN because the result is not defined when something is divided by zero. But why they defined that the result of something greater than zero divided by zero is Infitity? Has this something todo with the method they use to perform the division of floating points?

Claudio P
  • 2,133
  • 3
  • 25
  • 45
  • 5
    This is behaving exactly as per JLS 15.17.2: "Division of a zero by a zero results in NaN... Division of a nonzero finite value by a zero results in a signed infinity." What would you propose the results be? This is pretty common - possibly as required by IEEE754 even. – Jon Skeet May 15 '17 at 15:45
  • all this math expressions 0/0, ∞/∞, 0 × ∞, ∞ − ∞, 1∞ and ∞0. produce an Indeterminate form – ΦXocę 웃 Пepeúpa ツ May 15 '17 at 15:45
  • @JonSkeet I excpected the result to be the same (both NaN) and not dependant on the numerator of the division. – Claudio P May 15 '17 at 15:50
  • 3
    @ClaudioP: Then basically your expectations were inappropriate, given that it's well-specified what it will do. You might want to read http://stackoverflow.com/questions/14682005 as well. – Jon Skeet May 15 '17 at 15:56

3 Answers3

3

JLS 15.17.2 clearly states that

Division of a zero by a zero results in NaN; division of zero by any other finite value results in a signed zero. The sign is determined by the rule stated above.

Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated above.

The JLS also says about the difference between these constants in operations.

If either operand is NaN, the result is NaN.

Division of an infinity by an infinity results in NaN.

Division of an infinity by a finite value results in a signed infinity. The sign is determined by the rule stated above.

Not inconsistent at all.

Community
  • 1
  • 1
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
  • 1
    Explaining the bit representations of NaN and Infinity is not helpful. The asker seems to be clear on the *concepts* of NaN and Infinity, just not the reasoning for the outcomes. – VGR May 15 '17 at 15:54
  • 2
    Thanks for your effort. I would be happy if someone could state me the reason for this definition. Why they don't use NaN for both? – Claudio P May 15 '17 at 15:54
2

According to math definition following operations result in an undetermined result and that is what NAN represent! Following undetermined result can be:

  • 0/0,
  • ∞/∞,
  • 0*∞,
  • ∞-∞,
  • 0^0,
  • 1^∞,
  • 0^∞

java match almost all of them (except ∞^0 and 0^0)

    // 0/0
    double a = 0.0 / 0.0;
    System.out.println(a);
    // ∞/∞
    a = Double.POSITIVE_INFINITY / Double.POSITIVE_INFINITY;
    System.out.println(a);
    // 0*∞
    a = 0 * Double.POSITIVE_INFINITY;
    System.out.println(a);
    // ∞-∞
    a = Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY;
    System.out.println(a);
    // 0^0
    a = Math.pow(0, 0);
    System.out.println(a);
    // 1^∞
    a = Math.pow(1, Double.POSITIVE_INFINITY);
    System.out.println(a);
    // ∞^0
    a = Math.pow(Double.POSITIVE_INFINITY, 0);
    System.out.println(a);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
2

There are other answers that specify that this is in the standard and therefore "expected" behavior. You seem to want to know why the standard is like that. I would guess that the reason is that some expressions have well-defined limits (as in calculus) and some do not even have a limit. The ones with a well-defined limit get a signed infinity (since that is the limit). The ones like 0/0 get NaN because there is no limit there. (The limit from the left is negative infinity and the limit from the right is positive infinity.) In all cases, when I say "limit", I mean limit of n/x as x -> 0, where n is the fixed numerator.

Brick
  • 3,998
  • 8
  • 27
  • 47