Github user viirya commented on the pull request:

    https://github.com/apache/spark/pull/7198#issuecomment-118281070
  
    I have run some tests about this. It is interesting. The exception is 
thrown when java.math.BigDecimal is constructed with special String, e.g. 
    
        val x = BigDecimal(BigInt("18889465931478580854784"), -2147483648)
        new 
java.math.BigDecimal(x.bigDecimal.round(MathContext.UNLIMITED).toString)
        java.lang.NumberFormatException
                at java.math.BigDecimal.<init>(BigDecimal.java:554)
                at java.math.BigDecimal.<init>(BigDecimal.java:383)
                at java.math.BigDecimal.<init>(BigDecimal.java:806)
    
    `Decimal(x).toBigDecimal` actually is calling 
`scala.math.BigDecimal.apply(mc: MathContext)` which is defined as `def 
apply(mc: MathContext): BigDecimal = new BigDecimal(this.bigDecimal round mc, 
mc)`.
    
    We can successfully run this:
    
        new BigDecimal(x.bigDecimal.round(MathContext.UNLIMITED), 
MathContext.UNLIMITED)
        res59: scala.math.BigDecimal = 1.8889465931478580854784E+2147483670
    
    But if we do this, the exception will be thrown:
     
        new BigDecimal(new 
java.math.BigDecimal(x.bigDecimal.round(MathContext.UNLIMITED).toString), 
MathContext.UNLIMITED)
        java.lang.NumberFormatException
                at java.math.BigDecimal.<init>(BigDecimal.java:554)
                at java.math.BigDecimal.<init>(BigDecimal.java:383)
                at java.math.BigDecimal.<init>(BigDecimal.java:806)
    
    Because `x.bigDecimal.round(MathContext.UNLIMITED)` is already a 
`java.math.BigDecimal`, currently I am not sure why it will construct another 
`java.math.BigDecimal` with the string version of rounded value when calling 
`scala.math.BigDecimal.apply(mc: MathContext)`.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to