Problem with Precision floating point operation in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Floating-point precision surprises in C are usually expected behavior, not random arithmetic failures. Decimal values like 0.1 often cannot be represented exactly in binary floating formats, so tiny rounding differences appear in results. Correct C code handles this with tolerant comparisons, numerically stable algorithms, and appropriate type choice for domain requirements.
Why Exact Decimal Equality Often Fails
Most C platforms implement IEEE 754 float and double. These types store the nearest representable binary value, not exact decimal fractions.
The comparison often prints false because exact equality is too strict for rounded values.
Use Tolerance-Based Comparison
For computed values, compare with absolute and relative tolerances.
Example usage:
Tolerance values should match your domain, not a random constant copied from another project.
Accumulation Error and Stable Summation
Adding many values can amplify rounding error, especially when values differ greatly in magnitude.
Stable algorithms such as Kahan summation can preserve small contributions lost by naive accumulation.
Choose Numeric Type by Data Semantics
Use double for most scientific and engineering calculations unless memory constraints require float. For exact decimal domains like money, prefer scaled integers.
This avoids floating drift for financial totals and comparisons.
Platform and Compiler Effects
Floating-point results can differ slightly across compilers, instruction sets, and optimization levels. For portable behavior:
- Keep compiler flags consistent in CI and production.
- Use tolerance-based assertions in tests.
- Avoid depending on one exact least-significant-bit output.
Numerical code should be designed for stability across valid platform implementations.
Debugging Workflow for Precision Issues
When debugging numeric discrepancies:
- Print intermediate values at high precision.
- Isolate the first step where results diverge.
- Compare naive and stable algorithm variants.
- Test large magnitudes, tiny increments, and cancellation-heavy expressions.
This process identifies root cause much faster than changing random constants.
Common Pitfalls
- Comparing computed floating-point values with strict
==. - Using one global epsilon for all modules and scales.
- Mixing
floatanddoubleunintentionally in expressions. - Summing values in numerically unstable order.
- Using floating-point arithmetic where exact decimal semantics are required.
Summary
- Floating-point approximation is expected in C due to binary representation.
- Exact decimal equality after arithmetic is often the wrong validation method.
- Use absolute plus relative tolerance for robust comparisons.
- Stable summation algorithms reduce accumulation error.
- For exact-money style domains, use scaled integers instead of floats.
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.