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 62a309652c IMAGINARY/IMREAL of a number without a real part; COMPLEX 
with -1 or 0 (#1444)
62a309652c is described below

commit 62a309652ca7ac115d8b15b5c870c5ceceecd5f7
Author: PJ Fanning <[email protected]>
AuthorDate: Sun Oct 18 11:29:54 2026 +0100

    IMAGINARY/IMREAL of a number without a real part; COMPLEX with -1 or 0 
(#1444)
    
    IMAGINARY("3i") returned 1 and IMREAL("3i") returned 3, because
    COMPLEX_NUMBER_PATTERN captures the sign and coefficient of a number
    without a real part, e.g. "-i" or "3i", in the real part groups.
    
    COMPLEX dropped the sign of an imaginary coefficient of -1, giving "3i"
    for COMPLEX(3,-1) and "i" for COMPLEX(0,-1), and returned "" rather
    than "0" for COMPLEX(0,0).
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../apache/poi/ss/formula/functions/Complex.java   |  7 ++++++-
 .../apache/poi/ss/formula/functions/ImReal.java    |  2 +-
 .../apache/poi/ss/formula/functions/Imaginary.java | 16 ++++++++++++++-
 .../poi/ss/formula/functions/TestComplex.java      | 23 ++++++++++++++++++++++
 4 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Complex.java 
b/poi/src/main/java/org/apache/poi/ss/formula/functions/Complex.java
index ee63080bb0..2637569610 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Complex.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Complex.java
@@ -118,12 +118,17 @@ public class Complex extends Var2or3ArgFunction 
implements FreeRefFunction {
                 }
             }
 
-            if (realINum != 1 && realINum != -1) {
+            if (realINum == -1) {
+                strb.append("-");
+            } else if (realINum != 1) {
                 strb.append(NumberToTextConverter.toText(realINum));
             }
 
             strb.append(suffixValue);
         }
+        if (strb.length() == 0) {
+            strb.append("0");
+        }
 
         return new StringEval(strb.toString());
     }
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/ImReal.java 
b/poi/src/main/java/org/apache/poi/ss/formula/functions/ImReal.java
index 26b59fd40b..7639b974ec 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/ImReal.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/ImReal.java
@@ -59,7 +59,7 @@ public class ImReal extends Fixed1ArgFunction implements 
FreeRefFunction {
         }
 
         String realGroup = groups[2];
