Infinity


The java.lang.Float class provides two constants for infinity: Float.NEGATIVE_INFINITY and Float.POSITIVE_INFINITY. The class java.lang.Double provides corresponding constants.

While integral division by zero results in an error condition, double and float operations involving zero division result in the mathematically correct infinity value. The following assertions demonstrate use of the infinity constants.

 final float tolerance = 0.5f; final float x = 1f; assertEquals(    Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY * 100, tolerance); assertEquals(Float.NEGATIVE_INFINITY,    Float.POSITIVE_INFINITY * -1, tolerance); assertEquals(Float.POSITIVE_INFINITY, x / 0f, tolerance); assertEquals(Float.NEGATIVE_INFINITY, x / -0f, tolerance); assertTrue(Float.isNaN(x % 0f)); assertEquals(0f, x / Float.POSITIVE_INFINITY, tolerance); assertEquals(-0f, x / Float.NEGATIVE_INFINITY, tolerance); assertEquals(x, x % Float.POSITIVE_INFINITY, tolerance); assertTrue(Float.isNaN(0f / 0f)); assertTrue(Float.isNaN(0f % 0f)); assertEquals(    Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY / x, tolerance); assertEquals(    Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY / x, tolerance); assertTrue(Float.isNaN(Float.POSITIVE_INFINITY % x)); assertTrue(    Float.isNaN(Float.POSITIVE_INFINITY / Float.POSITIVE_INFINITY)); assertTrue(    Float.isNaN(Float.POSITIVE_INFINITY % Float.POSITIVE_INFINITY)); assertTrue(    Float.isNaN(Float.POSITIVE_INFINITY / Float.NEGATIVE_INFINITY)); assertTrue(    Float.isNaN(Float.POSITIVE_INFINITY % Float.NEGATIVE_INFINITY)); assertTrue(    Float.isNaN(Float.NEGATIVE_INFINITY / Float.POSITIVE_INFINITY)); assertTrue(    Float.isNaN(Float.NEGATIVE_INFINITY % Float.POSITIVE_INFINITY)); assertTrue(    Float.isNaN(Float.NEGATIVE_INFINITY / Float.NEGATIVE_INFINITY)); assertTrue(    Float.isNaN(Float.NEGATIVE_INFINITY % Float.NEGATIVE_INFINITY)); 



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net