Groovy as a substitute for Java when using BigDecimal?

Posted by geejay on Stack Overflow See other posts from Stack Overflow or by geejay
Published on 2010-04-23T15:10:23Z Indexed on 2010/04/23 15:13 UTC
Read the original article Hit count: 251

Filed under:
|
|
|
|

I have just completed an evaluation of Java, Groovy and Scala.

The factors I considered were: readability, precision

The factors I would like to know: performance, ease of integration

I needed a BigDecimal level of precision.

Here are my results:

Java

void someOp()
{
    BigDecimal del_theta_1 = toDec(6);
    BigDecimal del_theta_2 = toDec(2);
    BigDecimal del_theta_m = toDec(0);

    del_theta_m = abs(del_theta_1.subtract(del_theta_2))
      .divide(log(del_theta_1.divide(del_theta_2)));

}

Groovy

void someOp()
{
 def del_theta_1 = 6.0
 def del_theta_2 = 2.0
 def del_theta_m = 0.0

 del_theta_m = Math.abs(del_theta_1 - del_theta_2) / Math.log(del_theta_1 / del_theta_2);
}

Scala

def other(){
 var del_theta_1 = toDec(6);
 var del_theta_2 = toDec(2);
 var del_theta_m = toDec(0);
 del_theta_m = (
  abs(del_theta_1 - del_theta_2)  
  / log(del_theta_1 / del_theta_2)
 )
}

Note that in Java and Scala I used static imports.

Java: Pros: it is Java
Cons: no operator overloading (lots o methods), barely readable/codeable

Groovy: Pros: default BigDecimal means no visible typing, least surprising BigDecimal support for all operations (division included)
Cons: another language to learn

Scala: Pros: has operator overloading for BigDecimal
Cons: some surprising behaviour with division (fixed with Decimal128), another language to learn

© Stack Overflow or respective owner

Related posts about java

Related posts about groovy