details: http://code.openbravo.com/erp/devel/pi/rev/217112210a53 changeset: 3443:217112210a53 user: Stefan Hühner <stefan.huehner <at> openbravo.com> date: Tue Mar 24 12:40:34 2009 +0100 summary: Fixed 8177: change xmlEngine calculations from using double -> BigDecimal
diffstat: src-core/src/org/openbravo/xmlEngine/FunctionAddValue.java | 12 ++-- src-core/src/org/openbravo/xmlEngine/FunctionDivideValue.java | 25 +++++--- src-core/src/org/openbravo/xmlEngine/FunctionEqualValue.java | 8 +- src-core/src/org/openbravo/xmlEngine/FunctionGtValue.java | 6 +- src-core/src/org/openbravo/xmlEngine/FunctionLtValue.java | 6 +- src-core/src/org/openbravo/xmlEngine/FunctionMaxValue.java | 10 ++- src-core/src/org/openbravo/xmlEngine/FunctionMedValue.java | 33 ++++++----- src-core/src/org/openbravo/xmlEngine/FunctionMinValue.java | 10 ++- src-core/src/org/openbravo/xmlEngine/FunctionModuleValue.java | 30 +++++---- src-core/src/org/openbravo/xmlEngine/FunctionMultiplyValue.java | 12 ++-- src-core/src/org/openbravo/xmlEngine/FunctionSubtractValue.java | 12 ++-- src-core/src/org/openbravo/xmlEngine/FunctionSumValue.java | 14 ++-- src-core/src/org/openbravo/xmlEngine/FunctionTemplate.java | 9 +- 13 files changed, 104 insertions(+), 83 deletions(-) diffs (truncated from 513 to 300 lines): diff -r 25b73760644e -r 217112210a53 src-core/src/org/openbravo/xmlEngine/FunctionAddValue.java --- a/src-core/src/org/openbravo/xmlEngine/FunctionAddValue.java Tue Mar 24 12:19:14 2009 +0100 +++ b/src-core/src/org/openbravo/xmlEngine/FunctionAddValue.java Tue Mar 24 12:40:34 2009 +0100 @@ -11,6 +11,8 @@ */ package org.openbravo.xmlEngine; +import java.math.BigDecimal; + import org.apache.log4j.Logger; class FunctionAddValue extends FunctionEvaluationValue { @@ -26,9 +28,8 @@ || arg2Value.printSimple().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - return functionTemplate.printFormatOutput(Double.valueOf(arg1Value.printSimple()) - .doubleValue() - + Double.valueOf(arg2Value.printSimple()).doubleValue()); + return functionTemplate.printFormatOutput(new BigDecimal(arg1Value.printSimple()) + .add(new BigDecimal(arg2Value.printSimple()))); } } @@ -37,9 +38,8 @@ || arg2Value.printSimple().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - return functionTemplate.printFormatSimple(Double.valueOf(arg1Value.printSimple()) - .doubleValue() - + Double.valueOf(arg2Value.printSimple()).doubleValue()); + return functionTemplate.printFormatSimple(new BigDecimal(arg1Value.printSimple()) + .add(new BigDecimal(arg2Value.printSimple()))); } } diff -r 25b73760644e -r 217112210a53 src-core/src/org/openbravo/xmlEngine/FunctionDivideValue.java --- a/src-core/src/org/openbravo/xmlEngine/FunctionDivideValue.java Tue Mar 24 12:19:14 2009 +0100 +++ b/src-core/src/org/openbravo/xmlEngine/FunctionDivideValue.java Tue Mar 24 12:40:34 2009 +0100 @@ -11,6 +11,9 @@ */ package org.openbravo.xmlEngine; +import java.math.BigDecimal; +import java.math.RoundingMode; + import org.apache.log4j.Logger; class FunctionDivideValue extends FunctionEvaluationValue { @@ -29,12 +32,13 @@ || arg2Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - double division = Double.valueOf(arg1Value.printSimple()).doubleValue() - / Double.valueOf(arg2Value.printSimple()).doubleValue(); - if (Double.isInfinite(division) || Double.isNaN(division)) { + // divide uses exception when divisor=0 so catch and wrap with strTextDividedByZero + try { + BigDecimal division = new BigDecimal(arg1Value.printSimple()).divide(new BigDecimal( + arg2Value.printSimple()), 12, RoundingMode.HALF_UP); + return functionTemplate.printFormatOutput(division); + } catch (ArithmeticException a) { return XmlEngine.strTextDividedByZero; - } else { - return functionTemplate.printFormatOutput(division); } } } @@ -47,12 +51,13 @@ || arg2Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - double division = Double.valueOf(arg1Value.printSimple()).doubleValue() - / Double.valueOf(arg2Value.printSimple()).doubleValue(); - if (Double.isInfinite(division) || Double.isNaN(division)) { + // divide uses exception when divisor=0 so catch and wrap with strTextDividedByZero + try { + BigDecimal division = new BigDecimal(arg1Value.printSimple()).divide(new BigDecimal( + arg2Value.printSimple()), 12, RoundingMode.HALF_UP); + return functionTemplate.printFormatSimple(division); + } catch (ArithmeticException a) { return XmlEngine.strTextDividedByZero; - } else { - return functionTemplate.printFormatSimple(division); } } } diff -r 25b73760644e -r 217112210a53 src-core/src/org/openbravo/xmlEngine/FunctionEqualValue.java --- a/src-core/src/org/openbravo/xmlEngine/FunctionEqualValue.java Tue Mar 24 12:19:14 2009 +0100 +++ b/src-core/src/org/openbravo/xmlEngine/FunctionEqualValue.java Tue Mar 24 12:40:34 2009 +0100 @@ -11,6 +11,8 @@ */ package org.openbravo.xmlEngine; +import java.math.BigDecimal; + import org.apache.log4j.Logger; class FunctionEqualValue extends FunctionEvaluationValue { @@ -25,8 +27,7 @@ if (arg1Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - return functionTemplate.printFormatOutput(Double.valueOf(arg1Value.printSimple()) - .doubleValue()); + return functionTemplate.printFormatOutput(new BigDecimal(arg1Value.printSimple())); } } @@ -34,8 +35,7 @@ if (arg1Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - return functionTemplate.printFormatSimple(Double.valueOf(arg1Value.printSimple()) - .doubleValue()); + return functionTemplate.printFormatSimple(new BigDecimal(arg1Value.printSimple())); } } diff -r 25b73760644e -r 217112210a53 src-core/src/org/openbravo/xmlEngine/FunctionGtValue.java --- a/src-core/src/org/openbravo/xmlEngine/FunctionGtValue.java Tue Mar 24 12:19:14 2009 +0100 +++ b/src-core/src/org/openbravo/xmlEngine/FunctionGtValue.java Tue Mar 24 12:40:34 2009 +0100 @@ -11,6 +11,8 @@ */ package org.openbravo.xmlEngine; +import java.math.BigDecimal; + import org.apache.log4j.Logger; class FunctionGtValue extends FunctionEvaluationValue { @@ -28,8 +30,8 @@ || arg2Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - if (Double.valueOf(arg1Value.printSimple()).doubleValue() > Double.valueOf( - arg2Value.printSimple()).doubleValue()) { + if (new BigDecimal(arg1Value.printSimple()) + .compareTo(new BigDecimal(arg2Value.printSimple())) > 0) { return "1"; } else { return "0"; diff -r 25b73760644e -r 217112210a53 src-core/src/org/openbravo/xmlEngine/FunctionLtValue.java --- a/src-core/src/org/openbravo/xmlEngine/FunctionLtValue.java Tue Mar 24 12:19:14 2009 +0100 +++ b/src-core/src/org/openbravo/xmlEngine/FunctionLtValue.java Tue Mar 24 12:40:34 2009 +0100 @@ -11,6 +11,8 @@ */ package org.openbravo.xmlEngine; +import java.math.BigDecimal; + import org.apache.log4j.Logger; class FunctionLtValue extends FunctionEvaluationValue { @@ -28,8 +30,8 @@ || arg2Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - if (Double.valueOf(arg1Value.printSimple()).doubleValue() < Double.valueOf( - arg2Value.printSimple()).doubleValue()) { + if (new BigDecimal(arg1Value.printSimple()) + .compareTo(new BigDecimal(arg2Value.printSimple())) < 0) { return "1"; } else { return "0"; diff -r 25b73760644e -r 217112210a53 src-core/src/org/openbravo/xmlEngine/FunctionMaxValue.java --- a/src-core/src/org/openbravo/xmlEngine/FunctionMaxValue.java Tue Mar 24 12:19:14 2009 +0100 +++ b/src-core/src/org/openbravo/xmlEngine/FunctionMaxValue.java Tue Mar 24 12:40:34 2009 +0100 @@ -11,6 +11,8 @@ */ package org.openbravo.xmlEngine; +import java.math.BigDecimal; + import org.apache.log4j.Logger; class FunctionMaxValue extends FunctionEvaluationValue { @@ -26,8 +28,8 @@ || arg2Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - return functionTemplate.printFormatOutput(Math.max(Double.valueOf(arg1Value.printSimple()) - .doubleValue(), Double.valueOf(arg2Value.printSimple()).doubleValue())); + return functionTemplate.printFormatOutput(new BigDecimal(arg1Value.printSimple()) + .max(new BigDecimal(arg2Value.printSimple()))); } } @@ -36,8 +38,8 @@ || arg2Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - return functionTemplate.printFormatSimple(Math.max(Double.valueOf(arg1Value.printSimple()) - .doubleValue(), Double.valueOf(arg2Value.printSimple()).doubleValue())); + return functionTemplate.printFormatSimple(new BigDecimal(arg1Value.printSimple()) + .max(new BigDecimal(arg2Value.printSimple()))); } } diff -r 25b73760644e -r 217112210a53 src-core/src/org/openbravo/xmlEngine/FunctionMedValue.java --- a/src-core/src/org/openbravo/xmlEngine/FunctionMedValue.java Tue Mar 24 12:19:14 2009 +0100 +++ b/src-core/src/org/openbravo/xmlEngine/FunctionMedValue.java Tue Mar 24 12:40:34 2009 +0100 @@ -8,14 +8,17 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. ************************************************************************************ - */ +*/ package org.openbravo.xmlEngine; -import org.apache.log4j.Logger; +import java.math.BigDecimal; +import java.math.RoundingMode; + +import org.apache.log4j.Logger ; class FunctionMedValue extends FunctionValue { int count; - double sum; + BigDecimal sum; static Logger log4jFunctionMedValue = Logger.getLogger(FunctionMedValue.class); @@ -24,30 +27,30 @@ } public String print() { - if (functionTemplate.formatOutput != null) { - return functionTemplate.formatOutput.format(sum / count); - } else { - return Double.toString(sum / count); + try { + return functionTemplate.printFormatOutput(sum.divide(new BigDecimal(count), 12, RoundingMode.HALF_UP)); + } catch (ArithmeticException a) { + return XmlEngine.strTextDividedByZero; } } public String printSimple() { - if (functionTemplate.formatSimple != null) { - return functionTemplate.formatSimple.format(sum / count); - } else { - return Double.toString(sum / count); + try { + return functionTemplate.printFormatSimple(sum.divide(new BigDecimal(count), 12, RoundingMode.HALF_UP)); + } catch (ArithmeticException a) { + return XmlEngine.strTextDividedByZero; } } public void acumulate() { - count++; - if (!fieldValue.print().equals("")) { - sum += Double.valueOf(fieldValue.printSimple()).doubleValue(); + count ++; + if (fieldValue.print() != "") { + sum = sum.add(new BigDecimal(fieldValue.printSimple())); } } public void init() { - sum = 0; + sum = BigDecimal.ZERO; count = 0; } diff -r 25b73760644e -r 217112210a53 src-core/src/org/openbravo/xmlEngine/FunctionMinValue.java --- a/src-core/src/org/openbravo/xmlEngine/FunctionMinValue.java Tue Mar 24 12:19:14 2009 +0100 +++ b/src-core/src/org/openbravo/xmlEngine/FunctionMinValue.java Tue Mar 24 12:40:34 2009 +0100 @@ -11,6 +11,8 @@ */ package org.openbravo.xmlEngine; +import java.math.BigDecimal; + import org.apache.log4j.Logger; class FunctionMinValue extends FunctionEvaluationValue { @@ -26,8 +28,8 @@ || arg2Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - return functionTemplate.printFormatOutput(Math.min(Double.valueOf(arg1Value.printSimple()) - .doubleValue(), Double.valueOf(arg2Value.printSimple()).doubleValue())); + return functionTemplate.printFormatOutput(new BigDecimal(arg1Value.printSimple()) + .min(new BigDecimal(arg2Value.printSimple()))); } } @@ -36,8 +38,8 @@ || arg2Value.print().equals(XmlEngine.strTextDividedByZero)) { return XmlEngine.strTextDividedByZero; } else { - return functionTemplate.printFormatSimple(Math.min(Double.valueOf(arg1Value.printSimple()) - .doubleValue(), Double.valueOf(arg2Value.printSimple()).doubleValue())); + return functionTemplate.printFormatSimple(new BigDecimal(arg1Value.printSimple()) + .min(new BigDecimal(arg2Value.printSimple()))); } } ------------------------------------------------------------------------------ Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits