Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add arbitrary-precision arithmetic test to Java 1.1
  • Loading branch information
mperor committed Sep 27, 2026
commit 92b48de0d49b60862744100085c66254ec0fe7ec
22 changes: 22 additions & 0 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java1.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
Expand Down Expand Up @@ -192,4 +194,24 @@ public void testCharacterStreams() throws IOException {
}
}

@Test
public void testArbitraryPrecisionArithmetic() {
// Java 1.1 added java.math - double cannot represent most decimal fractions exactly, BigDecimal can
Assertions.assertNotEquals(0.3, 0.1 + 0.2);
Assertions.assertEquals(new BigDecimal("0.3"), new BigDecimal("0.1").add(new BigDecimal("0.2")));

// BigDecimal(double) keeps the binary approximation of the double - prefer the String constructor
Assertions.assertNotEquals(new BigDecimal("0.1"), new BigDecimal(0.1));
Assertions.assertEquals(new BigDecimal("0.1"), BigDecimal.valueOf(0.1)); // valueOf (Java 5) goes through Double.toString

// equals compares value & scale, while compareTo compares only the numerical value
Assertions.assertNotEquals(new BigDecimal("2.0"), new BigDecimal("2.00"));
Assertions.assertEquals(0, new BigDecimal("2.0").compareTo(new BigDecimal("2.00")));

// long silently overflows, while BigInteger has no fixed upper bound
Assertions.assertEquals(Long.MIN_VALUE, Long.MAX_VALUE + 1);
BigInteger beyondLong = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE);
Assertions.assertEquals(new BigInteger("9223372036854775808"), beyondLong);
}

}
Loading