Home > Java > Java Tutorial > body text

How to use the isNaN() method of the Double class to determine whether a number is NaN

王林
Release: 2023-07-24 20:09:19
Original
1703 people have browsed it

How to use the isNaN() method of the Double class to determine whether a number is NaN

NaN is a special numerical value that represents Not-a-Number. In Java, the Double class provides the isNaN() method to determine whether a number is NaN. This article will introduce how to use the isNaN() method of the Double class to determine whether a number is NaN, and provide corresponding code examples.

First of all, we need to understand some characteristics of NaN. NaN is a special floating-point number that is mathematically defined as the result of an impossible mathematical operation. The characteristics of NaN include:

  1. The result of any mathematical operation between NaN and any number (including NaN itself) is still NaN;
  2. The result of comparing NaN with any number (including NaN) The result is always false;
  3. Use the Double.isNaN() method to determine whether a number is NaN.

Next, we will use a code example to demonstrate how to use the isNaN() method of the Double class to determine whether a number is NaN.

public class DoubleExample {
    public static void main(String[] args) {
        double num1 = 10.5;
        double num2 = Double.NaN;
        
        System.out.println("判断num1是否为NaN:" + Double.isNaN(num1));
        System.out.println("判断num2是否为NaN:" + Double.isNaN(num2));
    }
}
Copy after login

In the above code, we define two double type variables num1 and num2. Among them, the value of num1 is set to a valid floating point value 10.5, and the value of num2 is set to NaN.

Next, we use the Double.isNaN() method to determine whether num1 and num2 are NaN. By calling the Double.isNaN(num1) and Double.isNaN(num2) methods, we get the results of whether num1 and num2 are NaN respectively.

Run the above code and you will get the following output:

判断num1是否为NaN:false
判断num2是否为NaN:true
Copy after login

As you can see from the output, num1 is not a NaN value, so the result is false; and the value of num2 is NaN, so the result is true.

To summarize, using the Double.isNaN() method can very conveniently determine whether a number is NaN. You only need to pass the number to be judged as a parameter into the Double.isNaN() method, and you can get a Boolean value to indicate whether the number is NaN.

Through the introduction and code examples of this article, I believe readers have mastered how to use the isNaN() method of the Double class to determine whether a number is NaN. I hope this article is helpful to readers, thank you for reading!

The above is the detailed content of How to use the isNaN() method of the Double class to determine whether a number is NaN. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!