liveBook · Manning

Floating-point arithmetic

As explained in Functional Design and Architecture: Examples in Haskell

A floating-point data type represents approximate real-number values using a finite-precision fractional representation.

Floating-point arithmetic represents real-number values approximately, so exact equality can surprise you. That matters when storing measurements such as meteor coordinates in relational databases. section 12.1.1 section 12.1.2

A comparison that fails

The expressions 0.1 + 0.1 == 0.2 and 0.1 + 0.2 == 0.3 produce different results: the first is True, while the second is False. Strict equality is available, but should not automatically be treated as meaningful for floating-point values. section 12.1.2

Use floating-point fields deliberately when designing persistent data:

  • Store measurements such as azimuth and altitude with FLOAT-compatible database fields, while keeping database types separate from domain types. section 12.1.1
  • Avoid using floating-point values in primary keys because rounding makes exact values uncertain and equality-based lookup unreliable. section 12.1.2
  • Prefer a surrogate key when records need stable identification, rather than combining several measurement fields into a natural key. section 12.1.2

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains rounding-related equality failures and database key risks, but does not develop numerical error analysis or alternative comparison algorithms.
Examples
Uses meteor coordinates and GHCi comparisons showing different outcomes for decimal additions.
What it assumes
Assumes basic data modeling and equality, while illustrating rounding behavior through concrete numeric expressions.
What you can do after
Evaluate floating-point fields for persistence and reject them as primary-key components when exact identity is required.
How it connects
Connects floating-point arithmetic to relational databases, SQL FLOAT, equality, natural keys, and surrogate keys.

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 Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.