5

I'm trying to create some Java classes, that should work with either float or double numbers (for simulation purposes I am required to support both). The classes need to do some basic arithmetic and also require use of trigonometric functions (sin, cos, atan2).

I tried to do a generic approach. As Java does not allow primitive types in generics and MyClass<T extends Number> does indeed allow Double and Float, but makes basic arithmetic impossible, I build a wrapper class around Double and Float. But this approach fails, as soon as I need to instantiate a value in one of the generic classes.

Is there any clean way to support both float and double, without duplicating all the code for each type?

5
  • If you write the the method for double you can use it for float as well.. Commented Dec 17, 2012 at 9:13
  • Do you need to work with wrapper classes or with primitives? Commented Dec 17, 2012 at 9:15
  • @Thihara Yes, but the method will then return a double, independent of the type of its parameters. I need to stay with the given precision. Commented Dec 17, 2012 at 9:56
  • @AndrewLogvinov: I do not need to use the Number wrapper classes, but for using collections it would be helpful, if some kind of wrapper class could be used in the solution. I mainly need to do arithmetic. With primitives only I would have to write all the methods once per type, if possible I want to avoid that. Commented Dec 17, 2012 at 10:04
  • If you use Number then you will have to do the checks to ensure its actualls Doubles and Floats that's passed... Commented Dec 17, 2012 at 10:25

2 Answers 2

7

Maybe this is what you are looking for?

class MyClass<T extends Number> {
    T add(T t1, T t2) {
        if (t1 instanceof Double) {
            return (T) Double.valueOf((t1.doubleValue() + t2.doubleValue()));
        } else if (t1 instanceof Float) {
            return (T) Float.valueOf(((t1.floatValue() + t2.floatValue())));
        } else if (t1 instanceof Integer) {
            return (T) Integer.valueOf(((t1.intValue() + t2.intValue())));
        }
        // you can add all types or throw an exception
        throw new IllegalArgumentException();
    }

    public static void main(String[] args) {
        MyClass<Double> mc = new MyClass<Double>();
        mc.add(1.0, 1.1);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

5

From experience, if you're doing hardcore numerical work in Java, it's better to stick to primitive types. This means that generics are a no-go for this type of work (but of course are perfectly fine for many other uses).

Is there any clean way to support both float and double, without duplicating all the code for each type?

You could implement the lower-precision method in terms of the higher-precision one:

public static double calc(double x, double y) {
   // do the calculation and return double
}

public static float calc(float x, float y) {
   return (float)calc((double)x, (double)y);
}

1 Comment

thanks, I decided to use something like that. Not as generic as I hoped, but it doesn't look like there is a way for generics and arithmetic to work together well in Java

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.