You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by wg...@apache.org on 2005/04/12 03:17:09 UTC

svn commit: r160997 - in jakarta/velocity/trunk: src/java/org/apache/velocity/runtime/parser/node/ src/java/org/apache/velocity/test/ test/templates/ test/templates/compare/

Author: wglass
Date: Mon Apr 11 18:17:07 2005
New Revision: 160997

URL: http://svn.apache.org/viewcvs?view=rev&rev=160997
Log:
division of integers now produces an integer.  added unit tests.  fix typo of method substract for good measure.

Modified:
    jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTSubtractNode.java
    jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/MathUtils.java
    jakarta/velocity/trunk/src/java/org/apache/velocity/test/ArithmeticTestCase.java
    jakarta/velocity/trunk/test/templates/compare/arithmetic.cmp
    jakarta/velocity/trunk/test/templates/compare/math.cmp
    jakarta/velocity/trunk/test/templates/math.vm

Modified: jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTSubtractNode.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTSubtractNode.java?view=diff&r1=160996&r2=160997
==============================================================================
--- jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTSubtractNode.java (original)
+++ jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTSubtractNode.java Mon Apr 11 18:17:07 2005
@@ -107,7 +107,7 @@
             return null;
         }
 
-        return MathUtils.substract ( (Number)left,(Number)right);
+        return MathUtils.subtract ( (Number)left,(Number)right);
     }
 }
 

Modified: jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/MathUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/MathUtils.java?view=diff&r1=160996&r2=160997
==============================================================================
--- jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/MathUtils.java (original)
+++ jakarta/velocity/trunk/src/java/org/apache/velocity/runtime/parser/node/MathUtils.java Mon Apr 11 18:17:07 2005
@@ -283,7 +283,7 @@
      * Subtract two numbers and return the correct value / type.
      * Overflow detection is done for integer values (byte, short, int, long) only!
      */
