I'm fairly new to Scala after working with Java for a while, so I apologize if this is a basic question. I have a method that I would like to be run in either float or double precision, and it seemed like it would be fairly easy to do with Scala's polymorphism. In Java, the only way I found to do it was by overloading the method and having one take float arguments and the other take double arguments, but I'm hoping that polymorphism would allow me to avoid that and just have one method that will work with either. The way I've implemented it so far looks like this (https://stackoverflow.com/a/4033390/6247850 for how to make the generic operations work between arguments):
def Sample[A: Numeric](x: A, y: A)(implicit num: Numeric[A]): A= { import num._ var out = 2.0.asInstanceOf[A] * x * y return out } This works fine for double precision, but results in a ClassCastException if I try to run it in Float. I can hardcode a workaround by changing it to 2.0.toFloat.asInstanceOf[A] which makes me think there has to be some way to do this, but hardcoding like that defeats the point of making it generic. I also had to do what seemed like a disproportionate amount of work to get division to work properly by using this answer https://stackoverflow.com/a/34977463/6247850, and building off of it to get a less than comparison to work.
Essentially, what I want is the ability to specify the precision that a method should run in and return. The problem comes from casting other variables used in the method to the appropriate type since .asInstanceOf[A] doesn't work unless it already was the correct type.