Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quadratic equation in Ada

Tags:

math

ada

I just came around and decided to try some Ada. The downside is that the syntax and function strays quite away from C++. So I had to like cram various stuff to get this thing to work.

My question is if there are some better way to do this calculation that what I have done here

   IF(B < 0.0) THEN
      B := ABS(B);
      X1 := (B / 2.0) + Sqrt( (B / 2.0) ** 2.0 + ABS(C));
      X2 := (B / 2.0) - Sqrt( (B / 2.0) ** 2.0 + ABS(C));
   ELSE
      X1 := -(B / 2.0) + Sqrt( (B / 2.0) ** 2.0 - C);
      X2 := -(B / 2.0) - Sqrt( (B / 2.0) ** 2.0 - C);
   END IF;

I had some problem with negative numbers, that's why I did a IF statement and used ABS() to turn those into positive. But the weird thing is that it works perfectly for the other case, which is strange...

like image 280
starcorn Avatar asked Dec 21 '10 20:12

starcorn


People also ask

How can we use quadratic equation in real life situation?

Quadratic equations are used in many real-life situations such as calculating the areas of an enclosed space, the speed of an object, the profit and loss of a product, or curving a piece of equipment for designing.

How are quadratic equations used in solving real life problems and making decisions?

Quadratic equations are actually used in everyday life, as when calculating areas, determining a product's profit or formulating the speed of an object. Quadratic equations refer to equations with at least one squared variable, with the most standard form being ax² + bx + c = 0.

What is quadratic formula used for?

This equation is known as the Quadratic Formula. This formula is very helpful for solving quadratic equations that are difficult or impossible to factor, and using it can be faster than completing the square. The Quadratic Formula can be used to solve any quadratic equation of the form ax2 + bx + c = 0.

Why is quadratic equation important?

So why are quadratic functions important? Quadratic functions hold a unique position in the school curriculum. They are functions whose values can be easily calculated from input values, so they are a slight advance on linear functions and provide a significant move away from attachment to straight lines.


1 Answers

Solving quadratic equations is not as simple as most people think.

The standard formula for solving a x^2 + b x + c = 0 is

delta = b^2 - 4 a c
x1 = (-b + sqrt(delta)) / (2 a)   (*)
x2 = (-b - sqrt(delta)) / (2 a)

but when 4 a c << b^2, computing x1 involves subtracting close numbers, and makes you lose accuracy, so you use the following instead

delta as above
x1 = 2 c / (-b - sqrt(delta))     (**)
x2 = 2 c / (-b + sqrt(delta))

which yields a better x1, but whose x2 has the same problem as x1 had above.

The correct way to compute the roots is therefore

q = -0.5 (b + sign(b) sqrt(delta))

and use x1 = q / a and x2 = c / q, which I find very efficient. If you want to handle the case when delta is negative, or complex coefficients, then you must use complex arithmetic (which is quite tricky to get right too).

Edit: With Ada code:

DELTA := B * B - 4.0 * A * C;

IF(B > 0.0) THEN
    Q := -0.5 * (B + SQRT(DELTA));
ELSE
    Q := -0.5 * (B - SQRT(DELTA));
END IF;

X1 := Q / A;
X2 := C / Q;
like image 181
Alexandre C. Avatar answered Oct 14 '22 11:10

Alexandre C.