Double automatic rounding - c

I'm developing a program that calculates the roots of a cubic equation with Tartaglia's method. The problem is when I print the value of the imaginary part, the program rounds the value of 1.54686889 to 1.50.
I have tried to use float instead of double, but, without success.
I'm using Code::Blocks 17.12
My code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <locale.h>
#define PI 3.14159265359
int main(void)
{
setlocale(LC_ALL,"");
double a, b, c, d, p, q, r, t, u, u3, v, v3, A, B, C, D, M, x1, x2, x2i, x3, x3i;
printf("Insira os coeficientes (a b c d): ");
scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
//Verifica se o coeficiente a é válido
if(a != 0){
printf("\nForma geral:\n");
printf("\n (%.2lf)x³ + (%.2lf)x² + (%.2lf)x + (%.2lf) = 0 \n", a, b, c, d);
A = b/a;
B = c/a;
C = d/a;
p = B - (pow(A, 2.0)/3.0);
q = C - ((A*B)/3.0) + (2.0*pow(A, 3.0))/27.0;
D = (pow(q, 2.0)/4.0) + (pow(p, 3.0)/27.0);
if(D < 0){
M = pow(-1.0*D, 1.0/2.0);
r = sqrt(pow(q, 2.0)/4.0 + pow(M, 2.0));
t = acos(-1.0*q/2.0/r);
x1 = 2.0 * pow(r, 1.0/3.0) * cos(t/3.0) - A/3.0;
x2 = 2.0 * pow(r, 1.0/3.0) * cos((t + 2.0 * PI)/3.0) - A/3.0;
x3 = 2.0 * pow(r, 1.0/3.0) * cos((t + 4.0 * PI)/3.0) - A/3.0;
printf("\nAs raízes são: <%lf> <%lf> <%lf>\n", x1, x2, x3);
}
else{
u3 = (-1.0*q/2.0) + pow(D, 1.0/2.0);
if(u3 < 0)
u = -1.0 * pow(-1.0*u3, 1.0/3.0);
else
u = pow(u3, 1.0/3.0);
v3 = (-1.0*q/2.0) - pow(D, 1.0/2.0);
if(v3 < 0)
v = -1.0 * pow(-1.0*v3, 1.0/3.0);
else
v = pow(v3, 1.0/3.0);
x1 = u + v - A/3.0;
D = pow((A + x1), 2.0) + (4.0*C/x1);
x2 = -1.0 * (A + x1)/2.0;
x2i = pow(abs(D), 1.0/2.0)/2.0;
x3 = x2;
x3i = x2i;
if(D < 0){
printf("\nAs raízes são: <%lf>, <%lf + %lfi>, <%lf - %lfi>\n", x1, x2, x2i, x3, x3i);
}
else{
x2 = x2 + x2i;
x3 = x3 - x3i;
printf("\nAs raízes são: <%lf>, <%lf>, <%lf>\n", x1, x2, x3);
}
}
}
else{
printf("O coeficiente <a> não pode ser igual a 0!\n");
}
printf("Fonte de pesquisa: http://www.matematicando.net.br/metodo-de-tartaglia-para-obter-raizes-de-equacao-do-3o-grau/\n");
system("pause");
return 0;
}
The console after printing:

Change the use of abs to fabs and the result is:
As raízes são: <-1.650629>, <-0.174685 + 1.546869i>, <-0.174685 - 1.546869i>
The function abs returns an integer, whereas fabs works on doubles.

Related

Read the coefficients a,b,c of the quadratic equation ax^2+bx+c and print it roots nicely for imaginary roots print in x+iy form

