I am trying to create some Java classes that should work with either float or double numbers (for modeling purposes, I have to support both). Classes need to perform some basic arithmetic, as well as use trigonometric functions (sin, cos, atan2).
I tried to make a general approach. Since Java does not allow primitive types in generics, and MyClass<T extends Number> does allow Double and Float, but makes basic arithmetic impossible, I create a wrapper class around Double and Float. But this approach is not suitable as soon as I need to instantiate the value in one of the common classes.
Is there any clean way to support both float and double, without duplicating all the code for each type?
source share