Emulate double using 2 floats
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If a platform has fast float support but slow or unavailable double, you can represent one high-precision value as the sum of two single-precision values. This technique is often called double-single arithmetic: one float stores the leading digits and the second stores the rounding error.
It does not produce a true IEEE double, and it does not automatically solve all numerical problems. Still, it can give substantially more precision than a plain float and is useful in graphics, DSP, and specialized numeric code.
The Core Idea
Suppose you store a number as hi + lo, where hi is the main value and lo is a correction term. The pair should be normalized so that hi contains the dominant part and lo is small compared with it.
A simple way to create such a pair from a float is:
The hard part is keeping the pair accurate after arithmetic. For addition, a common strategy is the two_sum algorithm, which computes both the rounded sum and the lost low-order bits.
On a normal float, 10000000.0f + 1.0f may round back to 10000000.0f. With a double-single representation, the missing unit can often be preserved in lo.
What You Gain and What You Do Not
This representation can deliver roughly twice the precision of a single float in many operations, but it is not a perfect substitute for hardware double.
Important limits:
- The exponent range is still basically the
floatexponent range. - Every arithmetic operation must be implemented carefully.
- Transcendental functions such as
sinorlogneed custom versions if you want consistent extra precision. - Compiler optimizations that reorder floating-point expressions can break carefully designed error tracking.
In other words, two floats can emulate higher precision arithmetic, but they do not magically become the built-in double type.
Where This Technique Is Useful
Historically, GPUs and older SIMD pipelines made double-single arithmetic attractive because single-precision math was much faster. It also appears in code that wants predictable storage layouts or must run on systems where double support is restricted.
For application code on modern desktop or server CPUs, native double is usually the better choice. It is simpler, faster to maintain, and integrates cleanly with math libraries. Emulating double with two float values only makes sense when you have a concrete platform or compatibility constraint.
If you need even more precision, libraries based on arbitrary-precision arithmetic are often a better investment than hand-rolled multi-float types.
Common Pitfalls
A common misconception is that storing two unrelated float values gives you extra precision automatically. It does not. The pair only works when each arithmetic operation is designed to preserve the rounding residual in the low component.
Another pitfall is ignoring normalization. If lo becomes too large relative to hi, later operations lose the benefit of the split representation. Normalize after arithmetic so that hi carries the main magnitude and lo stays small.
Performance can also disappoint. Double-single arithmetic often uses many more instructions than native double, so it is only worthwhile when the target hardware strongly favors float.
Finally, test with adversarial inputs, not only easy values. Large plus small, cancellation, and repeated accumulation are the cases that reveal whether the implementation is actually preserving precision.
Summary
- A higher-precision value can be represented as
hi + lousing twofloatvalues. - The technique is usually called double-single arithmetic.
- Extra precision only appears if operations preserve the rounding error explicitly.
- The exponent range still behaves like
float, not true IEEEdouble. - Use it only when native
doubleis unavailable, too slow, or otherwise unsuitable.
Related reading
- Erasing elements in stdvector by using indexes
- Error Microsoft Visual C 14.0 is required Unable to find vcvarsall.bat
- error static assertion failed stdthread arguments must be invocable after conversion to rvalues
- Error when refering to the pointer to a structure after using memcpy()
- Example for boost shared_mutex multiple reads/one write?
- Export Python TensorFlow model and import it in C
- Export Tensorflow graphs from Python for use in C
- Fast ceiling of an integer division in C / C
.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.