[ https://issues.apache.org/jira/browse/GROOVY-7636?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14963439#comment-14963439 ]
ASF GitHub Bot commented on GROOVY-7636: ---------------------------------------- GitHub user tkruse opened a pull request: https://github.com/apache/incubator-groovy/pull/151 GROOVY-7636: Fall back to BigDecimalMath for custom Numbers You can merge this pull request into a Git repository by running: $ git pull https://github.com/tkruse/incubator-groovy groovy-7636-choose-math Alternatively you can review and apply these changes as the patch at: https://github.com/apache/incubator-groovy/pull/151.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #151 ---- commit 181b7ad7117ec6c95edde10bc8d1f3d34586f162 Author: Thibault Kruse <thibault.kr...@gmx.de> Date: 2015-10-19T15:05:56Z GROOVY-7636: Fall back to BigDecimalMath for custom Number implementations ---- > NumberMath.getMath suboptimal choice for custom Numbers > ------------------------------------------------------- > > Key: GROOVY-7636 > URL: https://issues.apache.org/jira/browse/GROOVY-7636 > Project: Groovy > Issue Type: Bug > Components: groovy-jdk > Reporter: Thibault Kruse > Priority: Minor > > When implementing the java.lang.Number interface with a custom class, the > groovy NumberMath class file's instanceof check do not find a math, and it > defaults to IntegerMath. I believe the default should be BigDecimal for > best-effort compatibility. > Also when comparing BigDecimal to a true Float or Double, FloatingPointMath > is used, not BigDecimalMath. > to reproduce: > {code} > static class MyNumber extends Number { > def n > MyNumber(n) { > this.n = n > } > int intValue(){n} > long longValue(){n} > float floatValue(){n} > double doubleValue(){n} > int hashCode(){-n} > boolean equals(other) { > if (other instanceof MyNumber) { return n==other.n} > return false > } > String toString() {return Double.toString(floatValue())} > } > void testGetMathCustom() { > //assert getMath(50.0G, Float.valueOf("1.0")) == > BigDecimalMath.INSTANCE; > //assert getMath(50G, Float.valueOf("1.0")) == > BigDecimalMath.INSTANCE; > MyNumber num = new MyNumber(42); > assert getMath(num) == BigDecimalMath.INSTANCE; > assert getMath(num, 25) == BigDecimalMath.INSTANCE; > } > {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332)