Code to calculate 2nd degree equation returns "NaN" as roots

5

I tried to create a program that calculates the two roots of a second-degree equation. When I execute my code, it asks for the values of a , b and c correctly, but at the time of displaying the result, it always returns "NaN".

My code is this, I do not know how to solve it:

package com.Class1;
import java.lang.*;
import java.util.Scanner;

public class Class1 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("Insira o valor de a:");
        int a = in.nextInt();

        System.out.println("Insira o valor de b:");
        int b = in.nextInt();

        System.out.println("Insira o valor de c:");
        int c = in.nextInt();

        double pB = Math.pow(b, 2);
        double delta = pB - 4 * a * c;
        double x1 = -1 * pB + Math.sqrt(delta) / 2 * a;
        double x2 = -1 * pB - Math.sqrt(delta) / 2 * a;

        double r1 = Math.round(x1);
        double r2 = Math.round(x2);

        System.out.println("A raíz x1 vale: "+ x1);
        System.out.println("A raíz x2 vale: "+ x2);
    }
}
    
asked by anonymous 10.11.2016 / 23:23

1 answer

9

You are not considering the negative (complex root) discriminant in your code.

When the discriminant value ( delta ) is negative (and this is common in a quadratic equation), the Math.sqrt function returns NaN , which means "Not a number". The Nan is propagated by the rest of the expression, being stored in x1 and x2 .

As in the quadratic equation you can have complex roots in the result, a very simple solution is you normally work with positive values, and "warn" in the output that they are dealing with complex roots when appropriate.

You can do this quite simply with a little customization in your code:

    double pB = Math.pow(b, 2);
    double delta = pB - 4 * a * c;
    double x1 = -1 * pB + Math.sqrt(Math.abs(delta)) / 2 * a;
    double x2 = -1 * pB - Math.sqrt(Math.abs(delta)) / 2 * a;

    double r1 = Math.round(x1);
    double r2 = Math.round(x2);

    if(delta < 0) {
        System.out.println("A raíz x1 vale: "+ x1 + "i");
        System.out.println("A raíz x2 vale: "+ x2 + "i");
    } else {
        System.out.println("A raíz x1 vale: "+ x1);
        System.out.println("A raíz x2 vale: "+ x2);
    }

We basically add Math.abs here:

    double x1 = -1 * pB + Math.sqrt(Math.abs(delta)) / 2 * a;

The Math.abs returns the absolute value, which in practice will return the number always positive.

For this, we use if(delta < 0) to add i to output println , because in this case, they are complex roots, not real roots. Fundamental to understand that 9 and 9i are completely different things (but there the problem is of Mathematics).

Note: In your original code, you are storing round in r1 and r2 , but you are not using the value anywhere. If it is the case to display the rounded values on the screen, adjust the output lines:

System.out.println("A raíz x1 vale: "+ r1); // troque o x por r em todas

See working at IDEONE .

If you want to improve your code, you can create a situation to show a root only when the discriminant is zero.

    
10.11.2016 / 23:49