You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/10/28 08:11:03 UTC

[groovy] branch GROOVY_4_0_X updated: GROOVY-10786: Cannot call modulo operator (%) on BigDecimal types

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new 662210ed30 GROOVY-10786: Cannot call modulo operator (%) on BigDecimal types
662210ed30 is described below

commit 662210ed30705f76149a18b86f8d11e280553121
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Oct 27 16:26:03 2022 +1000

    GROOVY-10786: Cannot call modulo operator (%) on BigDecimal types
---
 .../runtime/typehandling/BigDecimalMath.java       |  5 ++
 .../groovy/operator/BigDecimalOperatorsTest.groovy | 19 ++++--
 .../operator/BigIntegerOperationsTest.groovy       | 23 +++++++
 ...tionTest.groovy => DoubleOperationsTest.groovy} | 71 ++++++++++++----------
 4 files changed, 82 insertions(+), 36 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/runtime/typehandling/BigDecimalMath.java b/src/main/java/org/codehaus/groovy/runtime/typehandling/BigDecimalMath.java
index dd14405d17..6e3ce620ed 100644
--- a/src/main/java/org/codehaus/groovy/runtime/typehandling/BigDecimalMath.java
+++ b/src/main/java/org/codehaus/groovy/runtime/typehandling/BigDecimalMath.java
@@ -91,4 +91,9 @@ public final class BigDecimalMath extends NumberMath {
     protected Number unaryPlusImpl(Number left) {
         return toBigDecimal(left);
     }
+
+    @Override
+    protected Number modImpl(Number left, Number right) {
+        return toBigDecimal(left).remainder(toBigDecimal(right));
+    }
 }
