-3

Question : I thought the source code is right and it worked well a lot of times for example at functions like : x^2 + 4x +4 or 3x^2 +5x+1 but in some functions the output is NaN and i don't understand why (function that outputs NaN : 4x^2 - 2x +8

import java.util.*; 
public class QuadGl {

    /**      * @param args   */
    public static void main(String[] args) {

    double a,b,c,rechnung1,rechnung11,rechnung2,rechnung22,rechnung23,ergebnis1,ergebnis2;//BENÖTIGTE DATENTYPEN

    Scanner input = new Scanner(System.in);             
    System.out.println("Bitte geben sie einen Wert für a ein : "); a = input.nextDouble();
    System.out.println("Bitte geben sie einen Wert für b ein : "); b = input.nextDouble(); 
    System.out.println("Bitte geben sie einen Wert für c ein : "); c = input.nextDouble();

    //ERSTER RECHNUNGSSCHRITT DER P.Q.FORMEL 
    rechnung1 = b/a;//b=p
    rechnung11 = c/a; //c=q 
    System.out.println(rechnung1);
    System.out.println(rechnung11);


    //ZWEITER RECHNUNGSSCHRITT 
    rechnung2 = (-rechnung1/2);// -b/2
    rechnung22 = (rechnung1/2); // b/2 
    rechnung23 = Math.sqrt(( rechnung22 * rechnung22 -rechnung11));//

    System.out.println(rechnung2);
    System.out.println(rechnung22);
    System.out.println(rechnung23);

    ergebnis1 = rechnung2 + rechnung23;//-b/2 + b/2 ^2 - c 
    ergebnis2 = rechnung2 - rechnung23;//-b/2 - b/2 ^2 - c 

    System.out.println("x(1)= "+ ergebnis1);
    System.out.println("x(2)= " + ergebnis2);
    }

}
alk
  • 69,737
  • 10
  • 105
  • 255
burakburi
  • 23
  • 1
  • 7
  • Is there a data type for complex numbers in Java? – alk Jun 25 '16 at 11:58
  • @alk As far as I know, no. But you can create one! I know that in Ruby, you can do it (calculation with complex numbers) – dryairship Jun 25 '16 at 12:06
  • @Hackerdarshi: In C there is one; so perhaps retag this C again? – alk Jun 25 '16 at 12:11
  • 1
    @alk Ahh... No. Please. The question has been answered. There is no need for anything now. Moreover, the code is in Java. So tagging C would be like ... what? – dryairship Jun 25 '16 at 12:16
  • It had been tagged C in the beginning as well, perhaps with the intention in mind to received a C based answer using complex numbers? ;-) @Hackerdarshi – alk Jun 25 '16 at 12:17
  • @alk Well, but if he asked this question, then that means that he was probably unaware that it shows NaN because it is a complex number... – dryairship Jun 25 '16 at 12:22

2 Answers2

4

4x2 - 2x +8 shows NaN because, the formula to solve a quadratic equation is :

(-b ± (b2 - 4ac)1/2) / 2a

Here, b2 - 4ac is -ve (4 - 4*16*8), and hence, its square root is NaN

b2 - 4ac is called Discriminant.

  • When it is equal to 0, the roots are equal.
  • When is is less than 0, you will get NaN, as the roots are imaginary.
  • When it is greater than 0, you get two different roots.
Boann
  • 48,794
  • 16
  • 117
  • 146
dryairship
  • 6,022
  • 4
  • 28
  • 54
0

Just format the output according to the discriminant

disc = b*b-4*a*c
if( disc > 0 ) {
    rdisc = sqrt( disc )/(2*abs(a));
    print("1st root = " + ( -b / (2*a) + rdisc ) );
    print("2nd root = " + ( -b / (2*a) - rdisc ) );
} else {
    rdisc = sqrt( -disc )/(2*abs(a));
    print("1st root = " + ( -b / (2*a) ) + " + i*" + rdisc );
    print("2nd root = " + ( -b / (2*a) ) + " - i*" + rdisc );

}

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51