#include <math.h>
#include <stdio.h>
main() {
int a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
scanf("%d%d%d", &a, &b, &c);
printf("The quadratic equation is %d*pow(x,2)+%d*x+%d=0", a, b, c);
if (pow(b, 2) - 4 * a * c >= 0) {
x1 = (-b + sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
x2 = (-b - sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
printf("the roots of the equation are x1=%d,x2=%d", x1, x2);
}
else
printf("roots of the equation in the form of x+iy and x-iy");
return 0;
}
Is this code alright for the given question, i had a bit confusion at that printing imaginary roots. could you please help
Use proper main prototype.
Use floating point numbers instead of integers
pow(b,2) == b*b
/ 2 * a -> / (2 * a)
int main(void) {
double a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
if(scanf("%lf %lf %lf", &a, &b, &c) != 3) { /* handle error */}
printf("\nThe quadratic equation is %f*x^2+%f*x+%f=0\n", a, b, c);
if (b*b - 4 * a * c >= 0) {
x1 = (-b + sqrt(b*b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b*b - 4 * a * c)) / (2 * a);
printf("the roots of the equation are x1=%f,x2=%f\n", x1, x2);
}
else
{
double r = -b / (2*a);
double z = sqrt(fabs(b*b - 4 * a * c));
printf("the roots of the equation are x1 = %f + i%f, x2 = %f - i%f", r,z,r,z);
}
}
https://gcc.godbolt.org/z/Ys1s8bWY7

How to print the imaginary root of quadratic equation in c?

I need to print the imaginary root in the quadratic equation.
but when I execute my code the result shows me that the imaginary root is 0.00000i.
even I use to <complex.h> also same.
Can everybody help me to check the code that I bold?
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + sqrt( disc ) ) / 2 * a;
x2 = ( -b - sqrt( disc ) ) / 2 * a;
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}
The output are shown as below:
Please enter the value of quadratic equation, a: 4
Please enter the value of quadratic equation, b: 1
Please enter the value of quadratic equation, c: 3
xr = -0.125000, ximg1 = 0.000000i, ximg2 = 0.000000i
Process returned 0 (0x0) execution time : 3.914 s
Press any key to continue.
You should print the roots as complex numbers using the computed real and imaginary parts. No need for <complex.h> nor complex types:
double xr = - b / ( 2 * a );
double ximg1 = -sqrt( -disc ) / (2 * a);
double ximg2 = -ximg1;
printf("x1 = %lf%+lfi, x2 = %lf%+lfi\n", xr, ximg1, xr, ximg2);
As you have used complex header file you just need to use the csqrt() instead of sqrt(),
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + csqrt( disc ) ) / 2 * a;//changed the function here
x2 = ( -b - csqrt( disc ) ) / 2 * a;//changed the function here
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}
Complex types not needed
When code reaches the below, disc < 0. Find the square root of the negation.
// ximg1 = sqrt( disc ) / 2 * a;
ximg1 = sqrt( -disc ) / 2 * a; // Use -disc
// ximg2 = - sqrt( disc ) / 2 * a;
ximg2 = -ximg1; // Simply negate ximg1
xr = - b / ( 2 * a );
printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi",
// crealf(xr), cimagf(ximg1), cimagf(ximg2));
xr, ximg1, ximg2);
Tip: use "%e", it is more informative.
printf("xr = %le, ximg1 = %lei, ximg2 = %lei",
xr, ximg1, ximg2);
Bug
Instead of / 2 * a;, in various places, certainly you want / (2 * a);.

Finding Third Coordinate Of a Triangle

