liveBook · Manning

Floating-point arithmetic

As explained in Clojure: The Essential Reference

Floating-point arithmetic is numerical computation using finite-precision representations with a significand and exponent.

Floating-point arithmetic enables numerical computation with finite-precision binary representations. Its trade-off between range and precision makes ordinary decimal-looking values potentially inexact. section 6.2

Rounding can propagate

A literal can print as 0.1 while storing a longer binary approximation. Repeated arithmetic exposes the difference: (== 0.3 (+ 0.1 0.1 0.1)) returns false. section 6.2

Use floating-point values deliberately when approximation is acceptable. Avoid float and double for currency; choose an exact representation such as BigDecimal when precision matters. section 6.2 section 5.13

Mixed numeric comparison

Clojure’s == is suited to comparing numerically equivalent values across types: (== 1M 1.) returns true, whereas (= 1M 1.) returns false. This supports matching market orders whose prices arrive as integers, doubles, or BigDecimals.

How Clojure: The Essential Reference covers it

How deep it goes
Explains binary precision, visible rounding propagation, and representation choices, but does not derive floating-point internals.
Examples
Uses 0.1, repeated addition, mixed numeric equality, and cross-market order prices.
What it assumes
Assumes familiarity with Clojure numeric literals and distinguishes integers, doubles, and BigDecimals while explaining their precision implications.
What you can do after
Apply it via Clojure’s == and choose BigDecimal when computations require explicit precision.
How it connects
Connects floating-point arithmetic with numeric types, BigDecimal, equality, mixed-type arithmetic, and numeric contagion.

Also explained in

Compare how 20 books explain floating-point arithmetic →

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.