-1

I was wondering if anyone was able to understand the reason why this program doesn't return NaN the way it should.

I'm currently writing a program that calculates the solutions of a quadratic equation, but if the solutions are not real, I want it to print out "The solutions are not real" instead of NaN. Instead, it keeps on printing NaN nonetheless.

This is the code I've written:

    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;
            
        }
    
    }
  • 1
    Does this answer your question? [How do you test to see if a double is equal to NaN?](https://stackoverflow.com/questions/1456566/how-do-you-test-to-see-if-a-double-is-equal-to-nan) – maloomeister Nov 18 '21 at 12:24
  • TL;DR: Don't use `doubleValue == Double.NaN`, instead use `Double.isNaN(doubleValue)`. – maloomeister Nov 18 '21 at 12:26

1 Answers1

1

You should use Double.isNaN(x) instead of comparing x == Double.NaN.

It's an interesting property of NaN that NaN != NaN. You can check this question for the reasons behind that.

kikon
  • 3,670
  • 3
  • 5
  • 20