Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:10:12+00:00 2026-06-14T06:10:12+00:00

This site solves cubic equations, and has the equations it uses I wrote this

  • 0

This site solves cubic equations, and has the equations it uses

I wrote this function to get the same results but it’s not working

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));
}

What could be the problem, is there something I am missing?

Update:

For instance, using these values

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

The discriminant is less then zero, so r13 is nan

But the site mentioned above and a couple of online solvers I’ve tested find the three roots

23.775687816485593
2.548516232734247
10.395270950780164

All of them real numbers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:10:14+00:00Added an answer on June 14, 2026 at 6:10 am

    The site you link to provide a Cubic Equation Calculator. You could start by looking for differences between your code and theirs:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on this site for the past two weeks and everything has
This site has a technique to pass xml data around in Microsoft SQL Server:
On this site that I'm working on for work - http://www.basicliving.com - when you
I have a new MVC 2 site that has been working fine on my
Ok, I googled and found some answers here on this site regarding this, but
I am trying to get an HTML page source content from this site: http://207.200.96.231:8008
I am updating an existing ASP .NET site. This site has a custom grid
I have been working on this site, http://hidden.xx The problem is that the <body>
Hey Everyone. I'm a first time poster, but I've browsed this site a number
A site works fine with Chrome, FireFox, IE8 but does not work with IE7

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.