Details
-
Type:
Sub-task
-
Status:
Open
-
Priority:
Major
-
Resolution: Unresolved
-
Affects Version/s: 1.7.4
-
Fix Version/s: None
-
Component/s: None
-
Labels:None
-
Number of attachments :
Description
The following test case executes successfully when compiled+executed as java. However it throws the exception "Cannot use mod() on this number type: java.math.BigDecimal with value: 10.7" when executed as groovy.
public class TesCastPrecedence { @org.junit.Test public void testCastWithMod() { long result = (long)10.7 % 3L; System.out.println(result); org.junit.Assert.assertEquals(result, 1L); } }
The issue with the precedence of casts and binary operations produces other unexpected behavior/exceptions. Consider the following two examples:
def percentage = 5.3
(int)Math.floor(percentage) + "%" //Should produce "5%", but causes GroovyCastException, as the integer cast gets applied to the string instead of the result of calling floor()
def someInt = Integer.MAX_VALUE
(long)someInt + someInt //Should produce 4294967294, but produces -2 (overflow)
In both cases, wrapping with parentheses solves the problem, but should not be necessary.