Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What will be output from System.out.println (1.0 amp 0) in java

Shulou Source: shulou.com Published: 2022-06-01 02:30:10 09月30日 Update

This article mainly introduces what will be output of System.out.println (1.0 System.out.println 0) in java, which has certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article. Let's take a look at it.

Let's first try package erdan.demo;public class TestDouble {public static void main (String [] args) {System.out.println (1.0 / 0);}}

What you think, what I think, it should throw an ArithmeticException exception.

But it now outputs Infinity.

Why?

The Double data type supports infinity. Are there any other types that support it?

Yes, and Float. Let's take a look at the Double source code. We can see / * a constant that maintains the positive infinity of the type * / public static final double POSITIVE_INFINITY = 1.0 / 0.0X infinity * * a constant, and the negative infinity of the type * / public static final double NEGATIVE_INFINITY =-1.0 / 0.0 / * A constant, non-numeric type * / public static final double NaN = 0.0d / 0.0

Let's experiment with 0.0Univer and-1.0ppm.

Double positive infinity = 1.0 / 0 * * double negative infinity =-1.0 / 0

System.out.println ("positive infinity:" + positive infinity); System.out.println ("negative infinity:" + negative infinity); Double non-numeric value = 0.0amp 0; System.out.println ("non-numeric 0.0infinity 0->" + non-numeric value); output:

Positive infinity: Infinity negative infinity:-Infinity non-numeric 0.0Universe 0-> NaN

Operate on infinity next to test infinity of Float type and Doubloe type public static void testFloatInfinity () {Float infFloat = Float.POSITIVE_INFINITY; Double infDouble = Double.POSITIVE_INFINITY; System.out.println ("infFloat + 5 =" + (infFloat + 5)); System.out.println ("infFloat-infDouble =" + (infFloat-infDouble) System.out.println ("infFloat *-1 =" + (infFloat *-1));} output:

InfFloat + 5 = InfinityinfFloat-infDouble = NaNinfFloat *-1 =-Infinity can notice that the three-line operation is consistent with our expected results.

Ps: the result of Infinity- Infinity is not a numeric type

Judge these values

Public static void checkFloatInfinity () {Double positive infinity = 1.0 / 0; Double negative infinity =-1.0 / 0; Double non-numeric value = 0.0 / 0; System.out.println ("judge positive infinity:" + Double.isInfinite (positive infinity); System.out.println ("judge negative infinity:" + (Double.NEGATIVE_INFINITY = = negative infinity)) System.out.println ("determine non-numeric value (= =):" + (Double.NaN = = non-numeric value); System.out.println ("determine non-numerical value (isNaN):" + Double.isNaN (non-numeric value)) } output: judge positive infinity: true judge negative infinity: true judge non-numerical value (= =): false judge non-numerical value (isNaN): true thank you for reading this article carefully. I hope the article "what will be output of System.out.println (1.0cm 0) in java" shared by the editor will be helpful to you. At the same time, I hope you will support it. Pay attention to the industry information channel, more related knowledge is waiting for you to learn!

Tags: Values types output infinity articles constants support operations results values interests at the same time numbers data more friends source code knowledge tape industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei Shulou Information Linux Xiaomi