i have an assignment for Programming class.
i have finished 80 percent of it, i am not able to complete the last part.
We have to find the distance between the shore and the boat(Point C) And Coordinates Of The Boat (Point C).
We are given (X,Y) coordinates And Angles of Points A and B.
Given A(76,316) B(57,516) Angle A = 17° and B = 17° Angle.
So Far i found AB= 200.9 AC= 105.04 BC = 105.04 C = 146° And the distance between the shore and the boat d = 30.71.
I tried everything but i am not able to find the Coordinates of Point C (Boat).
The Output is also Given : d = 30.71, Coordinates of C = C(97.07,418.90)
Please help me to find coordinates of Point C.
#include <stdio.h>
#include <math.h>
float radian(int degree);
float get_watchtowers_distance(int x1,int y1,int x2,int y2);
float get_boat_distance(float d,int alpha,int beta);
int main(){
get_watchtowers_distance(76,316,57,516);
get_boat_distance(200.90,17,17);
}
float radian(int degree){
return degree * (M_PI/180);
}
float get_watchtowers_distance(int x1,int y1,int x2,int y2){
return sqrtf(powf(x2-x1,2.0f)+powf(y2-y1,2.0f));
}
float get_boat_distance(float d,int alpha,int beta){
float a,b,c = 180 - (float)alpha - (float)beta;
a = (float)radian(alpha);
b = (float)radian(beta);
c = (float)radian(c);
printf("%.2f %.2f %.2f\n",a,b,c);
float B,A = d*sinf(a)/sinf(c);
B = d*sinf(b)/sinf(c);
float dist,area = 1.0f/2.0f*A*B*sinf(c);
dist = 2*area/d;
printf("AC Distance : %.2f , BC distance : %.2f\n",A,B);
printf("Boat Distance : %.2f\n", dist);
return dist;
}
Simpler approach to find d distance. We can express lengths of the left and right parts of base through d and cotangents
aa = d * ctg(alpha)
bb = d * ctg(beta)
aa + bb = l
d * (ctg(alpha) + ctg(beta)) = l
d = l / (ctg(alpha) + ctg(beta)) =
= l * sin(alpha) * sin(beta) / sin(alpha+beta)
Now you can find C coordinates using normalized vector ab and perpendicular component
abx = (b.x - a.x) / l
aby = (b.y - a.y) / l
aalen = d / tg(alpha)
c.x = a.x + abx * aalen - aby * d
c.y = a.y + aby * aalen + abx * d
There you go I have written some code for your question..
float get_coordinates(int x1, int y1, int x2, int y2, float alpha, float beta){
float a_angle_alpha, b_angle_beta, c_angle_delta;
c_angle_delta = 180 - (float)alpha - (float)beta;
printf("Alpha Angle in Degrees = %.2f\nBeta Angle in Degrees = %.2f\nDelta Angle in Degrees = %.2f\n", alpha, beta, c_angle_delta);
a_angle_alpha = (float)radian(alpha);
b_angle_beta = (float)radian(beta);
c_angle_delta = (float)radian(c_angle_delta);
printf("Alpha in radiance: %.2f\nBeta in radiance: %.2f\nDelta in radiance: %.2f\n", a_angle_alpha, b_angle_beta, c_angle_delta);
int u, v;
float d;
u = x2 - x1;
v = y2 -y1;
printf("u = %.d\nv = %.d\n", u, v);
float d1_distance_AB, d2_distance_BC, d3_distance_AC;
d1_distance_AB = sqrtf((u*u)+ (v*v));
d2_distance_BC = d1_distance_AB*sinf(b_angle_beta)/sinf(c_angle_delta);
d3_distance_AC = d1_distance_AB*sinf(a_angle_alpha)/sinf(c_angle_delta);
printf("Distance between A and B: %.2f\nDistance between B and C: %.2f\nDistance between A and C: %.2f\n",d1_distance_AB, d2_distance_BC, d3_distance_AC);
float RHS1, RHS2;
float x3, y3;
RHS1 = ((x1 * u) + (y1 * v) + (d2_distance_BC * d1_distance_AB * cosf(a_angle_alpha)));
RHS2 = ((y2 * u) - (x2 * v) - (d2_distance_BC * d1_distance_AB * sinf(a_angle_alpha)));
x3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((u * RHS1) - (v * RHS2)));
y3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((v * RHS1) + (u * RHS2)));
printf("Coordinates of Boat at point C is = %.2f:%.2f (x:y)", x3, y3);
float coordinates[2] = {x3,y3};
return coordinates;
}
Full Code:
#include <stdio.h>
#include <math.h>
float radian(int degree);
float get_watchtowers_distance(int x1,int y1,int x2,int y2);
float get_boat_distance(float d,int alpha,int beta);
float get_coordinates(int x1, int y1, int x2, int y2, float alpha, float beta);
int main(){
get_watchtowers_distance(76,316,57,516);
get_boat_distance(200.90,17,17);
get_coordinates(76,316,57,516,17,17);
}
float radian(int degree){
return degree * (M_PI/180);
}
float get_watchtowers_distance(int x1,int y1,int x2,int y2){
return sqrtf(powf(x2-x1,2.0f)+powf(y2-y1,2.0f));
}
float get_boat_distance(float d,int alpha,int beta){
float a,b,c = 180 - (float)alpha - (float)beta;
a = (float)radian(alpha);
b = (float)radian(beta);
c = (float)radian(c);
printf("%.2f %.2f %.2f\n", a, b, c);
float B,A = d*sinf(a)/sinf(c);
B = d*sinf(b)/sinf(c);
float dist,area = 1.0f/2.0f*A*B*sinf(c);
dist = 2*area/d;
printf("AC Distance : %.2f , BC distance : %.2f\n",A,B);
printf("Boat Distance : %.2f\n", dist);
return dist;
}
float get_coordinates(int x1, int y1, int x2, int y2, float alpha, float beta){
float a_angle_alpha, b_angle_beta, c_angle_delta;
c_angle_delta = 180 - (float)alpha - (float)beta;
printf("Alpha Angle in Degrees = %.2f\nBeta Angle in Degrees = %.2f\nDelta Angle in Degrees = %.2f\n", alpha, beta, c_angle_delta);
a_angle_alpha = (float)radian(alpha);
b_angle_beta = (float)radian(beta);
c_angle_delta = (float)radian(c_angle_delta);
printf("Alpha in radiance: %.2f\nBeta in radiance: %.2f\nDelta in radiance: %.2f\n", a_angle_alpha, b_angle_beta, c_angle_delta);
int u, v;
float d;
u = x2 - x1;
v = y2 -y1;
printf("u = %.d\nv = %.d\n", u, v);
float d1_distance_AB, d2_distance_BC, d3_distance_AC;
d1_distance_AB = sqrtf((u*u)+ (v*v));
d2_distance_BC = d1_distance_AB*sinf(b_angle_beta)/sinf(c_angle_delta);
d3_distance_AC = d1_distance_AB*sinf(a_angle_alpha)/sinf(c_angle_delta);
printf("Distance between A and B: %.2f\nDistance between B and C: %.2f\nDistance between A and C: %.2f\n",d1_distance_AB, d2_distance_BC, d3_distance_AC);
float RHS1, RHS2;
float x3, y3;
RHS1 = ((x1 * u) + (y1 * v) + (d2_distance_BC * d1_distance_AB * cosf(a_angle_alpha)));
RHS2 = ((y2 * u) - (x2 * v) - (d2_distance_BC * d1_distance_AB * sinf(a_angle_alpha)));
x3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((u * RHS1) - (v * RHS2)));
y3 = ((1 / (d1_distance_AB * d1_distance_AB)) * ((v * RHS1) + (u * RHS2)));
printf("Coordinates of Boat at point C is = %.2f:%.2f (x:y)", x3, y3);
float coordinates[2] = {x3,y3};
return coordinates;
}
Output:
0.30 0.30 2.55
AC Distance : 105.04 , BC distance : 105.04
Boat Distance : 30.71
Alpha Angle in Degrees = 17.00
Beta Angle in Degrees = 17.00
Delta Angle in Degrees = 146.00
u = -19
v = 200
Distance between A and B: 200.90
Distance between B and C: 105.04
Distance between A and C: 105.04
Coordinates of Boat at point C is = 97.07:418.90 (x:y)
To find the distance d directly without determining point C, first, you can calculate the area S of the triangle in two ways.
S = 0.5 * l * d
S = 0.5 * l * l * sin(a) * sin(b) / sin(a + b)
Equating the two and canceling out 0.5 * l gives d = l * sin(a) * sin(b) / sin(a + b).

