As far as I know, exact comparison doesn't make much sense with floating point values as what is intended to be 0.0001 can actually be something like 0.0001000...0001... Should I implement my own comparison function to specify precision or is there a common practice for this?
I used to use the following with C# (which, I suspect, is still wrong as a Double value can be probably uncapable of representing 0.0001 at all, even set as a constant (as Michael Borgwardt explained here)):
public static bool AlmostEquals(this double x, double y, double precision = 0.0001)
{
if (precision < 0.0)
throw new ArgumentException();
return Math.Abs(x - y) <= precision;
}
Should I do something alike in Scala?
Use Tolerance from scalautils
result: Double = 2.000001
res0: Boolean = true
res1: Boolean = false
Update: For Scala 2.11
Yes, you can do the same thing as in Java. You could also use some of Scala's cool features and pimp the Double class with a ~= method that takes an implicit precision parameter that only needs to be specified once.
Or with 2.10...