O'Reilly logo

Clojure for Finance by Timothy Washington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Numbers and precision

Clojure numbers default to their host implementation, which refers to the JVM in most cases. So, 1 is of type java.lang.Long, and 2.5 or 0.1 is of the java.lang.Double type:

(type 1)
java.lang.Long

(type 0.1)
java.lang.Double

Clojure supports Java's primitive number types of byte, char, int, long, and float. These symbols also correspond to Clojure functions, which convert to a native type. This is useful when dealing with a function where a particular type hints at a primitive number:

(defn double' ^long [^long n]
  (+ n n))

(double' (int 4))
8

Clojure also supports native JVM numeric types, such as Byte, Short, Integer, Long, BigInteger, Float, Double, and BigDecimal. Larger numbers in Clojure have custom types because they require ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required