-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcardano.c
74 lines (60 loc) · 1.39 KB
/
cardano.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
/********* DEFINED CONSTANTS *********/
#define MAX 4
/********* FUNCTION DECLARATION *********/
void cardano(float co[]);
/********* MAIN STARTS HERE *********/
int main(void)
{
int j;
float co[MAX];
for (j = 0; j < MAX; j++)
{
printf("Enter the co-efficient of x^%d: ", j);
scanf("%f", &co[j]);
}
cardano(co);
exit(0);
}
void cardano(float co[])
{
float Q, R, S, T, val1, val2, real, img, x1;
float complex x2, x3;
Q = (((3*co[3]*co[1])-(powf(co[2], 2)))/(9*(powf(co[3], 2))));
R = (((9*co[3]*co[2]*co[1])-(27*(powf(co[3], 2))*co[0])-(2*(powf(co[2], 3))))/(54*(powf(co[3], 3))));
val1 = (R+sqrtf((Q*Q*Q)+(R*R)));
val2 = (R-sqrtf((Q*Q*Q)+(R*R)));
if (val1 < 0)
{
val1 = -val1;
S = cbrtf(val1);
S = -S;
}
else
{
S = cbrtf(val1);
}
if (val2 < 0)
{
val2 = -val2;
T = cbrtf(val2);
T = -T;
}
else
{
T = cbrtf(val2);
}
real = (-((S+T)/2)-(co[2]/(3*co[3])));
img = ((sqrtf(3.0/4.0))*(S-T));
x1 = S+T-(co[2]/(3*co[3]));
x2 = real + img*I;
x3 = conjf(x2);
printf("The solutions for the Equation are:\n");
printf("%f\n", x1);
printf("%f+(%fJ)\n", creal(x2), cimag(x2));
printf("%f+(%fJ)\n", creal(x3), cimag(x3));
return ;
}