# Floating-Point Numbers

Mathematics I · Scientific Programming · https://tryals.app/learn/mathematics-i/floating-point-numbers

## Representing the Real World

Scientific computing relies on **floating-point** numbers to approximate real numbers within finite memory. Stored via the IEEE 754 standard, each value uses a **sign**, a **mantissa** for significant digits, and an **exponent** for scale.

Because storage is finite, most real numbers fall between representable values and are **rounded** to the nearest tick, causing **round-off error**. Double precision grants roughly 15–16 significant digits, meaning anything beyond that is noise.

Arithmetic can also trigger **infinity** from overflow or division by zero, and **NaN** ("not a number") from invalid operations like $0/0$, both of which propagate through code.

## The Rules of Floating-Point

Numerical computing requires managing fixed precision and gaps instead of expecting exact math. Common issues include:

| Problem | Cause & Consequence | Solution |
| --- | --- | --- |
| **Exact Equality** | `0.1 + 0.2 != 0.3` due to rounding bits. | Compare using tolerance: $|a - b| < \epsilon$. |
| **Limited Precision** | Only 15–16 significant digits survive. | Track accumulated error limits. |
| **Special Values** | **Infinity** and **NaN** propagate through math. | Check inputs and handle edge cases. |

**Common Pitfall:** Testing floats with `==`. Always use a **tolerance** ($\epsilon$) to avoid failing due to tiny rounding differences.

## Practice questions

5 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. Sort each way of comparing two floating-point values.

**Answer:**

- Safe: Test that the absolute difference is below 0.000000001, Test that the difference is below a small multiple of the larger magnitude
- Unsafe: Test a == b, Test a != b, Loop while x is not exactly 1.0, increasing x by 0.1 each pass

**Why:** Both == and != depend on exact bit agreement, and the loop is worse still: adding 0.1 ten times does not land exactly on 1.0, so it never terminates. Absolute and relative tolerance tests are the safe forms.

Page: https://tryals.app/practice/mathematics-i/floating-point-numbers/sort-each-way-of-comparing-two-floating-point-values

### 2. In 1996 the Ariane 5 rocket destroyed itself 37 seconds after launch, from converting a 64-bit float to a 16-bit integer that overflowed. What is the general lesson?

A. That the error was a hardware fault rather than a software one
B. That floating-point arithmetic should never be used in safety-critical systems
C. That representable range is part of a program's contract
D. That the conversion should have been to a 32-bit integer instead

**Answer:** C. That representable range is part of a program's contract

**Why:** The software was correct, for Ariane 4. A faster rocket produced a velocity outside the 16-bit range, and reuse carried the code across without re-checking its assumptions. Range is part of the contract, and 370 million dollars was the cost of an implicit precondition.

Page: https://tryals.app/practice/mathematics-i/floating-point-numbers/in-1996-the-ariane-5-rocket-destroyed-itself-37-seconds-after-launch

### 3. In that same toy system, representable values are spaced 0.25 apart. What is the largest possible round-off error when a real number in that range is stored?

**Answer:** 0.125 (within ±0.001)

**Why:** A value midway between two ticks is 0.125 from each, so the worst-case error is half the spacing. This is why round-off error is described as half the gap between neighbouring floats, and why it grows with the magnitude of the number.

Page: https://tryals.app/practice/mathematics-i/floating-point-numbers/in-that-same-toy-system-representable-values-are-spaced-0-25-apart

### 4. Select every statement about floating-point arithmetic that is TRUE.

A. Every real number between 1 and 2 has an exact representation as a double
B. Two computations that are mathematically equal can give slightly different stored values
C. Round-off error is a bug that careful programming removes entirely
D. NaN arises from operations such as 0 divided by 0 and contaminates later arithmetic
E. Whole numbers within the mantissa's range are stored exactly

**Answer:** B. Two computations that are mathematically equal can give slightly different stored values; D. NaN arises from operations such as 0 divided by 0 and contaminates later arithmetic; E. Whole numbers within the mantissa's range are stored exactly

**Why:** Only finitely many values are representable, so most reals are rounded, and round-off is therefore inherent rather than a bug. Different routes to the same value round differently, NaN propagates, and modest integers are exact.

Page: https://tryals.app/practice/mathematics-i/floating-point-numbers/select-every-statement-about-floating-point-arithmetic-that-is-true

### 5. Floating-point numbers are spaced evenly in a toy system like 1.0, 1.25, 1.5, 1.75, 2.0 - but across the full range they are not evenly spaced at all. Why does the gap between representable numbers grow with magnitude?

A. Because large numbers are used less often, so fewer of them are stored.
B. Because the exponent field runs out of values for large numbers, forcing gaps.
C. Because the mantissa's fixed precision is a relative precision.
D. Because rounding errors accumulate as numbers grow, widening the spacing.

**Answer:** C. Because the mantissa's fixed precision is a relative precision.

**Why:** Floating point is scientific notation in binary, fixed significant digits times a power of two, so a fixed *relative* precision means the absolute gap scales with size. Near 1 the gap is about 2×10⁻¹⁶; near 10¹⁶ it exceeds 1, and adding 1 changes nothing. Which is why summing an array smallest-first is more accurate, and why Kahan summation exists.

Page: https://tryals.app/practice/mathematics-i/floating-point-numbers/floating-point-numbers-are-spaced-evenly-in-a-toy-system-like-1-0
