Groovy supports access to all Java math classes and operations. However, in order to make scripting math operations as intuitive as possible to the end user, the groovy math model supports a 'least surprising' approach to literal math operations for script programmers. To do this, groovy uses exact, or decimal math for default calculations.
This means that user computations like:
| Code Block |
|---|
1.1 + 0.1 == 1.2
|
will return true rather than false (using float or double types in Java returns a result of 1.2000000000000002).
...
Type | Suffix |
|---|---|
BigInteger | G |
Long | L |
Integer | I |
BigDecimal | G |
Double | D |
Float | F |
Examples:
| Code Block |
|---|
assert 42I == new Integer("42"); assert 123L == new Long("123"); assert 2147483648 == new Long("2147483648"); //Long type used, value too large for an Integer assert 456G == new java.math.BigInteger("456"); assert 123.45 == new java.math.BigDecimal("123.45"); //default BigDecimal type used assert 1.200065D == new Double("1.200065"); assert 1.234F == new Float("1.234"); assert 1.23E23D == new Double("1.23E23"); |
Math operations
While the default behavior is to use decimal math, no attempt is made to preserve this if a binary floating point number is introduced into an expression (i.e. groovy never automatically promotes a binary floating point number to a BigDecimal). This is done for two reasons: First, doing so would imply a level of exactness to a result that is not guaranteed to be exact, and secondly, performance is slightly better under binary floating point math, so once it is introduced it is kept.
...
The division operators "/" and "/=" produce a Double result if either operand is either Float or Double and a BigDecimal result otherwise (both operands are any combination of Integer, Long, BigInteger, or BigDecimal). BigDecimal Division is performed as follows:
| Code Block | ||
|---|---|---|
| ||
BigDecimal.divide(BigDecimal right, <scale>, BigDecimal.ROUND_HALF_UP)
|
where <scale> is MAX(this.scale(), right.scale(), 10).Finally, the resulting BigDecimal is normalized (trailing zeros are removed).
For example:
| Code Block |
|---|
1/2 == new java.math.BigDecimal("0.5"); 1/3 == new java.math.BigDecimal("0.3333333333"); 2/3 == new java.math.BigDecimal("0.6666666667"); |
Integer division can be performed on the integral types by casting the result of the division. For example:
| Code Block |
|---|
assert (int)(3/2) == 1I; |
Future versions of Groovy may support an integer division operator such as div and/or ÷.
...
Since groovy 1.0 beta 10 release, the power operator "**" is supported for math calculation.
For example, 5**3 equals to Math.pow(5,3).
Java code:
| Code Block | ||
|---|---|---|
| ||
// y = 2 x^3 + 5 x^2 - 3 x + 2 defdouble x = 5.0; defdouble y = 2.0 * Math.pow(x,3) + 5.0 * Math.pow(x,2) - 3.0*x + 2.0; |
Groovy code:
| Code Block |
|---|
// y = 2 x^3 + 5 x^2 - 3 x + 2 def x = 5.0; def y = 2.0*x**3 + 5.0*x**2 - 3.0*x + 2.0 |
More In-depth Information
...
Numeric literal grammar
| Code Block |
|---|
IntegerLiteral: Base10IntegerLiteral HexIntegerLiteral OctalIntegerLiteral Base10IntegerLiteral: Base10Numeral IntegerTypeSuffix (optional) HexIntegerLiteral: HexNumeral IntegerTypeSuffix (optional) OctalIntegerLiteral: OctalNumeral IntegerTypeSuffix (optional) IntegerTypeSuffix: one of i I l L g G Base10Numeral: 0 NonZeroDigit Digits (optional) Digits: Digit Digits Digit Digit: 0 NonZeroDigit NonZeroDigit: one of \1 2 3 4 5 6 7 8 9 HexNumeral: 0 x HexDigits 0 X HexDigits HexDigits: HexDigit HexDigit HexDigits HexDigit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F OctalNumeral: 0 OctalDigits OctalDigits: OctalDigit OctalDigit OctalDigits OctalDigit: one of 0 1 2 3 4 5 6 7 DecimalPointLiteral: Digits . Digits ExponentPart (optional) DecimalTypeSuffix (optional) . Digits ExponentPart (optional) DecimalTypeSuffix (optional) Digits ExponentPart DecimalTypeSuffix (optional) Digits ExponentPart (optional) DecimalTypeSuffix (optional) ExponentPart: ExponentIndicator SignedInteger ExponentIndicator: one of e E SignedInteger: Signopt Digits Sign: one of + - DecimalTypeSuffix: one of f F d D g G |