2

I'm writing a function that's part of a Clojure/ClojureScript crossover, and I'd used the double function. When I compile this with cljsbuild, it complains that double is an undeclared var.

WARNING: Use of undeclared Var my-ns/double 

What's an alternative function to double that will operate on both platforms? FYI, I'm using it to convert a rational after division into a double — I'm then formatting this as a string.

4

1 回答 1

1

ClojureScript currently only supports integer and floating point literals that map to JavaScript primitives. Ratio, BigDecimal, and BigInteger literals are currently not supported.

The good news is that javascript floats are already double precision so there is no need to coerce them into doubles explicitly. The bad news is that there is no support for rational numbers so you can't use them.

You will either need to rewrite the expressions that uses rational numbers or separate the code for the JVM from the code targeted at JS. Have a look at the cljx project to do just that.

Concerning your use case. From your description I understand you are trying to rewrite an expression similar to this:

(str (double (/ 2/4 2)))
=> "0.25"

You could simply rewrite that expression in CLJS like this:

(str (double (/ (/ 2 4) 2)))
=> "0.25"

Note that you can leave out the call to double:

(str (/ (/ 2 4) 2))
=> "0.25"

Hope this solves you problem.

于 2014-07-23T11:02:48.770 回答