This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 14fa73c18b CellFormat: allow an improper fraction's numerator up to an
unsigned 32-bit int (#1494)
14fa73c18b is described below
commit 14fa73c18b859bf1b920f7c156913bde7ed259e4
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Oct 26 20:17:24 2026 +0000
CellFormat: allow an improper fraction's numerator up to an unsigned 32-bit
int (#1494)
Excel stops working out an improper fraction before its numerator exceeds
an unsigned 32-bit int: =TEXT(300000.707106781, "?????????/?????????") is
2435705741/8119. POI stopped at the int limit instead and gave
1008902378/3363. The fraction is now worked out for the part after the
whole number, with the improper numerator capped at 2^32 - 1.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../apache/poi/ss/format/CellNumberFormatter.java | 11 ++++++++++
.../org/apache/poi/ss/format/SimpleFraction.java | 24 ++++++++++++++++++++--
.../org/apache/poi/ss/format/TestCellFormat.java | 2 --
3 files changed, 33 insertions(+), 4 deletions(-)
diff --git
a/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
b/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
index f84da27c5e..a7c84ebf3d 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/CellNumberFormatter.java
@@ -43,6 +43,8 @@ import org.apache.poi.util.LocaleUtil;
*/
public class CellNumberFormatter extends CellFormatter {
private static final Logger LOG =
PoiLogManager.getLogger(CellNumberFormatter.class);
+ // the largest numerator Excel shows for an improper fraction, the maximum
unsigned 32-bit int
+ private static final long MAX_IMPROPER_NUMERATOR = 0xFFFFFFFFL;
private final String desc;
private final String printfFmt;
@@ -686,6 +688,15 @@ public class CellNumberFormatter extends CellFormatter {
// a fixed denominator is always used, without reducing the
fraction (2/4 stays 2/4)
d = fixedDenominator;
n = Math.toIntExact(Math.round(fractional * d));
+ } else if (improperFraction && wholeNumber == null) {
+ // Excel stops before an improper fraction's numerator exceeds an
unsigned 32-bit int:
+ // 300000.707106781 is 2435705741/8119, where the next fraction
would be 5880013860/19601
+ double floor = Math.floor(fractional);
+ wholeNumber = BigInteger.valueOf((long) floor);
+ SimpleFraction frac =
SimpleFraction.buildFractionMaxDenominator(fractional - floor, maxDenominator +
1,
+ (long) floor, MAX_IMPROPER_NUMERATOR);
+ n = frac.getNumerator();
+ d = frac.getDenominator();
} else {
// buildFractionMaxDenominator only gives denominators below the
maximum it is passed, so pass
// 10^digits, as FractionFormat does: e.g. "??/??" shows 0.99 as
98/99, where 99 would give 1/1
diff --git a/poi/src/main/java/org/apache/poi/ss/format/SimpleFraction.java
b/poi/src/main/java/org/apache/poi/ss/format/SimpleFraction.java
index 9c584c9ef7..27c251b9ef 100644
--- a/poi/src/main/java/org/apache/poi/ss/format/SimpleFraction.java
+++ b/poi/src/main/java/org/apache/poi/ss/format/SimpleFraction.java
@@ -56,7 +56,20 @@ public class SimpleFraction {
* @throws IllegalArgumentException if value > Integer.MAX_VALUE
*/
public static SimpleFraction buildFractionMaxDenominator(double value, int
maxDenominator){
- return buildFractionMaxDenominator(value, 0, maxDenominator, 100);
+ return buildFractionMaxDenominator(value, 0, maxDenominator, 100, 0,
Long.MAX_VALUE);
+ }
+
+ /**
+ * Create a fraction for the part of an improper fraction after the whole
number, stopping before the
+ * numerator of the improper fraction, {@code wholeNumber * denominator +
numerator}, exceeds a maximum.
+ *
+ * @param value the part after the whole number, from 0 up to (not
including) 1
+ * @param maxDenominator maximum denominator value allowed.
+ * @param wholeNumber the whole number, which must be below 2^31
+ * @param maxNumerator maximum numerator allowed for the improper fraction
+ */
+ static SimpleFraction buildFractionMaxDenominator(double value, int
maxDenominator, long wholeNumber, long maxNumerator) {
+ return buildFractionMaxDenominator(value, 0, maxDenominator, 100,
wholeNumber, maxNumerator);
}
/**
* Create a fraction given the double value and either the maximum error
@@ -76,11 +89,14 @@ public class SimpleFraction {
* {@code epsilon} of {@code value}, in absolute terms.
* @param maxDenominator maximum denominator value allowed.
* @param maxIterations maximum number of convergents
+ * @param wholeNumber the whole number of an improper fraction that {@code
value} is the rest of
+ * @param maxNumerator maximum value of {@code wholeNumber * denominator +
numerator}
* @throws IllegalStateException if the continued fraction failed to
* converge.
* @throws IllegalArgumentException if value > Integer.MAX_VALUE
*/
- private static SimpleFraction buildFractionMaxDenominator(double value,
double epsilon, int maxDenominator, int maxIterations)
+ private static SimpleFraction buildFractionMaxDenominator(double value,
double epsilon, int maxDenominator, int maxIterations,
+ long wholeNumber, long maxNumerator)
{
long overflow = Integer.MAX_VALUE;
double r0 = value;
@@ -121,6 +137,10 @@ public class SimpleFraction {
return new SimpleFraction(Math.toIntExact(p1),
Math.toIntExact(q1));
}
+ // wholeNumber * q2 + p2 > maxNumerator, without overflowing a long
+ if (wholeNumber > 0 && q2 > (maxNumerator - p2) / wholeNumber) {
+ return new SimpleFraction(Math.toIntExact(p1),
Math.toIntExact(q1));
+ }
if ((Math.abs(p2) > overflow) || (q2 > overflow)) {
if (epsilon == 0.0f && maxDenominator > 0) {
// an improper fraction's numerator can overflow before
the denominator reaches its maximum
diff --git a/poi/src/test/java/org/apache/poi/ss/format/TestCellFormat.java
b/poi/src/test/java/org/apache/poi/ss/format/TestCellFormat.java
index 06297fff84..c48b24efed 100644
--- a/poi/src/test/java/org/apache/poi/ss/format/TestCellFormat.java
+++ b/poi/src/test/java/org/apache/poi/ss/format/TestCellFormat.java
@@ -50,7 +50,6 @@ import org.apache.poi.util.LocaleUtil;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
-import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Isolated;
@@ -975,7 +974,6 @@ class TestCellFormat {
assertEquals("2100000000/1", cf.apply(2100000000.12345).text.trim());
}
- @Disabled("POI stops at 1008902378/3363 because the numerator overflows an
int before the denominator reaches 8119")
@Test
void testImproperFractionNumeratorOverflowExcelDenominator() {
// Excel's =TEXT(300000.707106781, "?????????/?????????") gives
2435705741/8119
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]