Is there is a way to change Groovy language behavior (and if not I think it could be interesting) that makes it convert numbers to float by default, instead of BigDecimal or Double?
Why do I need that? Because I use libgdx, which uses float in most of its methods for calculations, and when I use numbers with calculations without cast or using (someCalculation).floatValue() after, they always convert to BigDecimal or Double. And it is a bit annoying to always do (x* 5 + 8 /3).floatValue().
If there is a config file, annotation or something like that I could config it to convert always to float(by default), and if needed I specified a type? It will make the code less verbose, but is more verbose than java in that part at least... I didn't find anything like that in Groovy docs and web search
unfortunately floatingpoint math implementation in groovy based on double for both - float and double:
https://github.com/apache/groovy/blob/master/src/main/java/org/codehaus/groovy/runtime/typehandling/FloatingPointMath.java
And FloatingPointMath used from here: https://github.com/apache/groovy/blob/84e1299e02dd6d81577e15fe018b7c9a1d70b714/src/main/java/org/codehaus/groovy/runtime/typehandling/NumberMath.java#L230
However according to this example we could change the behavior of operations for a specific class with metaclass programming (there is a simple example for Integer):
https://docs.groovy-lang.org/latest/html/documentation/core-metaprogramming.html#_magic_package
Each numeric operator in groovy mapped to a method:
+-> plus--> minus*-> multiplymyFloat * myIntwill be processed in groovy asmyFloat.multiply(myInt)With metaprogramming you can override the behavior of those methods for Float meta class. So, whenever Float is a first parameter of math operation we could return a float result.
you should use javac to compile following class to use original java floating point operations instead of groovy.
filename: FloatMetaClass.java
command to compile:
This should create compiled version of java class in a special folder
groovy/runtime/metaclass/java/lang/FloatMetaClass.classNow running groovy with a classpath that is pointing to this folder will do the trick
Let's imagine the folder with compiled file mentioned above is in a current folder.
Where Test.groovy: