Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ using E in a mathematical equation

Tags:

c++

math

I'm trying to solve aX2 + bX + c = 0 but I can't seem to make it work with using the math header (which I'm not supposed to use).

printf("%E",(-b+(b*b-4*a*c)E0.5)/2a);
like image 968
user779444 Avatar asked Apr 11 '26 03:04

user779444


1 Answers

Use std::sqrt from header <cmath>. Also, you must write (2 * a), not 2a.

Another thing: don't use the textbook formula for solving quadratic equations. Use the method described there.

If you can't use the math header, then you have to implement the square root eg. as described there:

double my_abs(double x)
{
    return x > 0 ? x : -x;
}

double my_sqrt(double x)
{
    static const double eps = 1e-12;
    double u = x, uold;

    do { uold = u; u = (u * u + x) / (2 * u); }
    while (my_abs(u - uold) < eps * x);

    return u;
}
like image 124
Alexandre C. Avatar answered Apr 12 '26 16:04

Alexandre C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!