• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is Math.pow(0,0) = 1?

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems to be a blatant error. I would think ArithmeticException should be thrown or some other error since 0 to the 0 power is undefined.
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who said 00 is undefined? Back to basics....
0n = 1
taking the square root of both sides yields 1 on the right, since the square root of 1 is 1, therefore, n=1. The function is correct.
SAF
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saf- err, you might want to reconsider those basics again. 0n = 0, not 1. At least for all n > 0. For all n < 0, it's infinite, as is 1/0. And consider n0 = 1 - at least, for all n other than 0. The problem is that there are several possible "obvious" values for 00, and they conflict. Thus, it's reasonable to call the value undefined.
Jim- in general, I'd agree - it would make more sense to throw an exception here. But the design philosophy of Java floating -point arithmetic seems to be to avoid throwing exceptions whenever possible - e.g. dividing by zero returns Double.POSITIVE_INFINITY (even though the sign should really be unspecified here). My guess is that whoever wrote the function considered 1 to be the most-likely-expected value for most users - perhaps there is some IEEE standard, or a standard C library that leads people to expect this behavior? Whatever - at least the behavior is documented in the API. I guess it's up to users to make their own decisions about what behavior is appropriate for their own applications, and cover the special cases with hand-coded if-then statements if they don't like the library bahavior.

[This message has been edited by Jim Yingst (edited July 06, 2001).]
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok you got me. That's strange....
thanks,
SAF
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, thanks Jim. SAFROLE, try this one: log(0^0) = 0*log(0) = 0*(-oo) ...
[This message has been edited by Jim Baiter (edited July 09, 2001).]
 
Author and "Sun God"
Posts: 185
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is actually a fairly deep question. While the value of 00 is undefined, there are both practical and theoretical reasons why it makes sense to return 1. Practically speaking, it makes a whole bunch of formulas work; you have to code around it in languages that don't return 1. Many languages (e.g. Common Lisp, Java, ANSI Smalltalk) thus specify that 1 must be returned under these circumstances.
Guy Steele sent me letter describing the technical reasons in 1996 when I designed java.math, but I fear I've lost track of it.

------------------
Joshua Bloch
Author of Effective Java
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the zero power of any number is 1.


n0 = 1


 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A good discussion on this topic can be found here( Maha anna's discussion page)
http://www.javaranch.com/maha/Discussions/java_lang_Package/Can_anyboby_confirm_the_result_of_this_Math_fn__-_JavaRanch_Big_Moose_Saloon.htm
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela- yes, for any n > 0. Read my last post again; I mentioned this.
Rex- thanks; I thought this whole discussion sounded familiar.
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, funny I'd never really run into that one before - thanks. For anyone else who is into the math side, I found a VAX man page via a websearch that explains why they define pow(0,0) to be 1.
"The function pow(x, 0) returns x**0 = 1 for all x including x = 0, Infinity (not found on a VAX), and NaN (the reserved operand on a VAX). Previous implementations of pow may have defined x**0 to be undefined in some or all of these cases. Here are reasons for returning x**0 = 1 always:

1.
Any program that already tests whether x is zero (or infinite or NaN) before computing x**0 cannot care whether 0**0 = 1 or not. Any program that depends upon 0**0 to be invalid is dubious anyway since that expression's meaning and, if invalid, its consequences vary from one computer system to another.
2.
Some Algebra texts (e.g. Sigler's) define x**0 = 1 for all x, including x = 0. This is compatible with the convention that accepts a[0] as the value of polynomial
p(x) = a[0]*x**0 + a[1]*x**1 + a[2]*x**2 +...+ a[n]*x**n
at x = 0 rather than reject a[0]*0**0 as invalid.

3.
Analysts will accept 0**0 = 1 despite that x**y can approach anything or nothing as x and y approach 0 independently. The reason for setting 0**0 = 1 anyway is this:
If x(z) and y(z) are any functions analytic (expandable in power series) in z around z = 0, and if there x(0) = y(0) = 0, then x(z)**y(z) -> 1 as z -> 0.
4.
If 0**0 = 1, then infinity**0 = 1/0**0 = 1 too; and then NaN**0 = 1 too because x**0 = 1 for all finite and infinite x, i.e., independently of x."
[This message has been edited by Jim Baiter (edited July 12, 2001).]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting - good job finding the link, Jim. (For the young'uns in the audience I'll point out that "**" is notation for "to the power of" - in several dead languages. ) I'm not too sure about point 3 though. Let x(z) = 0 and y(z) = z. Nice simple analytic functions which satisfy the stated conditions. So x(z)**y(z) = 0**z. Are they saying that the limit of this function as z->0 is 1? It sure doesn't look like that coming in from values greater than z - rather, it looks like zero to me. What am I missing?
Anyway, my trusty HP-15C tells me 0**0 is an error, and it would never lie to me.

[This message has been edited by Jim Yingst (edited July 12, 2001).]
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, good point. I'll have to dig around Ahlfors or some of my other textbooks to see if that theorem really states that x(z) is a non-zero analyic function.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to the club so replying late but all I gotta say is Thanks...Thanks a ton. This thing was puzzeling me since long and now atleast i know some reason to it.
Thanks again.
------------------
Amit Agrawal,
New Delhi, India.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An excellent discussion guys and gals. Is there any provision for very important threads such as this to be edited/entered into a site FAQ
A
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic