GenerateRandomSearch

Floating Point Pitfall Generator

Everyone learns that 0.1 plus 0.2 is not quite 0.3. Far fewer people meet the other three, which cause more damage: addition is not associative, so grouping changes the answer; subtracting two close numbers can destroy every meaningful digit; and above a certain size, whole numbers stop being exact. Each is generated here with real values, printed to enough digits to show the discrepancy rather than hide it.

What this generator does

Produces a concrete example of the chosen pitfall, prints the value the machine actually holds to twenty significant digits, and shows the gap against what ordinary arithmetic would give. For non-associativity it searches until it finds a triple where the two groupings genuinely differ, rather than assuming one exists.

How to use this tool

  1. Pick which pitfall to see and generate an example.
  2. Compare what the arithmetic should give against what it actually gives.
  3. Read the explanation — each has a different cause and a different fix.
  4. Generate several of the same kind; the size of the error varies a great deal.

Understanding the controls

Which pitfall
Decimals that do not add up; the order of addition mattering; subtracting two close numbers; and whole numbers ceasing to be exact above a certain size.
Seed
Any word reproduces exactly the same example, so it can be shared or referred back to.

Common use cases

  • Explaining to a colleague why a total is a penny out
  • Teaching floating-point behaviour with concrete numbers
  • Producing examples for documentation or a code review comment
  • Understanding why money should be held in whole pence
  • Setting the same example twice from a seed

How this generator works

Nothing here is simulated or approximated — the arithmetic is performed and the result printed. The associativity example is the one that needs searching: many triples give the same answer either way, so candidates are tried until one is found where the two groupings genuinely differ, and the check confirms they do before it is shown.

Randomness and fairness

The particular numbers are chosen at random; their behaviour is a fixed property of binary floating point and not random at all. A seed reproduces the same example exactly and is therefore explicitly not cryptographically secure. Without a seed the browser's cryptographically secure generator is used.

For how randomness is produced across the whole site, see how Generate Random works.

Limitations and good to know

  • Double precision, which is what browsers and most languages use by default. Single precision fails sooner and decimal types not at all.
  • Four common pitfalls rather than an exhaustive catalogue.
  • The decimal example occasionally comes out exact, because two rounding errors cancelled — that is honest rather than a fault.
  • It shows the behaviour rather than prescribing a fix, though each explanation points at the usual one.
  • An example is gone on reload; the seed is what brings the same one back.

Privacy and your data

Every example is computed in your browser. Nothing about it or your seed is transmitted or kept.