我不明白如何在 Java 中正确使用 NaN [重复]

Posted

技术标签:

【中文标题】我不明白如何在 Java 中正确使用 NaN [重复]【英文标题】:I don't understand how to properly work with NaN in Java [duplicate] 【发布时间】:2021-12-29 08:04:06 【问题描述】:

我想知道是否有人能够理解这个程序没有按应有的方式返回 NaN 的原因。

我目前正在编写一个计算二次方程解的程序,但如果解不是实数,我希望它打印出“解不是实数”而不是 NaN。相反,它仍然继续打印 NaN。

这是我写的代码:

    import java.util.Scanner;
    import java.lang.*;
    
    public class QuadraticEquationTester 
    
        public static void main(String[] args) 
    
            Scanner scan = new Scanner(System.in);
    
            System.out.println("Insert the value a of the following equation: a*x^(2)+b*x+c=0");
            double a = Double.parseDouble(scan.nextLine());
            System.out.println("Insert the value b of the following equation: a*x^(2)+b*x+c=0");
            double b = Double.parseDouble(scan.nextLine());
            System.out.println("Insert the value c of the following equation: a*x^(2)+b*x+c=0");
            double c = Double.parseDouble(scan.nextLine());
            scan.close();
    
            QuadraticEquation equation1 = new QuadraticEquation(a, b, c);
            equation1.getEquation();
    
            System.out.println("Does this equation has solutions? " + equation1.hasSolutions());
            System.out.println("How many solutions does this equation have? " + equation1.howManySolutions());
    
            if (equation1.getSolution1() == Double.NaN && equation1.getSolution2() == Double.NaN) 
                System.out.println("There are no real solutions");
            
    
            if (equation1.getSolution1() != Double.NaN) 
                System.out.println("The first solution is: " + equation1.getSolution1());
             else if (equation1.getSolution1() == Double.NaN) 
                System.out.println("The first solution is not real");
            
    
            if (equation1.getSolution2() != Double.NaN) 
                System.out.println("The second solution is: " + equation1.getSolution2());
             else if (equation1.getSolution2() == Double.NaN) 
                System.out.println("The second solution is not real");
            
    
            if (equation1.getSolution1() != Double.NaN && equation1.getSolution2() != Double.NaN) 
                System.out.println(equation1.getAllSolutions());
            
    
        
    
    
    
    class QuadraticEquation 
    
        // I'm initialing the variables
        private double a;
        private double b;
        private double c;
        private double delta;
    
        // This is the basic constructor: it initializes the variables to zero
        public QuadraticEquation() 
            this.a = 0;
            this.b = 0;
            this.c = 0;
        
    
        public QuadraticEquation(double aCoefficient, double bCoefficient, double cCoefficient) 
            this.a = aCoefficient;
            this.b = bCoefficient;
            this.c = cCoefficient;
    
            this.delta = (Math.pow(this.b, 2)) - 4 * this.a * this.c;
        
    
        public void getEquation() 
            System.out
                    .println("The equation we're going to solve is " + this.a + "*x^(2)+" + this.b + "*x+" + this.c + "=0");
        
    
        public double getSolution1() 
            double solution1 = (-this.b + (Math.sqrt(this.delta))) / (2 * this.a);
            return solution1;
        
    
        public double getSolution2() 
            double solution2 = (-this.b - (Math.sqrt(this.delta))) / (2 * this.a);
            return solution2;
        
    
        public String getAllSolutions() 
            double solution1 = (-this.b + (Math.sqrt(this.delta))) / (2 * this.a);
            double solution2 = (-this.b - (Math.sqrt(this.delta))) / (2 * this.a);
            return "The first solution is: " + solution1 + " while the second solution is: " + solution2;
        
    
        public boolean hasSolutions() 
    
            if (this.delta >= 0) 
                return true;
             else if (this.delta < 0) 
                return false;
            
    
            return false;
        
    
        public String howManySolutions() 
    
            if (this.a == 0 && this.b == 0 && this.c != 0) 
                return "0 Solutions";
             else if (this.a == 0 && this.b != 0) 
                return "1 Solution";
             else if (this.a == 0 && this.b == 0 && this.c == 0) 
                return "Infinite Solutions";
             else 
                return "2 Solutions";
            
    
        
        
        public static boolean isNaN(double v) 
            if (v == Double.NaN) 
                return true;
            
            else return false;
            
        
    
    

【问题讨论】:

这能回答你的问题吗? How do you test to see if a double is equal to NaN? TL;DR:不要使用doubleValue == Double.NaN,而是使用Double.isNaN(doubleValue) 【参考方案1】:

您应该使用Double.isNaN(x) 而不是比较x == Double.NaN

NaN != NaN 是 NaN 的一个有趣属性。具体原因可以查看this question。

【讨论】:

以上是关于我不明白如何在 Java 中正确使用 NaN [重复]的主要内容,如果未能解决你的问题,请参考以下文章

您好,我是 WTL 的新手,我不明白如何在 ListView Ctrl 上接收消息

表单中的复选框

标签中的gif的Java问题

如何以正确的方式将 PostgreSQL 连接到 NodeJS? [复制]

标签中 gif 的 Java 问题

Expo Electron OAuth2 登录