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. To do this, groovy uses exact, or decimal math for default calculations.
This means that user computations like:
will return true rather than false (using float or double types in Java returns a result of 1.2000000000000002).
h2 Numeric literals
To support the 'least surprising' approach, groovy literals with decimal points are instantiated as java.math.BigDecimal types rather than binary floating point types (Float, Double). Float and Double types can of course be created explicitly or via the use of a suffix (see table below). Exponential notation is supported for decimal types (BigDecimal, Double Float) with or without a signed
exponent (1.23e-23). Hexadecimal and octal literals are also supported. Hexadecimal numbers are specified
in the typical format of "0x" followed by hex digits (e.g. 0x77).
Integral numeric literals (those without a decimal point) which begin with a 0 are treated as octal. Both octal and hexadecimal literals may have an integral suffix (G,L,I). Integral numeric literals without a suffix will be the smallest type into which the value will fit (Integer, Long, or BigInteger). See the numeric literal grammar at the end of this page for more details on syntax.
h2 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.
Finally, Groovy's math implementation is as close as practical to the Java 1.5 BigDecimal math model which implements precision based floating point decimal math (ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000
Therefore, binary operations involving subclasses of java.lang.Number automatically convert their arguments according to the following matrix (except for division, which is discussed below).
Note 1 - Byte, Character, and Short arguments are considered to be Integer types for the purposes of this matrix.
Note 2 - Division (/) produces 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:
where <scale> is MAX(this.scale(), right.scale(), 10).Finally, the resulting BigDecimal is normalized (trailing zeros are removed).
h2 Numeric literal grammar
IntegerTypeSuffix: one of
i I l L g G
NonZeroDigit: one of
\1 2 3 4 5 6 7 8 9
0 x HexDigits
0 X 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
OctalDigit: one of
0 1 2 3 4 5 6 7
Digits . Digits ExponentPart_(optional)_ DecimalTypeSuffix_(optional)_
. Digits ExponentPart_(optional)_ DecimalTypeSuffix_(optional)_
Digits ExponentPart DecimalTypeSuffix_(optional)_
Digits ExponentPart_(optional)_ DecimalTypeSuffix_(optional)_
ExponentIndicator: one of
Sign: one of
DecimalTypeSuffix: one of
f F d D g G