Skip to content

Handling Uncertainty and Confidence Intervals

In the real world, measurements are rarely perfect. Sensors have noise, and physical constants have margins of error. Zigma has native, built-in support for confidence intervals and automatic uncertainty propagation.

Instead of writing complex error-propagation formulas by hand, you define the uncertainty once, and the compiler handles the math through all subsequent tensor operations.


1. Defining Confidence Intervals

You can attach an uncertainty to any scalar by using the +/- operator.

// Defining initial conditions with their respective sensor errors
v0 = 20.0 +/- 0.5  m/s
g  = 9.81 +/- 0.01 m/s2
t  = 3.0  +/- 0.1  s

2. Automatic Error Propagation

Once defined, Zigma automatically propagates the uncertainty through your mathematical operations. Whether you are adding, multiplying, or using exponents, the language calculates the resulting margin of error natively.

// Zigma calculates both the base value AND the propagated uncertainty
y = (v0 * t) - (0.5 * g * t^2)

print "Final Position: {y}" 
// Output: Final Position: 15.855 +/- 2.14m

3. Mixed Scales in Uncertainty

You don't have to define the margin of error using the exact same scale as the base value. Zigma is smart enough to handle mixed scales.

For example, if you measure a length in meters but the laser rangefinder's error is in millimeters, you can write exactly that:

l = 10.5 m +/- 20 mm

Note: Behind the scenes, the uncertainty automatically takes on the scale and underlying memory type of the base value. In this case, the 20 mm margin is converted to 0.02 m internally.


4. Integer Precision Warnings

When using floating-point numbers (f32, f64), uncertainty works exactly as expected. However, if you explicitly restrict a variable to an integer type to save memory or enforce strict fixed-point math, you must be careful with small margins of error.

Because the uncertainty inherits the type of the base value, integer truncation can wipe out your confidence interval.

Precision Warning

Doing l = 10 m +/- 5 mm {i32} will result in an uncertainty of 0 m.

Why? The base value is 10 m stored as a 32-bit integer. The lowest possible precision it can hold is exactly 1 meter. When Zigma tries to convert the 5 mm margin into meters, it truncates 0.005 m down to 0 because integers cannot hold decimals.

How to fix this? Define the base value in the smaller scale if you want to use integers, so no precision is lost:

l = 10000 mm +/- 5 mm {i32} // Properly preserves the +/- 5mm error!