5

Math.pow(0.0, 0.0) in Java returns 1 which is wrong. 0^0 is undefined. The same problem exists also in the windows calculator (I am using Win7). Why is that?

Mathematica declares it as an error as well as my Casio scientific calculator, why not java or the Win calculator... Is it a bug?

Paul
  • 26,170
  • 12
  • 85
  • 119

5 Answers5

8

0^0 = 1 is considered a reasonable definition in many contexts. For a list of arguments for and against it, see http://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_power_of_zero

5

Because that's exactly what the Javadocs say it will do:

public static double pow(double a, double b)

Returns the value of the first argument raised to the power of the second argument. Special cases:
* If the second argument is positive or negative zero, then the result is 1.0.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
4

Is it a bug?

No, a bug is something that violates the specification. The specification states:

Returns the value of the first argument raised to the power of the second argument. Special cases:

  • If the second argument is positive or negative zero, then the result is 1.0.

Finally, mathematically, some do define 0^0 as 1. In fact, Knuth says that it has to be 1.

The number of mappings from the empty set to the empty set is 0^0. It has to be 1.

His reasoning is as follows. If you have two sets, A and B, the number of functions from A to B is |B|^|A|. How many functions are there from the empty set to the empty set? Well, there is exactly one. By this logic, 0^0 should be 1.

Community
  • 1
  • 1
jason
  • 236,483
  • 35
  • 423
  • 525
2

Java defines it that way. That's all you can really say.

However, mathematically it is an undefined quantity. One way to see why is to write

x = 0 ^ 0

where I've used ^ to represent exponentiation. Taking logarithms,

log x = 0 log 0

I've done this since every mathematician accepts that log 0 is undefined and so it follows that log x and therefore x are undefined too. (Mathematically it's called a singularity and a mathematician will tell you that it's one of the worst singularities you can encounter).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 2
    I'm a mathematician and it's not one of the worst singularities that you can encounter, just a limit :) – Benjamin Gruenbaum Jun 22 '13 at 19:44
  • Whether `log 0` is undefined depends on the context. If it's about logarithms of non-negative real numbers, `log 0 = -infinity` is the commonly accepted value. – Daniel Fischer Jun 22 '13 at 19:45
  • @Daniel Fischer, but what type of infinity? That's when things get really interesting. – Bathsheba Jun 22 '13 at 19:48
  • The type you get from the two-point compactification of the real line. – Daniel Fischer Jun 22 '13 at 19:49
  • The worst singularity that you can encounter is a black hole. – jason Jun 22 '13 at 19:49
  • @Jason I thought you couldn't encounter that, you'd be history before. – Daniel Fischer Jun 22 '13 at 19:51
  • @Jason, I'm not sure about that; I'd like to witness the end of the universe. Bring on that black hole... – Bathsheba Jun 22 '13 at 19:53
  • Yes, I know I am a mathematician myself. But why not returning lets say NaN such as when divide zero with 0 (0.0/0.0). It would be better and would not lead to false calculation when it is used – user2512326 Jun 22 '13 at 20:42
  • @user2512326: The documentation is unambiguous. It says exactly what will happen if you invoke `Math.pow(0.0, 0.0)`. You say that you don't like that. If it hurts when you do that then do not do that! If you need the result of `Math.pow(0.0, 0.0)` to be `Double.NaN`, then you need to test for these inputs before invoking `Math.pow` and use `Double.NaN` instead of blindly passing those parameters along to `Math.pow` and not getting the result that you want. Mathematicians do not universally agree on `0^0`, saying it's `1` is acceptable, so Java's choice is fine. – jason Jun 23 '13 at 06:56
1

The exact definition of the function's behavior is given at http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#pow(double,%20double)

You will see here that

public static double pow(double a,
         double b)
Returns the value of the first argument raised to the power of the second argument. Special cases:
If the second argument is positive or negative zero, then the result is 1.0.

Not a bug - it's by design.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • But why not returning lets say NaN such as when divide zero with 0 (0.0/0.0). It would be better and would not lead to false calculation when it is used. Mathematica returns Indeterminate which is also true... – user2512326 Jun 22 '13 at 20:48
  • Ask the people who made this design decision. If you say "anything to the power zero is one" there is no problem... – Floris Jun 22 '13 at 20:50
  • Thanks a lot I've got your point. That's why I am asking this question, it's about this incompatible design decision. It would be also strange and incompatible if 0.0/0.0 returns 1. Thanks anyway, I appreciate your answer and the time you have spent! – user2512326 Jun 22 '13 at 21:21