C++:求解三次方程

标签 c++ c equation solver cubic

<分区>

site求解三次方程,并有它使用的方程

我写这个函数是为了得到相同的结果,但它不起作用

void cubicFormula(float a, float b, float c, float d, float *results)
{
    float a2 = a*a;
    float b2 = b*b;
    float b3 = b2*b;

    float q = (3*c- b2)/9;
    float q2 = q*q;
    float q3 = q2*q;
    float r = -27*d + b*(9*c-2*b2);
    float r2 = r*r;

    float discriminant = q3 + r2;

    float s = r + sqrtf(discriminant);
    float t = r - sqrtf(discriminant);

    float term1 = powf((-t + s)/2, 1/3.);

    float r13= 2 * sqrtf(q);

    results[0] = (- term1 + r13 * cosf(q3/3));
    results[1] = (- term1 + r13 * cosf(q3 + (2*M_PI)/3));
    results[2] = (- term1 + r13 * cosf(q3 + (4*M_PI)/3));
}

可能是什么问题,是不是我遗漏了什么?

更新:

例如,使用这些值

a = -1.000000;
b = 36.719475;
c = -334.239960;
d = 629.877808;

判别式小于零,所以 r13nan

但是上面提到的网站和我测试过的几个在线求解器找到了三个根

23.775687816485593
2.548516232734247
10.395270950780164

都是实数

最佳答案

您链接的站点提供 Cubic Equation Calculator .您可以从寻找您的代码与他们的代码之间的差异开始:

function cubicsolve(dataForm)
{
    var a = parseFloat(dataForm.aIn.value);
    var b = parseFloat(dataForm.bIn.value);
    var c = parseFloat(dataForm.cIn.value);
    var d = parseFloat(dataForm.dIn.value);
    if (a == 0)
    {
        alert("The coefficient of the cube of x is 0. Please use the utility for a SECOND degree quadratic. No further action taken.");
        return;
    } //End if a == 0

    if (d == 0)
    {
        alert("One root is 0. Now divide through by x and use the utility for a SECOND degree quadratic to solve the resulting equation for the other two roots. No further action taken.");
        return;
    } //End if d == 0
    b /= a;
    c /= a;
    d /= a;
    var disc, q, r, dum1, s, t, term1, r13;
    q = (3.0*c - (b*b))/9.0;
    r = -(27.0*d) + b*(9.0*c - 2.0*(b*b));
    r /= 54.0;
    disc = q*q*q + r*r;
    dataForm.x1Im.value = 0; //The first root is always real.
    term1 = (b/3.0);
    if (disc > 0) { // one root real, two are complex
        s = r + Math.sqrt(disc);
        s = ((s < 0) ? -Math.pow(-s, (1.0/3.0)) : Math.pow(s, (1.0/3.0)));
        t = r - Math.sqrt(disc);
        t = ((t < 0) ? -Math.pow(-t, (1.0/3.0)) : Math.pow(t, (1.0/3.0)));
        dataForm.x1Re.value = -term1 + s + t;
        term1 += (s + t)/2.0;
        dataForm.x3Re.value = dataForm.x2Re.value = -term1;
        term1 = Math.sqrt(3.0)*(-t + s)/2;
        dataForm.x2Im.value = term1;
        dataForm.x3Im.value = -term1;
        return;
    } 
    // End if (disc > 0)
    // The remaining options are all real
    dataForm.x3Im.value = dataForm.x2Im.value = 0;
    if (disc == 0){ // All roots real, at least two are equal.
        r13 = ((r < 0) ? -Math.pow(-r,(1.0/3.0)) : Math.pow(r,(1.0/3.0)));
        dataForm.x1Re.value = -term1 + 2.0*r13;
        dataForm.x3Re.value = dataForm.x2Re.value = -(r13 + term1);
        return;
    } // End if (disc == 0)
    // Only option left is that all roots are real and unequal (to get here, q < 0)
    q = -q;
    dum1 = q*q*q;
    dum1 = Math.acos(r/Math.sqrt(dum1));
    r13 = 2.0*Math.sqrt(q);
    dataForm.x1Re.value = -term1 + r13*Math.cos(dum1/3.0);
    dataForm.x2Re.value = -term1 + r13*Math.cos((dum1 + 2.0*Math.PI)/3.0);
    dataForm.x3Re.value = -term1 + r13*Math.cos((dum1 + 4.0*Math.PI)/3.0);
    return;
}  //End of cubicSolve

关于C++:求解三次方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13328676/

相关文章:

c++ - QwebSocket::sendTextMessage/textMessageReceived 是异步的还是我不应该打扰 QMutex/QEventLoop?

c - 发送分段数据报,每个分段均带有 UDP header

matlab - 在 MATLAB 中使用带有常量参数的内联函数

boolean - 为什么(a 和(不是 b))或(a 和 b)= a?

c++ - 将 char 数组分配给另一个 char 数组

python - 如何在 Python 中得到 10**20 + 10**-20 的精确结果?它给了我 1e+20

c++ - 唯一指针-为什么析构函数被调用3次

c++ - 由于奇怪的 "compiler generated file could not be created"错误,QT5.6.2 构建无法正常工作

c++ - 是否允许显式调用析构函数,然后在具有固定生命周期的变量上放置 new?

c - 如何检查 CLI 定义的 MACRO (-DMACRO=) 是否已定义