finding cube root in C++?

后端 未结 12 540
自闭症患者
自闭症患者 2020-12-03 15:18

Strange things happen when i try to find the cube root of a number.

The following code returns me undefined. In cmd : -1.#IND

cout<

        
相关标签:
12条回答
  • 2020-12-03 15:31

    because the 1/3 will always return 0 as it will be considered as integer... try with 1.0/3.0... it is what i think but try and implement... and do not forget to declare variables containing 1.0 and 3.0 as double...

    0 讨论(0)
  • 2020-12-03 15:33

    The power 1/3 is a special case. In general, non-integral powers of negative numbers are complex. It wouldn't be practical for pow to check for special cases like integer roots, and besides, 1/3 as a double is not exactly 1/3!

    I don't know about the visual C++ pow, but my man page says under errors:

    EDOM The argument x is negative and y is not an integral value. This would result in a complex number.

    You'll have to use a more specialized cube root function if you want cube roots of negative numbers - or cut corners and take absolute value, then take cube root, then multiply the sign back on.

    Note that depending on context, a negative number x to the 1/3 power is not necessarily the negative cube root you're expecting. It could just as easily be the first complex root, x^(1/3) * e^(pi*i/3). This is the convention mathematica uses; it's also reasonable to just say it's undefined.

    0 讨论(0)
  • 2020-12-03 15:36

    While (-1)^3 = -1, you can't simply take a rational power of a negative number and expect a real response. This is because there are other solutions to this rational exponent that are imaginary in nature.
    http://www.wolframalpha.com/input/?i=x^(1/3),+x+from+-5+to+0

    Similarily, plot x^x. For x = -1/3, this should have a solution. However, this function is deemed undefined in R for x < 0.

    Therefore, don't expect math.h to do magic that would make it inefficient, just change the signs yourself.

    0 讨论(0)
  • 2020-12-03 15:39

    pow( x, y ) is the same as (i.e. equivalent to) exp( y * log( x ) )

    if log(x) is invalid then pow(x,y) is also.

    Similarly you cannot perform 0 to the power of anything, although mathematically it should be 0.

    0 讨论(0)
  • 2020-12-03 15:39

    Here's a little function I knocked up.

    #define uniform() (rand()/(1.0 + RAND_MAX))
    
    double CBRT(double Z)
    {
        double guess = Z;
        double x, dx;
        int loopbreaker;
    
    retry:
        x = guess * guess * guess;
        loopbreaker = 0;
        while (fabs(x - Z) > FLT_EPSILON)
        {
            dx = 3 * guess*guess;
            loopbreaker++;
            if (fabs(dx) < DBL_EPSILON || loopbreaker > 53)
            {
                guess += uniform() * 2 - 1.0;
                goto retry;
            }
            guess -= (x - Z) / dx;
            x = guess*guess*guess;
        }
    
        return guess;
    }
    

    It uses Newton-Raphson to find a cube root.

    Sometime Newton -Raphson gets stuck, if the root is very close to 0 then the derivative can get large and it can oscillate. So I've clamped and forced it to restart if that happens. If you need more accuracy you can change the FLT_EPSILONs.

    0 讨论(0)
  • 2020-12-03 15:40

    I was looking for cubit root and found this thread and it occurs to me that the following code might work:

    #include <cmath>
    using namespace std;
    
    function double nth-root(double x, double n){
        if (!(n%2) || x<0){
            throw FAILEXCEPTION(); // even root from negative is fail
        }
    
        bool sign = (x >= 0);
    
        x = exp(log(abs(x))/n);
    
        return sign ? x : -x;
    }
    
    0 讨论(0)
提交回复
热议问题