# Java：实现简单的等式Java: Implementing simple equation

i，j = -Q±√（Q 2 -4PR）/ 2P

``````    //Q*Q – 4PR = -351 mod 11 = -10 mod 11 = 1, √1 = 1
double test = Math.sqrt(modulo(((Q*Q) - ((4*P)*R))));

// Works, but why *-10 needed?
i = (int)(((-Q+test)/(P*2))*-10);    // i = 3
j = (int)(((-Q-test)/(P*2))*-10);    // j = 4
``````

## 5 个回复5

### ===============>>#1 票数：6

``````public static void findRoots(double a, double b, double c)
{
if (b * b < 4 * a * c)
{
throw new IllegalArgumentException("Equation has no roots");
}

double tmp = Math.sqrt(b * b - 4 * a * c);
double firstRoot = (-b + tmp) / (2 * a);
double secondRoot = (-b - tmp) / (2 * a);
System.out.println("Roots: " + firstRoot + ", " + secondRoot);
}
``````

``````public static int modulo(int x)
{
return ((x % 11) + 11) % 11;
}
``````

``````public static void findRoots(double a, double b, double c)
{
int squareMod11 = (((b * b - 4 * a * c) % 11) + 11) % 11;
double tmp = Math.sqrt(squareMod11);
double firstRoot = (-b + tmp) / (2 * a);
double secondRoot = (-b - tmp) / (2 * a);
System.out.println("Roots: " + firstRoot + ", " + secondRoot);
}
``````

### ===============>>#3 票数：1

``````
double test = Q*Q - 4*P*R;
if(Q < 0) throw new Exception("negative discriminant!");
else {
test = Math.sqrt(test);
double i = (-Q + test) / 2*P;
double i = (-Q - test) / 2*P;
}
``````

### ===============>>#4 票数：0

``````double delta = Q*Q-4*P*R);
if(delta < 0.0) {
throw new Exception("no roots");
}
double d = Math.power(delta,0.5);
double r1 = (Q + d)/(2*P)
double r2 = (Q - d)/(2*P)
``````

### ===============>>#5 票数：0

“C ++中的数字配方”第5.6节提出了一种更好的方法：如果我们定义的话

（来源： equationsheet.com

（来源： equationsheet.com

（来源： equationsheet.com

（来源： equationsheet.com

5回复

3回复

3回复

3回复

1回复

5回复

1回复

1回复

4回复

3回复