You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (JIRA)" <ji...@apache.org> on 2019/02/26 22:54:00 UTC

[jira] [Commented] (GROOVY-9011) (2/1).equals(2) is false; but (2/1) == (2) is true

    [ https://issues.apache.org/jira/browse/GROOVY-9011?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16778663#comment-16778663 ] 

Paul King commented on GROOVY-9011:
-----------------------------------

This is expected behavior. In Groovy, {{2 / 1}} is {{BigDecimal}} 2. This allows equations like {{0.5 == 1/2}} to be true in Groovy even though it is false in most other languages. The {{equals}} method for {{BigDecimal}} however follows Java conventions and returns false for anything that isn't also a {{BigDecimal}} (and then needs the same value and precision/scale). Groovy's {{==}} operator uses {{compareTo}} for numbers but we don't attempt to override {{equals}}. It's not wise to override that method. You can instead try:
{code}
println 2.intdiv(1).equals(2)
println ((2.0/1).equals(2.0))
{code}


> (2/1).equals(2) is false; but (2/1) == (2) is true
> --------------------------------------------------
>
>                 Key: GROOVY-9011
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9011
>             Project: Groovy
>          Issue Type: Bug
>          Components: syntax
>         Environment: Linux, Ubuntu 18.04
>            Reporter: Adithyan K
>            Priority: Major
>
> Is it an expected behavior??
> Difference between '==' and 'equals()' observed....
>  
> {{adithyan@adithyan:~$ *groovy -e 'println( (2/1).equals(2) )'* }}
> {{{color:#14892c}*false* {color}}}
> {{adithyan@adithyan:~$ *groovy -e 'println( (2/1) == (2) )'*         }}
> {{{color:#14892c}*true*{color}}}
> {{adithyan@adithyan:~$ *groovy -v* }}
> {{*{color:#14892c}Groovy Version: 2.4.15 JVM: 1.8.0_144 Vendor: Oracle Corporation OS: Linux{color}*}}
>  
>  
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)