-    public static Number substract (Number op1, Number op2) {
+    public static Number subtract (Number op1, Number op2) {
 
         int calcBase = findCalculationBase( op1, op2);
         switch (calcBase) {
@@ -342,9 +342,9 @@
     }
 
     /**
-     * Divide two numbers. Integer-types will be returned as Integer-type if and only if
-     * the modulo of the two numbers is 0. Otherwise a Double will be returned. The same
-     * for BigIntegers.
+     * Divide two numbers. The result will be returned as Integer-type if and only if
+     * both sides of the division operator are Integer-types. Otherwise a Float, Double, 
+     * or BigDecimal will be returned. 
      */
     public static Number divide (Number op1, Number op2) {
 
@@ -353,17 +353,13 @@
             case BASE_BIGINTEGER:
                 BigInteger b1 = toBigInteger( op1 );
                 BigInteger b2 = toBigInteger( op2 );
-                if (b1.mod( b2).compareTo( INT_ZERO) == 0) {
-                    return b1.divide( b2);
-                }
-                return toBigDecimal( op1 ).divide( toBigDecimal ( op2 ), BigDecimal.ROUND_HALF_DOWN);
+                return b1.divide( b2);
+
             case BASE_LONG:
                 long l1 = op1.longValue();
                 long l2 = op2.longValue();
-                if (l1 % l2 == 0) {
-                    return wrapPrimitive( l1 / l2, op1, op2);
-                }
-                return new Double ((double)l1 / l2);
+                return wrapPrimitive( l1 / l2, op1, op2);
+
             case BASE_FLOAT:
                 return new Float (op1.floatValue()/op2.floatValue());
             case BASE_DOUBLE:

Modified: jakarta/velocity/trunk/src/java/org/apache/velocity/test/ArithmeticTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/src/java/org/apache/velocity/test/ArithmeticTestCase.java?view=diff&r1=160996&r2=160997
==============================================================================
--- jakarta/velocity/trunk/src/java/org/apache/velocity/test/ArithmeticTestCase.java (original)
+++ jakarta/velocity/trunk/src/java/org/apache/velocity/test/ArithmeticTestCase.java Mon Apr 11 18:17:07 2005
@@ -82,7 +82,7 @@
 
     private void subtractHelper (Number n1, Number n2, double expectedResult, Class expectedResultType) 
     {
-        Number result = MathUtils.substract( n1, n2);
+        Number result = MathUtils.subtract( n1, n2);
         assertEquals ("The arithmetic operation produced an unexpected result.", expectedResult, result.doubleValue(), 0.01);
         assertEquals ("ResultType does not match.", expectedResultType, result.getClass());
     }
@@ -115,7 +115,7 @@
         divideHelper (new Integer(10), new Short( (short)2), 5, Integer.class);
         divideHelper (new Byte((byte)10), new Short( (short)2), 5, Short.class);
         divideHelper (BigInteger.valueOf(10), new Short( (short)2), 5, BigInteger.class);
-        divideHelper (new Integer(10), new Short( (short)4), 2.5, Double.class);
+        divideHelper (new Integer(10), new Short( (short)4), 2, Integer.class);
         divideHelper (new Integer(10), new Float( 2.5f), 4, Float.class);
         divideHelper (new Integer(10), new Double( 2.5), 4, Double.class);
         divideHelper (new Integer(10), new BigDecimal( 2.5), 4, BigDecimal.class);

Modified: jakarta/velocity/trunk/test/templates/compare/arithmetic.cmp
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/test/templates/compare/arithmetic.cmp?view=diff&r1=160996&r2=160997
==============================================================================
--- jakarta/velocity/trunk/test/templates/compare/arithmetic.cmp (original)
+++ jakarta/velocity/trunk/test/templates/compare/arithmetic.cmp Mon Apr 11 18:17:07 2005
@@ -28,7 +28,7 @@
 
 100000000001
 
-2.5
+2
 
 
 This is a very long string that we are breaking up into multiple lines for testing.

Modified: jakarta/velocity/trunk/test/templates/compare/math.cmp
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/test/templates/compare/math.cmp?view=diff&r1=160996&r2=160997
==============================================================================
--- jakarta/velocity/trunk/test/templates/compare/math.cmp (original)
+++ jakarta/velocity/trunk/test/templates/compare/math.cmp Mon Apr 11 18:17:07 2005
@@ -8,7 +8,7 @@
 5 % 2 = 1
 5 % 0 = $rem2
 7 % 2 = 1
-5 / 2 = 2.5
+5 / 2 = 2
 5 / 0 = $rem4
 5 * 2 = 10
 
@@ -22,7 +22,7 @@
 1000 + 10000000000 = 10000001000
 1000 - 10000000000 = -9999999000
 1000 * 10000000000 = 10000000000000
-1000 / 10000000000 = 1.0E-7
+1000 / 10000000000 = 0
 1000 % 10000000000 = 1000
 
 1000 + 1000.1234 = 2000.1234
@@ -35,3 +35,10 @@
 1000 - 999.125 = 0.875
 1000 * 999.125 = 999125.0
 1000 / 999.125 = 1.0008757662955086
+
+Test integer division
+5 / 2 = 2
+
+Test decimal division
+5 / 2.0 = 2.5
+5.0 / 2 = 2.5
\ No newline at end of file

Modified: jakarta/velocity/trunk/test/templates/math.vm
URL: http://svn.apache.org/viewcvs/jakarta/velocity/trunk/test/templates/math.vm?view=diff&r1=160996&r2=160997
==============================================================================
--- jakarta/velocity/trunk/test/templates/math.vm (original)
+++ jakarta/velocity/trunk/test/templates/math.vm Mon Apr 11 18:17:07 2005
@@ -53,3 +53,10 @@
 $int1 - $templatenumber1.AsNumber = #set ($rem = $int1 - $templatenumber1)$rem
 $int1 * $templatenumber1.AsNumber = #set ($rem = $int1 * $templatenumber1)$rem
 $int1 / $templatenumber1.AsNumber = #set ($rem = $int1 / $templatenumber1)$rem
+
+Test integer division
+5 / 2 = #set($result = 5 / 2)$result
+
+Test decimal division
+5 / 2.0 = #set($result = 5 / 2.0)$result
+5.0 / 2 = #set($result = 5.0 / 2)$result
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org