I want to do math with the pq-formula but for some reason everything works except that the numbers are different then they should be.
The Formula that I want to represent is: x1,2 = -p/2 ± √(p^2/4 -q)
- If I do the Math with a calculator and not with my program I get other results (I tried with calculator and by hand)
Example with p=12 & q=-12:
what I get: x1 = 9.0 and x2= -21.0 what I want to get (does not have to be exact the same but close): x1 = 0.92820323027551 x2 = -12.928203230276
(teil means part; null means zero)
public class PQ {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("ERROR: Bitte genau zwei Argumente angeben");
return;
}
double p = Double.parseDouble(args[0]);
double q = Double.parseDouble(args[1]);
double teil1 = -(p / 2);
double teil2 = (Math.sqrt(p * p) / 4 - q);
double teil3 = (p * p / 4 - q);
double Null1 = teil1 + teil2;
double Null2 = teil1 - teil2;
if (teil3 < 0) {
System.out.println("Es gibt 0 Nullstellen.");
}
if (Null1 > Null2) {
System.out.println(Null2);
System.out.println(Null1);
System.out.println("Es gibt 2 Nullstellen.");
} else if (Null1 < Null2) {
System.out.println(Null1);
System.out.println(Null2);
System.out.println("Es gibt 2 Nullstellen.");
return;
}
if (Null1 == Null2) {
System.out.println(Null1);
System.out.println("Es gibt 1 Nullstelle.");
}
}
}
I tried changing the formula but there are always the same results