liveBook · Manning

Arbitrary-precision integer

As explained in Clojure: The Essential Reference

BigInt is Clojure's arbitrary-precision integer representation, optimized to use primitive numeric values when possible.

An arbitrary-precision integer represents numbers beyond the limits of a signed long. Clojure's BigInt arithmetic automatically promotes results past Long/MIN_VALUE or Long/MAX_VALUE, subject to available memory. section 5.2 section 5.12

Use the quoted integer operators when calculations may exceed fixed-width limits:

  • Use +', -', *', inc', and dec' for arbitrary-precision integer arithmetic. section 5.12
  • Keep integer operands as integers; convert floating-point inputs to BigDecimal before using these operators. section 5.12
  • Expect Java interoperation when using functionality implemented by java.math.BigInteger, including conversions to and from BigInt. section 5.12

Promotion at the long boundary

The ordinary fixed-precision operators can overflow at the long limits, while (+’ Long/MAX_VALUE 1) produces a BigInt instead of wrapping. A literal such as 2N also has type clojure.lang.BigInt. section 5.2 section 5.12

Large-number cryptography

A simplified Diffie-Hellman implementation uses BigInt for large modulus and exponent calculations, while converting to BigInteger to call Java's probable-prime and modular-exponentiation methods. section 5.12

Listing 5.9. → Integer Math, High Precision Arithmetic, Numeric Processing
(+'
  ([])
  ([x])
  ([x y])
  ([x y & more]))

(*'
  ([])
  ([x])
  ([x y])
  ([x y & more]))

(-'
  ([x])
  ([x y])
  ([x y & more]))

(inc' [x])
(dec' [x])

Shows the supported arities for the core arbitrary-precision arithmetic operators. section 5.12

How Clojure: The Essential Reference covers it

How deep it goes
Defines promotion boundaries, operand requirements, Java interoperation, examples, and operator arities without detailing BigInt internals.
Examples
Demonstrates boundary promotion, the 2N literal, and BigInt-backed Diffie-Hellman operations.
What it assumes
Assumes basic integer arithmetic and distinguishes integer operands from floating-point values requiring BigDecimal conversion.
What you can do after
Apply it via Clojure's quoted arithmetic operators and Java BigInteger conversions for calculations beyond fixed-width integer limits.
How it connects
Connects arbitrary-precision arithmetic with integer overflow, BigDecimal, BigInteger, and Diffie-Hellman key exchange.

Also explained in

Compare how 4 books explain arbitrary-precision integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.