-        if (realGroup.isEmpty()) {
+        if (realGroup.isEmpty() || Imaginary.isImaginaryOnly(groups)) {
             return new StringEval("0");
         }
 
diff --git 
a/poi/src/main/java/org/apache/poi/ss/formula/functions/Imaginary.java 
b/poi/src/main/java/org/apache/poi/ss/formula/functions/Imaginary.java
index 51bfc9859f..cff4eed5a8 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Imaginary.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Imaginary.java
@@ -79,11 +79,16 @@ public class Imaginary extends Fixed1ArgFunction implements 
FreeRefFunction {
             if (hasImaginaryPart) {
                 String sign = "";
                 String imaginarySign = groups[GROUP3_IMAGINARY_SIGN];
+                String groupImaginaryNumber = 
groups[GROUP4_IMAGINARY_INTEGER_OR_DOUBLE];
+                if (isImaginaryOnly(groups)) {
+                    // e.g. "-i" or "3i": the sign and coefficient were 
matched as the real part
+                    imaginarySign = groups[GROUP1_REAL_SIGN];
+                    groupImaginaryNumber = 
groups[GROUP2_IMAGINARY_INTEGER_OR_DOUBLE];
+                }
                 if (imaginarySign.length() != 0 && 
!(imaginarySign.equals("+"))) {
                     sign = imaginarySign;
                 }
 
-                String groupImaginaryNumber = 
groups[GROUP4_IMAGINARY_INTEGER_OR_DOUBLE];
                 if (groupImaginaryNumber.length() != 0) {
                     imaginary = sign + groupImaginaryNumber;
                 } else {
@@ -97,6 +102,15 @@ public class Imaginary extends Fixed1ArgFunction implements 
FreeRefFunction {
         return new StringEval(imaginary);
     }
 
+    /**
+     * Whether the groups from {@link #parseComplexNumber(String)} are a 
number without a real part, e.g. "-i" or "3i".
+     * The pattern then captures the sign and coefficient in the real part 
groups, and the imaginary ones are empty.
+     */
+    static boolean isImaginaryOnly(String[] groups) {
+        return !groups[5].isEmpty() && groups[GROUP3_IMAGINARY_SIGN].isEmpty()
+                && groups[GROUP4_IMAGINARY_INTEGER_OR_DOUBLE].isEmpty();
+    }
+
     /**
      * Gives the same result as matching {@link #COMPLEX_NUMBER_PATTERN}, 
without the regular expression:
      * its groups can split a run of digits in many ways, so matching text 
that is not a complex number
diff --git 
a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestComplex.java 
b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestComplex.java
index 3c1bb4413b..015f6fb0bc 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestComplex.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestComplex.java
@@ -51,6 +51,9 @@ class TestComplex {
 
         confirmValue("Complex number with 0 and 1 as the real and imaginary 
coefficients (i)", "0","1", "", "i");
         confirmValue("Complex number with 1 and 0 as the real and imaginary 
coefficients (1)", "1","0", "", "1");
+        confirmValue("Complex number with 0 and -1 as the real and imaginary 
coefficients (-i)", "0","-1", "", "-i");
+        confirmValue("Complex number with 3 and -1 as the real and imaginary 
coefficients (3-i)", "3","-1", "", "3-i");
+        confirmValue("Complex number with 0 and 0 as the real and imaginary 
coefficients (0)", "0","0", "", "0");
 
         confirmValue("Complex number with 2 and 3 as the real and imaginary 
coefficients (2 + 3i)", "2","3", "", "2+3i");
         confirmValue("Complex number with -2 and -3 as the real and imaginary 
coefficients (-2-3i)", "-2","-3", "", "-2-3i");
@@ -58,6 +61,26 @@ class TestComplex {
         confirmValue("Complex number with -2 and -3 as the real and imaginary 
coefficients (-0.5-3.2i)", "-0.5","-3.2", "", "-0.5-3.2i");
     }
 
+    /**
+     * IMAGINARY and IMREAL must read back what COMPLEX produces, including a 
number without
+     * a real part such as "-i" or "3i", whose coefficient the regex captures 
as the real part
+     */
+    @Test
+    void testImaginaryAndImRealOfComplex() {
+        double[][] parts = {{3, 4}, {3, -1}, {-2, -3}, {-0.5, -3.2}, {1, 0}, 
{0, 0},
+                {0, 1}, {0, -1}, {0, 3}, {0, -3}, {0, 2.5}, {0, -0.5}};
+        for (double[] part : parts) {
+            for (String suffix : new String[]{"i", "j"}) {
+                ValueEval complex = invokeValue(String.valueOf(part[0]), 
String.valueOf(part[1]), suffix);
+                String text = ((StringEval) complex).getStringValue();
+                ValueEval real = new ImReal().evaluate(-1, -1, complex);
+                ValueEval imaginary = new Imaginary().evaluate(-1, -1, 
complex);
+                assertEquals(part[0], Double.parseDouble(((StringEval) 
real).getStringValue()), 0, "IMREAL(\"" + text + "\")");
+                assertEquals(part[1], Double.parseDouble(((StringEval) 
imaginary).getStringValue()), 0, "IMAGINARY(\"" + text + "\")");
+            }
+        }
+    }
+
     @Test
     void testErrors() {
         confirmValueError("argument is nonnumeric", "ABCD", "","", 
ErrorEval.VALUE_INVALID);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to