JDK-4499431 : Request the method kthRoot(double a, long k) for kth root of a.
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.3.0
  • Priority: P5
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2001-09-04
  • Updated: 2003-07-29
  • Resolved: 2003-07-29
Related Reports
Relates :  
Description

Name: boT120536			Date: 09/04/2001


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build Blackdown-1.3.0-RC1)
Java HotSpot(TM) Client VM (build Blackdown-1.3.0-RC1, mixed mode)


I suggest you add the method kthRoot to the class java.lang.Math.
Currently there is a sqrt function for the square root. Another user
has submitted a request for cubeRoot for the cube root. I propose
kthRoot for the kth root.

Of course, people can use pow(a, 1/k) for the same effect. However,
if a is a negative number, this returns NaN, even if k is odd. And it
is not easy to extend the pow function to handle this correctly because
the floating point numbers are only approximations of the fractions.

Here are illustrations of the problem.

I want to compute the cube root of -27.
If I use pow(-27, 1/3) the result is NaN (instead of the desired -3).
I want to compute the fifth root of -32.
If I use pow(-32, 1/5) the result is NaN (instead of the desired -2).
(Review ID: 131190) 
======================================================================

Comments
WORK AROUND Name: boT120536 Date: 09/04/2001 Here is one simple way of implementing your own kthRoot method (I haven't tested this very carefully yet). double kthRoot(double a, long k) { if ((a == -1) && (k % 2 == 0)) { return Double.NaN; } else if ((a == -1) && (k % 2 == 1)) { return -1; } else { return kthRoot(Math.signum(a), k)*pow(Math.abs(a), 1/k); } } ======================================================================
11-06-2004

EVALUATION I don't see much need for this method over the pow workaround; however, will consider this request as part of work on 4633024. ###@###.### 2002-04-24 Closing as will not fix due to lack of large enough utility over a pow-based workaround. ###@###.### 2003-07-29
24-04-2002