poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r1135079 - in /poi/trunk/src: documentation/content/xdocs/ java/org/apache/poi/ss/formula/functions/ testcases/org/apache/poi/ss/formula/functions/ testcases/org/apache/poi/ss/usermodel/
Date Mon, 13 Jun 2011 10:34:43 GMT
Author: yegor
Date: Mon Jun 13 10:34:43 2011
New Revision: 1135079

URL: http://svn.apache.org/viewvc?rev=1135079&view=rev
Log:
Bug 51339 - Fixed arithmetic rounding in formula evaluation

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/ss/formula/functions/MathX.java
    poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1135079&r1=1135078&r2=1135079&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Mon Jun 13 10:34:43 2011
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta4" date="2011-??-??">
+           <action dev="poi-developers" type="add">51339 - Fixed arithmetic rounding
in formula evaluation </action>
            <action dev="poi-developers" type="add">51356 - Support autoSizeColumn in
SXSSF</action>
            <action dev="poi-developers" type="add">51335 - Parse picture goal and crop
sizes in HWPF</action>
            <action dev="poi-developers" type="add">51305 - Add sprmTCellPaddingDefault
support in HWPF</action>

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/functions/MathX.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/MathX.java?rev=1135079&r1=1135078&r2=1135079&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/MathX.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/MathX.java Mon Jun 13 10:34:43
2011
@@ -53,13 +53,7 @@ final class MathX {
             retval = Double.NaN;
         }
         else {
-            if (p != 0) {
-                double temp = Math.pow(10, p);
-                retval = Math.round(n*temp)/temp;
-            }
-            else {
-                retval = Math.round(n);
-            }
+            retval = java.math.BigDecimal.valueOf(n).setScale(p, java.math.RoundingMode.HALF_UP).doubleValue();
         }
 
         return retval;
@@ -87,22 +81,7 @@ final class MathX {
             retval = Double.NaN;
         }
         else {
-            if (p != 0) {
-                double temp = Math.pow(10, p);
-                double nat = Math.abs(n*temp);
-
-                retval = sign(n) *
-                    ((nat == (long) nat)
-                            ? nat / temp
-                            : Math.round(nat + 0.5) / temp);
-            }
-            else {
-                double na = Math.abs(n);
-                retval = sign(n) *
-                    ((na == (long) na)
-                        ? na
-                        : (long) na + 1);
-            }
+            retval = java.math.BigDecimal.valueOf(n).setScale(p, java.math.RoundingMode.UP).doubleValue();
         }
 
         return retval;
@@ -130,13 +109,7 @@ final class MathX {
             retval = Double.NaN;
         }
         else {
-            if (p != 0) {
-                double temp = Math.pow(10, p);
-                retval = sign(n) * Math.round((Math.abs(n)*temp) - 0.5)/temp;
-            }
-            else {
-                retval = (long) n;
-            }
+            retval = java.math.BigDecimal.valueOf(n).setScale(p, java.math.RoundingMode.DOWN).doubleValue();
         }
 
         return retval;

Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java?rev=1135079&r1=1135078&r2=1135079&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestMathX.java Mon Jun 13
10:34:43 2011
@@ -672,6 +672,9 @@ public class TestMathX extends AbstractN
         
         d = 150.0; p = -2;
         assertEquals("round ", 200, MathX.round(d, p));
+
+        d = 2162.615d; p = 2;
+        assertEquals("round ", 2162.62d, MathX.round(d, p));
     }
 
     public void testRoundDown() {

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java?rev=1135079&r1=1135078&r2=1135079&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java Mon
Jun 13 10:34:43 2011
@@ -281,4 +281,23 @@ public abstract class BaseTestFormulaEva
         assertEquals(3.5, cellB1.getNumericCellValue(), 0.0);
     }
 
+
+    public void testRounding_bug51339() {
+        Workbook wb = _testDataProvider.createWorkbook();
+        Sheet sheet = wb.createSheet("Sheet1");
+        Row row = sheet.createRow(0);
+        Cell cellA1 = row.createCell(0);
+        cellA1.setCellValue(2162.615d);
+        Cell cellB1 = row.createCell(1);
+        cellB1.setCellFormula("round(a1,2)");
+        Cell cellC1 = row.createCell(2);
+        cellC1.setCellFormula("roundup(a1,2)");
+        Cell cellD1 = row.createCell(3);
+        cellD1.setCellFormula("rounddown(a1,2)");
+        FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
+
+        assertEquals(2162.62, fe.evaluateInCell(cellB1).getNumericCellValue(), 0.0);
+        assertEquals(2162.62, fe.evaluateInCell(cellC1).getNumericCellValue(), 0.0);
+        assertEquals(2162.61, fe.evaluateInCell(cellD1).getNumericCellValue(), 0.0);
+    }
 }
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message