Frontory

Open code, practice, and notes →

Download source with English comments · for reading

WGSL · 24 Numeric Stability and Special Inputs

Series contents · Technology map

Stage: Numeric Verification and Kernel Design · Editable example · Storage ID: wgsl-step-24

Goals and learning outcomes

Prerequisites

Continue using the output and records from Unit 23. Before experimenting with Numeric Stability and Special Inputs, preserve the previous inputs and reference outputs.

Key concepts and explanations

Unstable Operations

Normalizing a vector of length 0 or dividing by a small number can produce unstable results. Check valid ranges and define meaningful fallback values.

Algorithmic Stability

Mathematically equivalent expressions can produce different floating-point errors. Compare different input scales rather than checking only whether results are close.

Practice setup

First check the execution scope guidance on the example screen. The following describes the environment needed to extend the assignment in a separate project. This document-guided exercise requires your own implementation in a separate localhost or HTTPS project. Check the secure context, navigator.gpu, and adapter request result, and provide an unsupported-environment message. Retain device.lost handling and resource cleanup.

The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.

Practice tasks

  1. In numeric.wgsl, write a function that uses a fallback direction when squared length is below a threshold.
  2. Compare vectors with 0, 1e-8, 1, and large coordinates against the CPU reference.
  3. Reject NaN or infinite inputs on the host and record their failure locations.

Variables to observe

VariableBaselineChangeExpected change / hypothesis
Vector length10 and 1e-8The chosen fallback direction is expected to be used instead of the normalization path.

Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.

Completion criteria

Self-check

  1. Can one small epsilon be used for every unit of measurement?
  2. How does a small error differ from algorithmic stability?

Official references

Linked example and execution scope

Example source · Runtime environments and limits

Runs actual WebGPU/WGSL. Requires HTTPS/localhost and a supported adapter; unsupported environments display a reason, and CPU results are not presented as GPU results.

Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.

Next learning

Previous: Storage Texture Output · Next: Kernel Tests and Sentinels

Open code, practice, and notes →