Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>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.<br /> This means that user computations like:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>1.1 + 0.1 == 1.2 </pre></td></tr></table><p>will return true rather than false (using float or double types in Java returns a result of 1.2000000000000002).</p><h2>Numeric literals</h2><p>To support the 'least surprising' approach, groovy literals with decimal points are instantiated as <em>java.math.BigDecimal</em> 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<br /> exponent (1.23e-23). Hexadecimal and octal literals are also supported. Hexadecimal numbers are specified<br /> in the typical format of "0x" followed by hex digits (e.g. 0x77).</p><p>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.</p><table class="confluenceTable"><tbody><tr><th class="confluenceTh"><p>Type</p></th><th class="confluenceTh"><p>Suffix</p></th></tr><tr><td class="confluenceTd"><p>BigInteger</p></td><td class="confluenceTd"><p>G</p></td></tr><tr><td class="confluenceTd"><p>Long</p></td><td class="confluenceTd"><p>L</p></td></tr><tr><td class="confluenceTd"><p>Integer</p></td><td class="confluenceTd"><p>I</p></td></tr><tr><td class="confluenceTd"><p>BigDecimal</p></td><td class="confluenceTd"><p>G</p></td></tr><tr><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>D</p></td></tr><tr><td class="confluenceTd"><p>Float</p></td><td class="confluenceTd"><p>F</p></td></tr></tbody></table><p>Examples:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>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") </pre></td></tr></table><h2>Math operations</h2><p>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.</p><p>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<br /> (section 7.4).</p><p>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).</p><table class="confluenceTable"><tbody><tr><th class="confluenceTh"><p> </p></th><th class="confluenceTh"><p>BigDecimal</p></th><th class="confluenceTh"><p>BigInteger</p></th><th class="confluenceTh"><p>Double</p></th><th class="confluenceTh"><p>Float</p></th><th class="confluenceTh"><p>Long</p></th><th class="confluenceTh"><p>Integer</p></th></tr><tr><td class="confluenceTd"><p><strong>BigDecimal</strong></p></td><td class="confluenceTd"><p>BigDecimal</p></td><td class="confluenceTd"><p>BigDecimal</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>BigDecimal</p></td><td class="confluenceTd"><p>BigDecimal</p></td></tr><tr><td class="confluenceTd"><p><strong>BigInteger</strong></p></td><td class="confluenceTd"><p>BigDecimal</p></td><td class="confluenceTd"><p>BigInteger</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>BigInteger</p></td><td class="confluenceTd"><p>BigInteger</p></td></tr><tr><td class="confluenceTd"><p><strong>Double</strong></p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td></tr><tr><td class="confluenceTd"><p><strong>Float</strong></p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td></tr><tr><td class="confluenceTd"><p><strong>Long</strong></p></td><td class="confluenceTd"><p>BigDecimal</p></td><td class="confluenceTd"><p>BigInteger</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Long</p></td><td class="confluenceTd"><p>Long</p></td></tr><tr><td class="confluenceTd"><p><strong>Integer</strong></p></td><td class="confluenceTd"><p>BigDecimal</p></td><td class="confluenceTd"><p>BigInteger</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Double</p></td><td class="confluenceTd"><p>Long</p></td><td class="confluenceTd"><p>Integer</p></td></tr></tbody></table><p><span style="text-decoration: underline;">Note</span> - Byte, Character, and Short arguments are considered to be Integer types for the purposes of this matrix.</p><h3>Division</h3><p>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:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>BigDecimal.divide(BigDecimal right, <scale>, BigDecimal.ROUND_HALF_UP) </pre></td></tr></table><p>where <scale> is MAX(this.scale(), right.scale(), 10).Finally, the resulting BigDecimal is normalized (trailing zeros are removed).<br /> For example:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>1/2 == new java.math.BigDecimal("0.5") 1/3 == new java.math.BigDecimal("0.3333333333") 2/3 == new java.math.BigDecimal("0.6666666667")</pre></td></tr></table><p>Integer division can be performed on the integral types by casting the result of the division. For example:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>assert (int)(3/2) == 1I </pre></td></tr></table><p>Future versions of Groovy may support an integer division operator such as div and/or ÷.</p><h3>Power Operator</h3><p>Since groovy 1.0 beta 10 release, the power operator "**" is supported for math calculation. <br class="atl-forced-newline" /> For example, 5**3 equals to Math.pow(5,3).</p><p>Java code:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>// y = 2 x^3 + 5 x^2 - 3 x + 2 double x = 5.0; double y = 2.0 * Math.pow(x,3) + 5.0 * Math.pow(x,2) - 3.0*x + 2.0; </pre></td></tr></table><p>Groovy code:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>// 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 </pre></td></tr></table><p> </p><h2>More In-depth Information</h2><p>Groovy and Java Math is explained in more depth in these pages:</p><p> <a href="http://groovy.codehaus.org/JN0515-Integers">Integer Math</a></p><p> <a href="http://groovy.codehaus.org/JN0525-Decimals">Decimal Math</a></p><p> <a href="http://groovy.codehaus.org/JN0535-Floats">Floating Point Math</a></p><h2>Numeric literal grammar</h2><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>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 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 </pre></td></tr></table>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced