Cover by Brian Carper, Christophe Grand, Chas Emerick

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Clojure Mathematics

Knowing the types of available numbers and their representations is not sufficient to understand how mathematics is modeled in Clojure. Clojure’s various arithmetic and equality operators provide additional semantic guarantees in order to support sensible results across all the numeric types in play and in the face of many common issues that arise in computational mathematics, such as the handling of overflow and underflow, controlling the promotion of arithmetic results, and collection equality.

Bounded Versus Arbitrary Precision

The 64-bit range of longs and doubles is quite expansive; integers ±263-1 can be represented and operated on, along with decimals ±1.7976931348623157308. Most applications have no need for additional range or precision, so longs and doubles are often perfectly sufficient.

For those applications that do require additional range and/or precision, arbitrary precision numeric representations are available. These representations are always boxed; Clojure uses both clojure.lang.BigInt and java.math.BigInteger to represent arbitrary-range integers, and java.math.BigDecimal for arbitrary-precision decimals.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required