Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

Groovy and Java Math is explained in far more depth in these pages:

         Groovy Integer Math

         Groovy BigDecimal Math

         Groovy Floating Point Math

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

...