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:

will return true rather than false (using float or double types in Java returns a result of 1.2000000000000002).

## 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.

_*Type*_ |_*Suffix*_

_*BigInteger*_ |G

_*Long*_ |L

_*Integer*_ |I

_*BigDecimal*_ |G

_*Double*_ |D

_*Float*_ |F

Examples:

## 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

(section 7.4).

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).

_ |
_ |
_ |
_ |
_ |
_ |
BigDecimal |
BigDecimal |
Double |
Double |
BigDecimal |
BigDecimal |
BigDecimal |
BigInteger |
Double |
Double |
BigInteger |
BigInteger |
Double |
Double |
Double |
Double |
Double |
Double |
Double |
Double |
Double |
Double |
Double |
Double |
BigDecimal |
BigInteger |
Double |
Double |
Long |
Long |
BigDecimal |
BigInteger |
Double |
Double |
Long |
Integer |

*Note* - Byte, Character, and Short arguments are considered to be Integer types for the purposes of this matrix.

### Division

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:

where <scale> is MAX(this.scale(), right.scale(), 10).Finally, the resulting BigDecimal is normalized (trailing zeros are removed).

For example:

Integer division can be performed on the integral types by casting the result of the division. For example:

Future versions of Groovy may support an integer division operator such as div and/or ÷.

### Power Operator

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).

Here is a Java code:

Here is a Groovy code: