0
    int x1, x2; double d;
    d = (int)(Math.sqrt(b*b - 4*a*c));
    if (d >= 0) {
        x1 = (-b + d) / 2*a;    // on this line 

"Type mismatch: cannot convert from double to int"

  • Maybe try casting d to an `Integer` instead of an `int`? – Santiago Benoit Jan 06 '15 at 21:41
  • 2
    @Santiago: That still wouldn't work. – Makoto Jan 06 '15 at 21:42
  • 1
    I'm not clear on why there are any integers involved in this code anyway. The roots of a quadratic equation are not necessarily integers, so what exactly are you trying to accomplish by using integers and trying to convert to integers? – ajb Jan 06 '15 at 21:52

6 Answers6

7

Here x1 shouldn't be an int. It looks like you're using it to store a root of a quadratic equation, which of course is likely not to be a whole number.

Change x1 (and probably x2 too) to be a double and the problem will go away.

Incidentally, you want parentheses around 2 * a, otherwise you're effectively putting a on the numerator, not the denominator.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Also the `(int)` cast needs to be removed when assigning to `d`. – ajb Jan 06 '15 at 21:50
  • 1
    @ajb Yes, that's true. The `if` can also be removed, since `Math.sqrt()` always either returns something `>= 0`, or throws an exception. – Dawood ibn Kareem Jan 06 '15 at 21:51
  • @DavidWallace If the argument is negative, Math.sqrt returns a NaN, rather than throwing an exception. Whether it needs to be tested depends on what the code is supposed to do for an equation with no real solutions. – Patricia Shanahan Jan 06 '15 at 22:40
  • Oops, my bad. I forgot about `NaN`. Thanks @PatriciaShanahan. Of course, the best thing for this code to do would be to check the sign of `b * b - 4 * a * c` before trying to square root it. – Dawood ibn Kareem Jan 06 '15 at 22:46
1

d is a double, so (-b + d) is a double. Even if b isn't a double, it'll get widened to one for that expression. Therefore, (-b + d) / 2*a is a double. Java won't let you implicitly convert that double expression to an int, because you could lose precision.

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • 1
    I think the issue is that he thinks when he is type casting that he is actually changing the variable type instead of actually casting the value. – Iootu Jan 06 '15 at 21:42
1

You simply need to do a cast to int at the point where you want to store the result of a double calculation into an int:

public class Test {
  public static void main(String[] args) {
    double a = 1.0;
    double b = 3.0;
    double c = 2.0;

    int x1, x2;
    double d;
    d = Math.sqrt(b * b - 4 * a * c);
    if (d >= 0) {
      x1 = (int) ((-b + d) / 2 * a); // on this line
    }
  }
}

You should also take another look at where you are doing the sign test d >= 0. It will work, because the result of a Math.sqrt call is either a non-negative number or a NaN, but testing the square root operand before the call would be clearer.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

Your error is from:

x1 = (-b + d) / 2*a;

Your variable did still defined as a double. When you add it to -b this will return a double. Try saving this line as a new int variable instead of d:

 Int temp = (int)(Math.sqrt(b*b - 4*a*c));
James C. Taylor IV
  • 599
  • 10
  • 24
0

d is still a double, because it was declared as a double. Casting only changes the type of a value; it won't change d itself to an int. If you want d to be an int, declare it as an int, like this:

int d;

instead of

double d;

and keep the rest of your code the same. This should fix the problem.

Santiago Benoit
  • 994
  • 1
  • 8
  • 22
0

If you're deadset on ending up with an int, you can do the following:

double answer = (-b + d) / 2*a;
x1 = new Double(answer).intValue();

Otherwise, the other answers have sufficiently told you why you're running into issues with casting.

How to convert Double to int directly?

Community
  • 1
  • 1
Foosh
  • 1,195
  • 12
  • 16
  • What is the advantage of boxing the `double` to a `Double` and using `intValue()`, as opposed to just casting to `(int)`? Isn't that just redundant code to do the same thing? – ajb Jan 06 '15 at 21:54
  • @ajb, it is, just presenting an alternative. If you want to get clever about it one could use Math.round() and then Long.intValue() as well. They all get to the same end. – Foosh Jan 06 '15 at 22:01