Tracing out the motion of a double pendulum in gnuplot (and gif conversion)?

I have written a C program to trace out the motion of a double pendulum, but am having difficulties in getting gnuplot (controlled from my c program) to trace out the paths of the masses (example).
Thus far I have created the program such that it produces a number of png images at each interval (using runge kutta method), however I want to output it as a gif instead so a line traces out the path of the masses in real time.
In the code below I am assuming the problem occurs with piping out to gnuplot from within the for loop (to save you from wasting your time sifting through it)
/* Header Files */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <assert.h>
// Definitions
#define GRAVITY 9.8
#define INCREMENT 0.0175
// Declerations of functions
double th1_xder(double t1d);
double th1_yder(double t2d);
double th2_xder(double t1d, double th1, double t2d, double th2, double l1, double l2, double m1, double m2);
double th2_yder(double t1d, double th1, double t2d, double th2, double l1, double l2, double m1, double m2);
int main (int argc, char * argv[]) {
double x1=0, y1=0, x2=0, y2=0; //coordinates
double l1=0, l2=0; //lengths
double m1=0, m2=0; //masses
double th1=0, th2=0; //angles
double t1d=0, t2d=0; //zero velocity initially
//second order Runge-Kutta equations
double k1=0, k2=0, k3=0, k4=0; //for x1
double q1=0, q2=0, q3=0, q4=0; //for y1
double r1=0, r2=0, r3=0, r4=0; //for x2
double s1=0, s2=0, s3=0, s4=0; //for y2
int i = 0;
double x0=0, y0=0;
printf("Enter '1' to input your own data or '0' to use the preset data\n");
char dummy = 'a';
scanf("%c", &dummy);
assert((dummy == '1') || (dummy == '0'));
if (dummy == '1') {
printf("Please enter a length l1:\n");
scanf("%lf", &l1);
printf("Please enter a length l2:\n");
scanf("%lf", &l2);
printf("Please enter a mass m1:\n");
scanf("%lf", &m1);
printf("Please enter a mass m2:\n");
scanf("%lf", &m2);
printf("Please enter an angle theta1:\n");
scanf("%lf", &th1);
printf("Please enter an angle theta2:\n");
scanf("%lf", &th2);
} else {
l1 = 1;
l2 = 1;
m1 = 1;
m2 = 1;
th1= 90;
th2= 0;
}
th1 = th1*(M_PI/180);
th2 = th2*(M_PI/180);
FILE *fsp;
if((fsp=fopen("origin.dat", "w"))==NULL) {
fprintf(stdout, "cannot open origin.dat\n");
exit (EXIT_FAILURE);
}
fprintf(fsp, "0\t0");
fclose(fsp);
FILE *fout;
if((fout=fopen("testout.dat", "w"))==NULL) {
fprintf(stdout, "cannot open testout.dat\n");
exit (EXIT_FAILURE);
}
printf("%f\t%f\t%f\t%f\n", x1, y1, x2, y2);
fprintf(fout, "%f\t%f\t%f\t%f\n", x1, y1, x2, y2);
for(i = 0; i < 250; i++) {
if ((fout=fopen("testout.dat", "w"))==NULL) {
fprintf(stdout, "cannot open testout.dat\n");
exit (EXIT_FAILURE);
}
k1 = th1_xder(t1d);
q1 = th1_yder(t2d);
r1 = th2_xder(t1d, th1, t2d, th2, l1, l2, m1, m2);
s1 = th2_yder(t1d, th1, t2d, th2, l1, l2, m1, m2);
k2 = th1_xder(t1d + (r1/2));
q2 = th1_yder(t2d + (s1/2));
r2 = th2_xder((t1d + (r1/2)), (th1 + (k1/2)), (t2d +(s1/2)), (th2 + (q1/2)), l1, l2, m1, m2);
s2 = th2_yder((t1d + (r1/2)), (th1 + (k1/2)), (t2d +(s1/2)), (th2 + (q1/2)), l1, l2, m1, m2);
k3 = th1_xder(t1d + (r2/2));
q3 = th1_yder(t2d + (s2/2));
r3 = th2_xder((t1d + (r2/2)), (th1 + (k2/2)), (t2d +(s2/2)), (th2 + (q2/2)), l1, l2, m1, m2);
s3 = th2_yder((t1d + (r2/2)), (th1 + (k2/2)), (t2d +(s2/2)), (th2 + (q2/2)), l1, l2, m1, m2);
k4 = th1_xder(t1d + r3);
q4 = th1_yder(t2d + s3);
r4 = th2_xder((t1d + r3), (th1 + k3), (t2d + s3), (th2 + q3), l1, l2, m1, m2);
s4 = th2_yder((t1d + r3), (th1 + k3), (t2d + s3), (th2 + q3), l1, l2, m1, m2);
t1d = t1d + (r1 + 2*r2 + 2*r3 + r4)/6;
t2d = t2d + (s1 + 2*s2 + 2*s3 + s4)/6;
th1 = th1 + (k1 + 2*k2 + 2*k3 + k4)/6;
th2 = th2 + (q1 + 2*q2 + 2*q3 + q4)/6;
x1 = l1*sin(th1);
y1 = -l1*cos(th1);
x2 = x1 + l2*sin(th2);
y2 = y1 - l2*cos(th2);
printf("%f\t%f\t%f\t%f\n", x1, y1, x2, y2);
fprintf(fout, "%f\t%f\t%f\t%f\n", x1, y1, x2, y2);
fclose(fout);
FILE *gnuplotPipe = popen("gnuplot -persist","w");
if (gnuplotPipe) {
fprintf(gnuplotPipe, "set style data lines\n");
fprintf(gnuplotPipe, "set terminal png nocrop enhanced size 1280,720; set output 'yyy%d.png'\n", i);
fprintf(gnuplotPipe, "set title 'frame%d'\n", i);
fprintf(gnuplotPipe, "set multiplot\n");
fprintf(gnuplotPipe, "set xrange [-2.5:2.5]; set yrange [-2.5:2]\n");
fprintf(gnuplotPipe, "unset key; unset ytics; unset xtics\n");
fprintf(gnuplotPipe, "plot 'testout.dat' using 3:4\n");
fprintf(gnuplotPipe, "plot '-' with lines lw 2 lc rgb 'black', 'testout.dat' u 1:2 w points pt 7 ps 2, 'testout.dat' u 3:4 w points pt 7 ps 2, 'origin.dat' u 1:2 w points pt 7 ps 2 lc 0\n");
fprintf(gnuplotPipe, "%f %f\n", x0, y0);
fprintf(gnuplotPipe, "%f %f\n", x1, y1);
fprintf(gnuplotPipe, "%f %f\n", x2, y2);
fprintf(gnuplotPipe, "e\n");
fprintf(gnuplotPipe, "\n");
fprintf(gnuplotPipe, "set nomultiplot\n");
fflush(gnuplotPipe);
fprintf(gnuplotPipe,"exit \n");
pclose(gnuplotPipe);
}
}
return EXIT_SUCCESS;
}
double th1_xder(double t1d) {
double k = t1d*INCREMENT;
return k;
}
double th1_yder(double t2d) {
double m = t2d*INCREMENT;
return m;
}
double th2_xder(double t1d, double th1, double t2d, double th2, double l1, double l2, double m1, double m2) {
double l = INCREMENT*((GRAVITY/l1)*((m2/(m1 + m2))*sin(th2)*cos(th1-th2)-sin(th1))-(m2/(m1 + m2))*sin(th1-th2)*((l2/l1)*t2d*t2d + t1d*t1d*cos(th1-th2)))/(1-((m2/(m1 + m2))*cos(th1-th2)*cos(th1-th2)));
return l;
}
double th2_yder(double t1d, double th1, double t2d, double th2, double l1, double l2, double m1, double m2) {
double p = INCREMENT*((GRAVITY/l2)*(sin(th1)*cos(th1-th2)-sin(th1)) + sin(th1-th2)*((l1/l2)*t1d*t1d + (m2/(m1 + m2))*t2d*t2d*cos(th1-th2)))/(1-((m2/(m1 + m2))*cos(th1-th2)*cos(th1-th2)));
return p;
}

