5

I was wondering could anyone help me with providing options for both double and float precision for a class please?

I am working with Java3D and from the API specification for Point3d and Point3f, it is my understanding that the implementation effectively duplicates the Point3f class replacing float with double where necessary. Is this because Generics cannot be used with primitive data types? Efficiency?

I am writing a Curve class and am wondering should I provide Curved and Curvef in a similar fashion. Is this the best option?

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
felice
  • 53
  • 4
  • 1
    See http://stackoverflow.com/questions/2721546/why-dont-generics-support-primitive-types for related info – djechlin Feb 28 '13 at 17:44
  • 1
    Only write both if you'll _use_ both. – Louis Wasserman Feb 28 '13 at 17:46
  • 1
    Also need to point out that this is clearly premature optimization. Just use double. – djechlin Feb 28 '13 at 18:08
  • The implementation involves curve and surface deformation calculations that can be prone to floating point errors under certain conditions so I would like to afford the user options concerning the trade off between performance and accuracy. Thanks for your feedback. – felice Mar 01 '13 at 14:50

2 Answers2

5

Yes, unfortunately Java generics doesn't work on primitive types.

You could do code generation though, producing multiple java files from one template.

Java 8 is going to introduce a lot of specialized primitive versions of functional interfaces, like IntFunction. It's going to be quite ugly.

irreputable
  • 44,725
  • 9
  • 65
  • 93
1

I think that the main concern here is performance. Generic type parameters must be a subtype of Object, then the generic implementation should use Double and Float wrappers (since primitive types are not available to the generic mechanism).

In this case, every instance of Curve would instantiate several objects, one for each double/float value. The actual value will be read by method invocations such as doubleValue() and floatValue(). Furthermore, since primitive wrappers are immutable, the result of any operation involving wrappers must be stored in a new instance. That doesn't seems acceptable because Curve will likely be used in heavy calculations.

Javier
  • 12,100
  • 5
  • 46
  • 57