diff --git a/src/test/groovy/operator/BigDecimalOperatorsTest.groovy b/src/test/groovy/operator/BigDecimalOperatorsTest.groovy
index de6fb2b0cf..0d75eb6c09 100644
--- a/src/test/groovy/operator/BigDecimalOperatorsTest.groovy
+++ b/src/test/groovy/operator/BigDecimalOperatorsTest.groovy
@@ -47,7 +47,7 @@ class BigDecimalOperatorsTest extends GroovyTestCase {
     }
 
     void testMinus() {
-        x = 1.1-0.01
+        x = 1.1 - 0.01
         assert x == 1.09
 
         x = 6 - 2.2
@@ -98,6 +98,16 @@ class BigDecimalOperatorsTest extends GroovyTestCase {
         assert y == 11.33333333333 , "y = " + y
     }
 
+    void testMod() {
+        x = 100.0 % 3
+
+        assert x == 1
+
+        y = 5.5
+        y %= 2.0
+        assert y == 1.5
+    }
+
     BigDecimal echoX ( BigDecimal x, BigDecimal y) {x}
 
     // test for Groovy-1250
@@ -146,14 +156,14 @@ class BigDecimalOperatorsTest extends GroovyTestCase {
     // -------------------------------------------------------
     // GROOVY-5102
     // we need both variants, since one seems to disable prim opts
-    public void testMath1() {
+    void testMath1() {
         assert BigDecimal == (3/2).getClass()
         assert BigDecimal == (7.0/8.0).getClass()
         assert BigDecimal == (new BigDecimal(3.0)/new BigDecimal(2.0)).getClass()
         true
     }
 
-    public void testMath2() {
+    void testMath2() {
         assert BigDecimal == (3/2).getClass()
         assert BigDecimal == (7.0/8.0).getClass()
         assert BigDecimal == (new BigDecimal(3.0)/new BigDecimal(2.0)).getClass()
@@ -161,7 +171,7 @@ class BigDecimalOperatorsTest extends GroovyTestCase {
     // -------------------------------------------------------
 
     // GROOVY-5173
-    public void testBDPrimOptFields() {
+    void testBDPrimOptFields() {
         assertScript """
             class BigDecimalBug {
 
@@ -180,5 +190,4 @@ class BigDecimalOperatorsTest extends GroovyTestCase {
         """
     }
 
-
 }
diff --git a/src/test/groovy/operator/BigIntegerOperationsTest.groovy b/src/test/groovy/operator/BigIntegerOperationsTest.groovy
index 273ef218b6..e54a7b47a9 100644
--- a/src/test/groovy/operator/BigIntegerOperationsTest.groovy
+++ b/src/test/groovy/operator/BigIntegerOperationsTest.groovy
@@ -21,6 +21,8 @@ package groovy.operator
 import groovy.test.GroovyTestCase
 
 class BigIntegerOperationsTest extends GroovyTestCase {
+    def x, y
+
     void testAssign() {
         BigInteger foo
         foo = (byte) 20
@@ -59,6 +61,27 @@ class BigIntegerOperationsTest extends GroovyTestCase {
         assert (BigInteger) d == d
     }
 
+    void testPlus() {
+        x = 2G + 3G
+        assert x instanceof BigInteger
+        assert x == 5G
+    }
+
+    void testMultiply() {
+        x = 2G * 3G
+        assert x instanceof BigInteger
+        assert x == 6G
+    }
+
+    void testMod() {
+        x = 100G % 3
+        assert x == 1G
+
+        y = 11G
+        y %= 3
+        assert y == 2G
+    }
+
     void testAsOperatorPrecisionLoss() {
         def value = BigInteger.valueOf(Long.MAX_VALUE) + 1
         def value2 = value as BigInteger
diff --git a/src/test/groovy/operator/DoubleOperationTest.groovy b/src/test/groovy/operator/DoubleOperationsTest.groovy
similarity index 63%
rename from src/test/groovy/operator/DoubleOperationTest.groovy
rename to src/test/groovy/operator/DoubleOperationsTest.groovy
index 5829ee937b..cc9fac3bf9 100644
--- a/src/test/groovy/operator/DoubleOperationTest.groovy
+++ b/src/test/groovy/operator/DoubleOperationsTest.groovy
@@ -20,61 +20,70 @@ package groovy.operator
 
 import groovy.test.GroovyTestCase
 
-class DoubleOperationTest extends GroovyTestCase {
+class DoubleOperationsTest extends GroovyTestCase {
 
     def x
     def y
 
     void testPlus() {
-        x = 2.1 + 2.1
-        assert x == 4.2
+        x = 2.1d + 2.1d
+        assert x == 4.2d
 
-        x = 3 + 2.2
-        assert x == 5.2
+        x = 3d + 2.2d
+        assert x == 5.2d
 
-        x = 2.2 + 4
-        assert x == 6.2
+        x = 2.2d + 4d
+        assert x == 6.2d
 
-        y = x + 1
-        assert y == 7.2
+        y = x + 1d
+        assert y == 7.2d
 
-        def z = y + x + 1 + 2
-        assert z == 16.4
+        def z = y + x + 1d + 2d
+        assert z == 16.4d
     }
 
     void testMinus() {
-        x = 6 - 2.2
-        assert x == 3.8
+        x = 6d - 2.2d
+        assert x == 3.8d
 
-        x = 5.8 - 2
-        assert x == 3.8
+        x = 5.8d - 2d
+        assert x == 3.8d
 
-        y = x - 1
-        assert y == 2.8
+        y = x - 1d
+        assert y == 2.8d
     }
 
     void testMultiply() {
-        x = 3 * 2.0
-        assert x == 6.0
+        x = 3d * 2.0d
+        assert x == 6.0d
 
-        x = 3.0 * 2
-        assert x == 6.0
+        x = 3.0d * 2d
+        assert x == 6.0d
 
-        x = 3.0 * 2.0
-        assert x == 6.0
-        y = x * 2
-        assert y == 12.0
+        x = 3.0d * 2.0d
+        assert x == 6.0d
+        y = x * 2d
+        assert y == 12.0d
     }
 
     void testDivide() {
-        x = 80.0 / 4
-        assert x == 20.0, "x = " + x
+        x = 80.0d / 4d
+        assert x == 20.0d, "x = " + x
 
-        x = 80 / 4.0
-        assert x == 20.0, "x = " + x
+        x = 80d / 4.0d
+        assert x == 20.0d, "x = " + x
 
-        y = x / 2
-        assert y == 10.0, "y = " + y
+        y = x / 2d
+        assert y == 10.0d, "y = " + y
+    }
+
+    void testMod() {
+        x = 100d % 3
+        assert x == 1d
+
+        y = 11d
+        y %= 3d
+        assert y == 2d
     }
 
     void testMethodNotFound() {