Module 12 · Statistical Computing and Reproducibility Lesson 111 of 120

Floating-Point Representation, Overflow, and Underflow

Why a familiar decimal can fail exact equality.

2:47 clip6:01:17–6:04:05 in the full courseWatch on YouTube

Transcript

17 sentences · select one to jump there

Check your understanding

Is Number.EPSILON a universal absolute tolerance for all values?

Choose one answer

Code lab

Run it yourself

The lesson source in 7 languages. Edit it, run TypeScript and Python right here, and compare with the expected output.

111-floating-point-representation-overflow-and-underflow.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 111 of 120
 * Floating-Point Representation, Overflow, and Underflow
 * Module 12: Statistical Computing and Reproducibility
 *
 * Scenario: Why a familiar decimal can fail exact equality
 * Rule:     binary floating point is finite, rounded representation
 *
 * Try it:   Is Number.EPSILON a universal absolute tolerance for all values?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/statistical-computing-and-reproducibility/floating-point-representation-overflow-and-underflow/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson111() {
  const sum=.1+.2;
  const overflow=Number.MAX_VALUE*2;
  const underflow=Number.MIN_VALUE/2;
  const result={sum,exactlyPointThree:sum===.3,
    overflow:String(overflow),underflow,epsilon:Number.EPSILON};
  return result;
}

export const checkedResult = {"sum":0.30000000000000004,"exactlyPointThree":false,"overflow":"Infinity","underflow":0,"epsilon":2.220446049250313e-16};

// Run this file directly: npx tsx lessons/12-statistical-computing-and-reproducibility/111-floating-point-representation-overflow-and-underflow.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson111(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "sum": 0.30000000000000004,
  "exactlyPointThree": false,
  "overflow": "Infinity",
  "underflow": 0,
  "epsilon": 2.220446049250313e-16
}

Prefer your own machine? Every file is in the course repository · open it in Codespaces.

Lesson notes

The rule

binary floating point is finite, rounded representation