You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2020/04/16 12:18:39 UTC
[commons-numbers] 24/26: Fraction: Add tests for Integer.MIN_VALUE
in intValue()
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit ccd5e31a6cf67cc061a4f53abc3920492e3ce5b5
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Apr 15 22:10:46 2020 +0100
Fraction: Add tests for Integer.MIN_VALUE in intValue()
The intValue cannot be done by simple integer divide arithmetic:
numerator / denominator
This will return Integer.MIN_VALUE for the fraction Integer.MIN_VALUE /
-1. The correct result is Integer.MAX_VALUE (as the fraction is
positive).
integer divide arithmetic can be used in longValue().
---
.../java/org/apache/commons/numbers/fraction/Fraction.java | 6 ++++--
.../java/org/apache/commons/numbers/fraction/FractionTest.java | 10 ++++++++++
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
index e9b7199..8a23872 100644
--- a/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
+++ b/commons-numbers-fraction/src/main/java/org/apache/commons/numbers/fraction/Fraction.java
@@ -444,7 +444,7 @@ public final class Fraction
* Returns the {@code float} value closest to this fraction.
* This calculates the fraction as numerator divided by denominator.
*
- * @return the fraction as {@code float}.
+ * @return the fraction as a {@code float}.
*/
@Override
public float floatValue() {
@@ -458,6 +458,8 @@ public final class Fraction
*/
@Override
public int intValue() {
+ // Note: numerator / denominator fails for Integer.MIN_VALUE / -1.
+ // Casting the double value handles this case.
return (int) doubleValue();
}
@@ -468,7 +470,7 @@ public final class Fraction
*/
@Override
public long longValue() {
- return (long) doubleValue();
+ return (long) numerator / denominator;
}
/**
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
index 798d6fb..80628fa 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
@@ -244,6 +244,11 @@ public class FractionTest {
Assertions.assertEquals(-1, Fraction.of(-3, 2).intValue());
Assertions.assertEquals(-1, Fraction.of(3, -2).intValue());
+ Assertions.assertEquals(0, Fraction.of(1, Integer.MIN_VALUE).intValue());
+ Assertions.assertEquals(0, Fraction.of(-1, Integer.MIN_VALUE).intValue());
+ Assertions.assertEquals(Integer.MIN_VALUE, Fraction.of(Integer.MIN_VALUE, 1).intValue());
+ Assertions.assertEquals(Integer.MAX_VALUE, Fraction.of(Integer.MIN_VALUE, -1).intValue());
+
Assertions.assertEquals(0, ZERO_P.intValue());
Assertions.assertEquals(0, ZERO_N.intValue());
}
@@ -260,6 +265,11 @@ public class FractionTest {
Assertions.assertEquals(-1L, Fraction.of(-3, 2).longValue());
Assertions.assertEquals(-1L, Fraction.of(3, -2).longValue());
+ Assertions.assertEquals(0, Fraction.of(1, Integer.MIN_VALUE).longValue());
+ Assertions.assertEquals(0, Fraction.of(-1, Integer.MIN_VALUE).longValue());
+ Assertions.assertEquals(Integer.MIN_VALUE, Fraction.of(Integer.MIN_VALUE, 1).longValue());
+ Assertions.assertEquals(Integer.MAX_VALUE + 1L, Fraction.of(Integer.MIN_VALUE, -1).longValue());
+
Assertions.assertEquals(0L, ZERO_P.longValue());
Assertions.assertEquals(0L, ZERO_N.longValue());
}