Garrett Rowe
Garrett Rowe

Reputation: 1235

Resolving overloaded float/double method variants from Clojure

java.lang.Math.scalb is overloaded to accept a double or a float value as the first parameter. I would like to call the double variant but I am converting from a clojure long. The only way I have found that resolves correctly is by coercing to a double, then calling the java.lang.Double constructor. Is there a less cluttered way of doing this?

user> (Math/scalb 21 -63)
IllegalArgumentException No matching method found: scalb  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:80)

user> (Math/scalb (double 21) -63)
CompilerException java.lang.IllegalArgumentException: More than one matching method found: scalb, compiling:(NO_SOURCE_PATH:1:1) 

user> (Math/scalb (Double. 21) -63)
IllegalArgumentException No matching ctor found for class java.lang.Double  clojure.lang.Reflector.invokeConstructor (Reflector.java:183)

user> (Math/scalb (Double. (double 21)) -63)
2.2768245622195593E-18

Upvotes: 1

Views: 108

Answers (1)

amalloy
amalloy

Reputation: 92147

You need to convert the second argument too: it's an int argument, but your numbers are longs. Obviously the compiler could figure it out in some cases (such as this one), but once you start providing typehints you're expected to finish disambiguating.

user> (Math/scalb (double 21) (int -63))
2.2768245622195593E-18

Upvotes: 2

Related Questions