BAEL-5733 Infinity in Java (#12843)
* Add deep vs shallow copy Java. * Refactor sheep names. * BAEL-5733 Add infinity in java test. * Removed test article code. * BAEL-5733 Refactor unit test classname.
This commit is contained in:
parent
c68ee78e45
commit
25a1270d7c
|
@ -8,3 +8,4 @@ This module contains articles about core features in the Java language
|
|||
- [Advantages and Disadvantages of Using Java Wildcard Imports](https://www.baeldung.com/java-wildcard-imports)
|
||||
- [Toggle a Boolean Variable in Java](https://www.baeldung.com/java-toggle-boolean)
|
||||
- [Handle Classes With the Same Name in Java](https://www.baeldung.com/java-classes-same-name)
|
||||
- [Infinity in Java](https://www.baeldung.com/java-infinity)
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
package com.baeldung.infinity;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class DoubleInfinityUnitTest {
|
||||
|
||||
@Test
|
||||
void givenInfinities_whenOperatingWithThem_thenNaNOrInfinity() {
|
||||
Double positiveInfinity = Double.POSITIVE_INFINITY;
|
||||
Double negativeInfinity = Double.NEGATIVE_INFINITY;
|
||||
|
||||
assertEquals(Double.NaN, (positiveInfinity + negativeInfinity));
|
||||
assertEquals(Double.NaN, (positiveInfinity / negativeInfinity));
|
||||
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeInfinity));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveInfinity));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeInfinity));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInfinityAndPositiveNumber_whenOperatingWithThem_thenInfinity() {
|
||||
Double positiveInfinity = Double.POSITIVE_INFINITY;
|
||||
Double negativeInfinity = Double.NEGATIVE_INFINITY;
|
||||
double positiveNumber = 10.0;
|
||||
|
||||
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + positiveNumber));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + positiveNumber));
|
||||
|
||||
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - positiveNumber));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveNumber));
|
||||
|
||||
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity * positiveNumber));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity * positiveNumber));
|
||||
|
||||
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity / positiveNumber));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity / positiveNumber));
|
||||
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (positiveNumber - positiveInfinity));
|
||||
assertEquals(Double.POSITIVE_INFINITY, (positiveNumber - negativeInfinity));
|
||||
|
||||
assertEquals(0.0, (positiveNumber / positiveInfinity));
|
||||
assertEquals(-0.0, (positiveNumber / negativeInfinity));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInfinityAndBegativeNumber_whenOperatingWithThem_thenInfinity() {
|
||||
Double positiveInfinity = Double.POSITIVE_INFINITY;
|
||||
Double negativeInfinity = Double.NEGATIVE_INFINITY;
|
||||
double negativeNumber = -10.0;
|
||||
|
||||
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + negativeNumber));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + negativeNumber));
|
||||
|
||||
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeNumber));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - negativeNumber));
|
||||
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeNumber));
|
||||
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity * negativeNumber));
|
||||
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity / negativeNumber));
|
||||
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity / negativeNumber));
|
||||
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (negativeNumber - positiveInfinity));
|
||||
assertEquals(Double.POSITIVE_INFINITY, (negativeNumber - negativeInfinity));
|
||||
|
||||
assertEquals(-0.0, (negativeNumber / positiveInfinity));
|
||||
assertEquals(0.0, (negativeNumber / negativeInfinity));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void givenRealNumbers_whenDivisionByZero_thenInfinity() {
|
||||
double d = 1.0;
|
||||
|
||||
assertEquals(Double.POSITIVE_INFINITY, (d / 0.0));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (d / -0.0));
|
||||
assertEquals(Double.NEGATIVE_INFINITY, (-d / 0.0));
|
||||
assertEquals(Double.POSITIVE_INFINITY, (-d / -0.0));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue