Reputation: 2428
I'm making a simple test in java, (i'm a begineer) and i was trying to make a program that calculates the discriminate of a second degree equation and displays the results, but when i change the sign of nom1 and nom2, the program doesn't displays any result, i've made some tests and i'm sure that the problem is there but i don't know how to resolve it. Any help is welcome.
Here is my code:
public class test {
public static void main(String[] args) {
double a = 2;
double b = 5;
double c = 3;
double delta = b*b - 4 * a * c;
double den = 2 * a;
if(delta == 0){
double nom = b;
double pgcd = pgcd(nom, den);
double x0nom = nom/pgcd;
double x0den = den/pgcd;
System.out.println("x0 = "+String.valueOf(x0nom)+" / "+String.valueOf(x0den));
}else if(delta > 0){
// x1:
double nom1 = -b + Math.sqrt(delta);
double pgcd1 = pgcd(nom1, den);
int x1nom = (int) (nom1 / pgcd1);
int x1den = (int) (den / pgcd1);
// x2:
double nom2 = -b - Math.sqrt(delta);
double pgcd2 = pgcd(nom2, den);
int x2nom = (int) (nom2 / pgcd2);
int x2den = (int) (den / pgcd2);
System.out.println("pgcd = "+String.valueOf(pgcd1)+" "+String.valueOf(pgcd2));
System.out.println("x1 = "+String.valueOf(x1nom)+" / "+String.valueOf(x1den));
System.out.println("x2 = "+String.valueOf(x2nom)+" / "+String.valueOf(x2den));
}else if(delta < 0){
}
}
public static double pgcd(double a, double b) {
while (a != b) {
if (a < b){
b = b - a;
}else{
a = a - b;
}
}
return a;
}
}
Upvotes: 2
Views: 95
Reputation: 3898
public static double pgcd(double a, double b) {
return (BigInteger.valueOf((long) a).gcd(BigInteger.valueOf((long) b)).intValue());
}
Upvotes: 1