The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
module Origin
private DEFAULT_TOLERANCE = 1e-7
def self.=~(x : Float64, y : Float64, rel_tol : Float64 = DEFAULT_TOLERANCE, abs_tol : Float64 = rel_tol)
(x - y).abs <= Math.max(abs_tol, rel_tol * Math.max(x.abs, y.abs))
end
end
Interestingly, this can be called as a fully-qualified method: Origin.=~(a, b, rel_tol: c, abs_tol: d)
, or a binary operator without qualification if the defaults are sufficient: a =~ b
. I'm not sure why the former requires qualification.
==
instead of =~
, and the error message says I'm trying to define a setter with more than 1 parameter. I know why this is the case, but maybe a special-cased error for arity-overloading == should exist
==
function by wrapping it in backticks
self
)