Code for calculating 2nd degree equation returns "Nan" as roots

Asked

Viewed 1,734 times

5

I tried to create a program that calculates the two roots of a second-degree equation. When I run my code, it asks for the values of a, b and c correctly, but when showing the result, it always returns "Nan".

My code is this, I don’t know how to solve:

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);
    }
}
  • Basically it’s because you’re trying to calculate the negative number root. Depending on the values, delta will be less than zero, and the sqrt will give Nan

1 answer

9


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

When the value of the discriminant (delta) is negative (and this is common in a quadratic equation), the function Math.sqrt 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 work normally with the positive values, and "warn" in the output that they deal with complex roots when the case is.

You can do this very simply with a small adaptation to 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);
    }

Basically we add the 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 always positive number.

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

Note: in its original code, you are storing the round in r1 and r2, but is not using the value anywhere. If shown on the screen the rounded values, adjust the output lines:

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

See working on IDEONE.

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

  • 1

    Thanks for the answer, I really wasn’t considering the probability of negative numbers. My upvote for you :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.