Groovy as a substitute for Java when using BigDecimal? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this questionI 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/codeableGroovy: Pros: default BigDecimal means no visible typing, least surprising BigDecimal support for all operations (division included)
Cons: another language to learnScala: Pros: has operator overloading for BigDecimal
Cons: some surprising behaviour with division (fixed with Decimal128), another language to learnThe core of BigDecimal is the same in each language. If the limiting factors for performance are the mathematical operations in BigDecimal, then there will be no difference between the three languages, because they all use java.math.BigDecimal.
Therefore, use the language which is easiest to read and understand (which is probably not java).
If the major core of your code is a lot of BigDecimal math, Java is just not the right language IMO. You need the operator overloading.
I hope that a future version of Java will allow system defined operator overloading for the built-in number types to make this kind of use case usable.
Since all these languages run on the JVM, you could of course get the advanced math done in the right language and use Java for everything else. That would minimize the "learn another language" aspect.
精彩评论