Floating point comparison functions for C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Floating-point comparison in C is harder than it first looks because many decimal values cannot be represented exactly in binary. If two numbers arrive through different calculations, == can fail even when the math says they should match.
Why Direct Equality Often Fails
Binary floating-point formats store approximations. A classic example is 0.1 + 0.2, which is very close to 0.3 but not bit-for-bit identical on most systems.
That does not mean floating point is broken. It means you need a comparison rule that matches the problem you are solving. Exact equality is still valid for some cases, such as checking whether a value is exactly 0.0 because it was assigned directly, or comparing sentinel values that must be identical by design.
A Practical Comparison Function
The most useful approach is usually a combined absolute and relative tolerance. Absolute tolerance handles values near zero. Relative tolerance handles larger magnitudes where a small percentage error is acceptable.
This pattern is more robust than using DBL_EPSILON by itself. DBL_EPSILON describes machine precision near 1.0. It is not a universal tolerance for every scale of input.
Choosing a Tolerance
Tolerance values are application-specific. Financial calculations, geometry, physics simulation, and user-interface layout all have different error budgets. A reasonable comparison function is only half of the solution; the other half is choosing thresholds that reflect the domain.
As a rule:
- use absolute tolerance when comparing against zero or very small values
- use relative tolerance when values can become large
- avoid copying a random epsilon from the internet without understanding the units
For example, if values are expected to be around 1e6, an absolute tolerance of 1e-12 is usually meaningless. If values are expected to be near zero, relying on relative tolerance alone can also fail because the scale collapses.
Special Values: NaN and Infinity
Comparison helpers should also account for special floating-point values. NaN is never equal to anything, including itself. Positive and negative infinity are exact symbolic values and can be compared directly when that behavior is what you want.
That policy is simple and predictable. If your program needs a different rule, such as treating two NaN values as equivalent missing data, make that behavior explicit instead of hiding it inside a generic helper.
When Exact Equality Is Still Correct
Developers sometimes hear "never use == with floating point" and overcorrect. Exact equality is still appropriate when you are checking values that should be identical by construction. Examples include values copied without arithmetic, results read from the same serialized source, or state-machine markers using specific constants.
The real mistake is using == after a chain of computations that introduces rounding. In those cases, compare meaning, not raw bits.
Common Pitfalls
- Using
DBL_EPSILONas a one-size-fits-all tolerance for every magnitude. - Comparing only with relative tolerance and then failing near zero.
- Comparing only with absolute tolerance and then misclassifying large values.
- Forgetting to define a policy for
NaNand infinity. - Assuming floating-point comparison rules from one application domain fit another.
Summary
- Direct
==comparison is often wrong after real floating-point arithmetic. - A combined absolute-and-relative tolerance is the most practical general solution.
- '
DBL_EPSILONis a machine constant, not a universal business rule.' - Near-zero comparisons need absolute tolerance.
- '
NaNand infinity should be handled explicitly so comparison behavior stays predictable.'
Related reading
- FlowLayoutPanel - Automatic Width for controls?
- Fluent Validation vs. Data Annotations
- FluentValidation Check if one of two fields are empty
- Flutter - Default image to Image.network when it fails
- force browsers to get latest js and css files in asp.net application
- Force XmlSerializer to serialize DateTime as 'YYYY-MM-DD hhmmss
- Formatting IPv6 as an int in C and storing it in SQL Server
- Func delegate with no return type

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the 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.