Measurement with Uncertainty¶
Try it live on Compiler Explorer
Overview¶
Scientific and engineering measurements always carry uncertainty. This example demonstrates how to create a custom representation type that pairs measured values with their uncertainties and automatically propagates errors through calculations using the mp-units library.
The implementation provides first-order uncertainty propagation following standard error analysis formulas, making it suitable for experimental physics, metrology, and engineering applications. It's particularly relevant for systems like IAU (International Astronomical Union) where constants are defined with explicit uncertainties.
The example also demonstrates the Faster-than-lightspeed constants feature, where mathematical constants like π are treated as exact symbolic values that don't contribute to numerical uncertainty, ensuring that only measurement errors propagate through geometric calculations.
The measurement Class¶
Header File¶
The complete implementation is provided in a separate header file that can be reused across projects:
| measurement.h | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
Key implementation details
- Value and uncertainty storage: Stores both the measured value and its absolute uncertainty (σ)
- Automatic error propagation: All arithmetic operators implement proper uncertainty formulas
- Mathematical functions: Includes
pow,sqrt,exp,logwith correct uncertainty derivatives - First-order approximation: Uses linear Taylor expansion for uncertainty propagation
- Independent variables assumption: Does not track correlations between measurements
Integration with mp-units¶
To use a custom type as a quantity representation, it must satisfy the RepresentationOf
concept. The library verifies this at compile time:
static_assert(mp_units::RepresentationOf<measurement<double>, mp_units::quantity_character::real_scalar>);
static_assert(mp_units::RepresentationOf<measurement<double>, mp_units::quantity_character::vector>);
This allows measurement<T> to be used seamlessly with any mp-units quantity type.
Example Usage¶
Basic Operations¶
The example demonstrates various uncertainty propagation scenarios:
void example()
{
using namespace mp_units;
using namespace mp_units::si::unit_symbols;
std::cout << "Mass of the Sun: M_sun = " << measurement{19884, 2} * (mag_power<10, 26> * kg) << '\n';
const auto acceleration = isq::acceleration(measurement{9.8, 0.1} * m / s2);
const auto time = measurement{1.2, 0.1} * s;
const QuantityOf<isq::velocity> auto velocity = acceleration * time;
std::cout << "Velocity calculation: V = " << acceleration << " * " << time << " = " << velocity << " = "
<< velocity.in(km / h) << '\n';
const auto length = measurement{123., 1.} * m;
std::cout << "Scalar multiplication: d = 10 * " << length << " = " << 10 * length << '\n';
const auto radius = measurement{5.0, 0.1} * m;
const auto circumference = radius * (mag<2> * mag<π> * one);
const auto area = pow<2>(radius) * (mag<π> * one);
std::cout << "Radius: r = " << radius << '\n';
std::cout << "Circular circumference: 2πr = " << circumference << " = " << circumference.in(m) << '\n';
std::cout << "Circular area: πr² = " << area << " = " << area.in(m2) << '\n';
const auto area_measured = measurement{25.0, 1.0} * (mag<π> * m2);
const auto radius_from_area = sqrt(area_measured / (mag<π> * one));
std::cout << "Radius from area: A = " << area_measured << " -> r = √(A/π) = " << radius_from_area << '\n';
}
This showcases:
- Astronomical constants - Using magnitudes with uncertainties for large-scale values
- Kinematic calculations - Multiplying acceleration by time with automatic unit conversions
- Scalar multiplication - Exact scaling preserves relative uncertainty
- Faster-than-lightspeed constants - Using
mag<π>keeps π as an exact symbolic value, not a numeric approximation - Geometric calculations - Computing circumference and area where π contributes zero uncertainty
- Inverse operations - Computing radius from area using
sqrt, with π canceling out exactly
Sample Output:
Mass of the Sun: M_sun = 19884 ± 2 (10²⁶ kg)
Velocity calculation: V = 9.8 ± 0.1 m/s² * 1.2 ± 0.1 s = 11.76 ± 0.98732 m/s = 42.336 ± 3.55435 km/h
Scalar multiplication: d = 10 * 123 ± 1 m = 1230 ± 10 m
Radius: r = 5 ± 0.1 m
Circular circumference: 2πr = 5 ± 0.1 (2 π) m = 31.4159 ± 0.628319 m
Circular area: πr² = 25 ± 1 (π) m² = 78.5398 ± 3.14159 m²
Radius from area: A = 25 ± 1 (π m²) -> r = √(A/π) = 5 ± 0.1 m
Uncertainty Propagation Analysis¶
Notice how uncertainties propagate through the calculations:
- Velocity: Combining two independent measurements (acceleration and time),
both with ~1-2% relative uncertainty, yields a velocity with ~8.4% relative uncertainty
(
0.98732/11.76 ≈ 0.084) - Scalar multiplication: Multiplying by exact scalar 10 preserves the relative uncertainty (~0.8%)
- Circumference (2πr): The factor 2π is exact (no uncertainty), so only the radius
uncertainty propagates:
σ_c = 2π × 0.1 m = 0.628... m - Area (πr²): Squaring doubles the relative uncertainty (r: 2% → r²: 4%), while π
remains exact:
σ_A = π × |2r × σ_r| = π × 1 m² = 3.14... m² - Round-trip verification: Computing radius from area with
√(A/π)recovers the original5 ± 0.1 mbecause π cancels exactly—this demonstrates the power of symbolic constants in avoiding accumulated numerical errors
Error Propagation Formulas¶
The implementation uses standard first-order uncertainty propagation:
| Operation | Formula | Implementation |
|---|---|---|
Addition: z = x + y |
σ_z² = σ_x² + σ_y² |
Quadrature sum (Pythagorean) |
Subtraction: z = x - y |
σ_z² = σ_x² + σ_y² |
Same as addition |
Multiplication: z = x × y |
(σ_z/z)² = (σ_x/x)² + (σ_y/y)² |
Relative uncertainties in quadrature |
Division: z = x / y |
(σ_z/z)² = (σ_x/x)² + (σ_y/y)² |
Same as multiplication |
Scalar multiply: z = k × x |
σ_z = |k| × σ_x |
Exact scaling |
Power: z = x^n |
σ_z = |n × z/x × σ_x| |
Derivative method |
Square root: z = √x |
σ_z = σ_x / (2√x) |
Special case of power |
Exponential: z = exp(x) |
σ_z = |z × σ_x| |
Derivative method |
Logarithm: z = ln(x) |
σ_z = |σ_x / x| |
Derivative method |
Limitations and Caveats¶
Independent measurements assumption
The measurement class assumes all values are statistically independent. Operations like
x - x will incorrectly give non-zero uncertainty. For correlated measurements, a more
sophisticated approach with covariance matrices is needed.
First-order approximation
Uncertainty propagation uses linear approximation (first derivative only). This is accurate when relative uncertainties are small (<10%). For larger uncertainties or highly non-linear functions, higher-order terms may be significant.
When to use this implementation
- ✅ Combining independent measurements from different instruments
- ✅ Standard metrology and experimental physics calculations
- ✅ Systems like IAU where constants have defined uncertainties
- ✅ Small to moderate relative uncertainties (<10%)
- ❌ Correlated measurements (same source used multiple times)
- ❌ Large relative uncertainties (>10%)
- ❌ Systematic errors (requires different treatment)
Why This Matters¶
- Automatic Error Propagation: No manual uncertainty calculations needed—formulas are built into operators
- Type Safety: The dimensional analysis works with uncertainties seamlessly through mp-units
- Scientific Accuracy: Properly tracks measurement precision through complex calculations
- Standard Compliant: Follows ISO GUM (Guide to the Expression of Uncertainty in Measurement) principles
- Extensibility: Demonstrates how to integrate domain-specific numeric types with mp-units
- Real-World Applicability: Suitable for IAU astronomical constants, NIST physical constants, and laboratory measurements
This pattern is essential for scientific computing, metrology, laboratory measurements, and any application where measurement precision and traceability matter.