Why doesn't this code for calculating the solution of quadratic equation not work?

Whenever I try running this, it returns the wrong solution, for example:
A: 303
B: 405
C: 50
Real solution: −0.13762776465722773
My solution : -110079.531250
#include <stdio.h>
#include <math.h>
int main(){
float a;
float b;
float c;
float solution;
float d;
printf("A: ");
scanf("%f", &a);
printf("B: ");
scanf("%f", &b);
printf("C: ");
scanf("%f",&c);
d = b * b - 4 * a * c;
solution = (-b - sqrt(d))/ 2*a;
printf("%f", solution);
}
You forgot BODMAS. Replace (-b - sqrt(d))/ 2*a by (-b - sqrt(d))/ (2*a)
solution = (-b - sqrt(d))/ (2*a);
Two things.
You need to watch out for the order of operations.
solution = (-b - sqrt(d)) / (2*a);
And depending on your customer you need to consider the accuracy of your result.
See "Avoiding loss of significance" for more information
And finally - i had a bit of fun writing my own version of your program:
#include <stdio.h>
#include <math.h>
void printLineSolution( double a, double b, double c );
int main()
{
printLineSolution(303,405,50);
printLineSolution(1,2,0);
printLineSolution(1,2,-1);
printLineSolution(1,-2,-3);
printLineSolution(1,-6,9);
printLineSolution(1,3,3);
getchar();
}
void printLineSolution( double a, double b, double c )
{
double d = (b * b) - (4 * a * c);
printf("(%lg)x^2 + (%lg)x + (%lg) = 0 ", a, b, c);
if( a == 0 )
{
printf("=> not quadratic");
}
else
{
if( 0 > d )
{
double r = - b / (2*a);
double i = sqrt( -d ) / (2*a);
printf("=> 2 complex: %lg + %lgi ; %lg - %lgi", r, i, r, i);
}
else if ( 0 == d )
{
double solution = - b / (2*a);
printf("=> 1 real: %lg", solution);
}
else
{
double s1 = (- b + sqrt( d ) ) / (2*a);
double s2 = (- b - sqrt( d ) ) / (2*a);
printf("=> 2 real: %lg ; %lg", s1, s2);
}
}
printf("\n");
}

Resources