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 2017/05/02 02:04:19 UTC

[jira] [Closed] (GROOVY-7636) NumberMath.getMath suboptimal choice for custom Numbers

     [ https://issues.apache.org/jira/browse/GROOVY-7636?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Paul King closed GROOVY-7636.
-----------------------------

> 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
>            Assignee: Paul King
>            Priority: Minor
>             Fix For: 2.5.0-alpha-1
>
>
> 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.15#6346)