Home > Java > javaTutorial > body text

How to use the isInfinite() method of the Double class to determine whether a number is infinite

WBOY
Release: 2023-07-25 11:09:15
Original
1295 people have browsed it

How to use the isInfinite() method of the Double class to determine whether a number is infinite

In numerical calculations, we often encounter situations where we need to determine whether a number is infinite. The Double class in Java The isInfinite() method is provided to meet this requirement. This article will introduce how to use the isInfinite() method of the Double class to determine whether a number is infinite, and provide corresponding code examples.

Double class is a wrapper class used to represent double-precision floating point numbers in Java. The isInfinite() method is an instance method provided by the Double class, which is used to determine whether a double-precision floating-point number is infinite. The function prototype of this method is:

public boolean isInfinite()

This method does not accept any parameters and the return value is of boolean type. If the value of this Double object is positive infinity or negative infinity, return true; otherwise, return false.

Let's look at some code examples that use the isInfinite() method to determine whether a number is infinite.

Example 1: Determine whether a number is infinite

public class InfiniteExample {
    public static void main(String[] args) {
        Double num1 = Double.POSITIVE_INFINITY;
        Double num2 = Double.NEGATIVE_INFINITY;
        Double num3 = 100.0;

        System.out.println(num1 + " is infinite? " + num1.isInfinite());
        System.out.println(num2 + " is infinite? " + num2.isInfinite());
        System.out.println(num3 + " is infinite? " + num3.isInfinite());
    }
}
Copy after login

The running result is:

Infinity is infinite? true
-Infinity is infinite? true
100.0 is infinite? false

In this example, we define three Double objects: num1, num2 and num3. The value of num1 is set to positive infinity, the value of num2 is set to negative infinity, and the value of num3 is set to 100.0. Use the isInfinite() method to judge these three numbers in turn, and output the judgment results.

It can be seen from the running results that the values ​​of num1 and num2 are infinite, so the corresponding isInfinite() method returns true; and the value of num3 is limited 100.0, so the isInfinite() method returns a value of false.

Example 2: Using the isInfinite() method for exception handling

public class InfiniteExceptionExample {
    public static void main(String[] args) {
        try {
            double result = 10.0 / 0.0;
            System.out.println(result);
        } catch (ArithmeticException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}
Copy after login

The running result is:

Error: / by zero

In this example, We are trying to calculate the result of dividing 10.0 by 0.0. Since 0.0 is an invalid denominator, an ArithmeticException will be thrown. We can avoid the program from exiting abnormally by performing exception handling in the try-catch block.

Summary:

This article introduces how to use the isInfinite() method of the Double class to determine whether a number is infinite, and provides corresponding code examples. Using the isInfinite() method can easily determine whether a number is infinite, thereby better handling abnormal situations that may be encountered in numerical calculations. In actual development, this method can be used flexibly according to specific needs to improve the robustness of the code.

The above is the detailed content of How to use the isInfinite() method of the Double class to determine whether a number is infinite. 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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template