NaN在二次方程中

时间:2017-04-12 19:54:21

标签: java eclipse

无论如何,我可以提高效率,并且我一直得到NaN的答案我需要帮助!

import java.util.Scanner;
public class RLAQuadraticEquation {
public static void main(String[] args){

    Scanner in = new Scanner(System.in);

    final int FOUR = 4;
    double a, b, c, discriminant, posQuadEquation, negQuadEquation, disc;

    System.out.print("Enter a value for a");
    a = in.nextDouble();
    System.out.println("Enter a value for b");
    b = in.nextDouble();
    System.out.println("Enter a value for c");
    c = in.nextDouble();
    in.close();

    disc = Math.sqrt((b*b) - (FOUR*a*c));
    discriminant = (b*b) - (FOUR*a*c);

    if(Math.abs(discriminant) > 0.00000001){
        posQuadEquation = Math.round(- b + Math.sqrt(disc))/(2*a);
        negQuadEquation = Math.round(-b - Math.sqrt(disc))/(2*a);
        System.out.println("The roots are" + posQuadEquation + "and" + negQuadEquation);
    }
    else if ((discriminant) <= (0.00000001)){
        posQuadEquation = (-b+ Math.sqrt(disc))/(2*a);
        System.out.println("The root is: " + posQuadEquation);
    }
    else
        System.out.println("No roots");
    }   
}

我遇到很多麻烦任何反馈或建议都会有所帮助! 谢谢!!

2 个答案:

答案 0 :(得分:1)

当使用.9 .6和.1作为输入时,判别式计算为-5.551115123125783E-17而不是零。这是因为浮点数的处理方式(它们并不总是准确的),这可能是他们告诉你回合的原因。但是,因为判别式计算为略低于零,所以取平方根得到NaN(因为负数的平方根是虚数。)

你有这个检查:

if(Math.abs(discriminant) > 0.00000001){

判断判别式是否接近零。但如果它接近于零,你可以:

posQuadEquation = (-b+ Math.sqrt(disc))/(2*a);

但是知道此时判别式为零。因此,您可以跳过平方根,甚至可以跳过添加或减去它。你可以这样做:

posQuadEquation = (-b)/(2*a);

答案 1 :(得分:0)

如果判别式为&lt;

我认为你根本不处理根的复杂部分。 0

您需要检查判别式是正面(真实根)还是负面(复杂根),然后相应地进行。另外,检查是否a == 0以确保您有一个真正的二次方程并防止除以0可能是一个好主意。您可能还想对用户的非双输入做一些额外的保护,但是我没有包含任何内容。

以下是对您的代码的一些修改:

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

    final int FOUR = 4;
    double a, b, c, discriminant, realPart, imPart, posQuadEquation, disc;

    System.out.println("Enter a value for a");
    a = in.nextDouble();
    System.out.println("Enter a value for b");
    b = in.nextDouble();
    System.out.println("Enter a value for c");
    c = in.nextDouble();
    in.close();

    // if a == 0 it's not a true quadratic, and you would be dividing by 0.
    if (a == 0) {
        System.out.println("Non-Quadratic Equation, \"a\" should not equal 0");
    } else {
        discriminant = (b*b) - (FOUR*a*c);
        boolean isImaginary = false;
        // we've got real roots
        if (discriminant >= 0) {
            disc = Math.sqrt(discriminant);
        // there's a complex part to the roots
        } else {
            isImaginary = true;
            disc = Math.sqrt(-discriminant);
        }

        if(disc > 0.00000001){
            realPart = - b/(2*a);
            imPart = disc/(2*a); 
            String posEq = (realPart + imPart) + "";
            String negEq = (realPart - imPart) + "";
            if (isImaginary) {
                posEq = realPart + " + " + imPart + "i";
                negEq = realPart + " - " + imPart + "i";
            }
            System.out.println("The roots are: " + posEq + " and " + negEq);
        }
        else if (disc <= (0.00000001)){
            posQuadEquation = (-b + disc)/(2*a);
            System.out.println("The root is: " + posQuadEquation);
        }
        else
            System.out.println("No roots");
    }
}