0Pricing
Mojo Academy · Lesson

Verifying Numeric Correctness

Check results against a reference.

Verifying Numeric Correctness is a free Mojo Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Fast but Wrong Is Useless

An optimized kernel only counts if it computes the right answer. Always verify before you trust a speedup.

Keep a Reference

Hold on to the simple, obviously correct version. It is your reference, the source of truth every fast kernel must match.

Floats Are Not Exact

Floating-point math rounds, so reordering sums can shift the last digits. Optimized results may differ slightly without being wrong.

Avoid Exact Equality

Because of rounding, never compare floats with ==. Tiny differences will make correct code look broken.

if fast == ref:  # fragile, avoid

Compare With a Tolerance

Instead, check that values are close within a small tolerance. If the gap is under epsilon, treat them as equal.

if abs(fast - ref) <= 1e-5:
    pass  # close enough

Absolute vs Relative Error

Absolute error is the raw gap; relative error scales it by magnitude. Big numbers need relative checks to stay fair.

rel = abs(fast - ref) / abs(ref)

Check Every Element

Loop over the whole result and test each cell. One mismatch beyond tolerance means the optimization broke something.

for i in range(n):
    if abs(fast[i] - ref[i]) > tol:
        print("mismatch at", i)

Track the Worst Gap

Record the largest difference you see. The max error across all elements is a clear, single health number for the kernel.

Test the Edge Cases

Try empty inputs, a single element, and odd sizes that do not divide evenly by the SIMD width. Edges expose tail bugs fast.

Use Known Answers

Multiply by the identity matrix or sum a vector of ones. Predictable inputs give answers you can verify by hand.

Automate the Check

Wrap verification in a small test that fails loudly on any mismatch. Run it after every change so regressions never slip by.

Quick Check

Your SIMD matmul is off from the reference by 0.0000007. What should you conclude?

Recap

Verify fast code against a trusted reference, compare with a tolerance not ==, check edges and known answers, and automate it. ✅

Frequently asked questions

Is the “Verifying Numeric Correctness” lesson free?

Yes — the full text of “Verifying Numeric Correctness” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Verifying Numeric Correctness”?

Check results against a reference. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Verifying Numeric Correctness” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Modeling a Tensor in Mojo
  2. Building a Matmul Step by Step
  3. Optimizing the Inner Product
  4. Verifying Numeric Correctness
← Back to Mojo Academy