Precision
This module uses native hardware precision. A double provides approximately 15–17 significant decimal digits — the same precision as any standard calculator or spreadsheet. There are no precision tiers to choose from; what you see is what the CPU computes.
Exact vs. Approximate Results
- = green — The displayed result is the exact internal value. This applies to all integer arithmetic (
1+2 = 3), and to floating-point values that happen to be exactly representable in binary, such as0.75,0.5,0.125, or100000. - ≈ orange — The displayed result is a clean rounded view. Click to expand and see the full internal value. This happens whenever a decimal number cannot be represented exactly in binary. For example,
0.1is stored internally as0.100000000000000005551115123...and0.1+0.2evaluates to0.300000000000000044408920985..., not0.3.
This is not a bug — it is how IEEE 754 binary floating-point works in every programming language and processor. The ≈ signal makes this visible rather than hiding it.
When to Use
Choose the c++ module when you want standard calculator behavior, fast execution, and compatibility with how numbers work in C, C++, Java, Python, JavaScript, and most other languages. It is the best module for understanding how your code actually computes.
Reference
- Standard: ISO/IEC 14882:2023 (C++23)
- Author: Bjarne Stroustrup
- Documentation: cppreference.com
Unlike binary floating-point, numbers are stored in base 10 — meaning 0.1+0.2=0.3 exactly, with no rounding error.
Precision Tiers
| Tier | Guaranteed Digits | Best For |
|---|---|---|
| 50 | 50 decimal digits | General-purpose high precision |
| 100 | 100 decimal digits | Financial modeling, scientific constants |
| 500 | 500 decimal digits | Extreme precision requirements |
All tiers are available for both real and complex types.
Exact vs. Approximate Results
- = green — The displayed result is exact. Because this module stores values in base 10, common decimal fractions are represented without error. Most results you will see with this module are green.
- ≈ orange — The displayed result has been rounded because it exceeds the display width. Click to expand and see more digits. This typically happens with expressions like
1/3at high precision.
Note: ≈ in this module does not indicate a binary rounding error (there are none). It simply means more digits are available than what is displayed.
When to Use
Choose the dec module for financial calculations, currency conversions, or any domain where decimal exactness matters. It eliminates the class of bugs caused by binary floating-point representation.
Reference
- Library: Boost.Multiprecision (
cpp_dec_float) - Version: 1.87.0
- License: BSL-1.0 (Boost Software License)
- Author: John Maddock
- Documentation: boost.org/multiprecision
Its distinguishing feature is exact rational arithmetic: 1/3 stays as the fraction 1/3, and 1/3*3=1 exactly — no rounding, no truncation.
Type System
- mpz — Arbitrary-precision integers. No overflow, no limit.
- mpq — Exact rationals (fractions).
1/3+1/6=1/2with zero error. This is the default integer type, so1/2evaluates to the fraction1/2rather than0. - mpf50 / mpf100 / mpf500 — Binary floating-point at 50, 100, or 500 digit precision. Used when operations leave the rational domain (e.g.,
sqrt(2)). - rqc — Rational complex. Exact complex arithmetic with two mpq components:
(1+i)/(1-i) = iexactly. When irrational operations are needed, values promote to float-backed complex (gmpc50/100/500).
Precision Tiers
| Tier | Integer / Rational | Float | Complex |
|---|---|---|---|
| 50 | Exact (unlimited) | 50 decimal digits | Exact rational or 50-digit float |
| 100 | Exact (unlimited) | 100 decimal digits | Exact rational or 100-digit float |
| 500 | Exact (unlimited) | 500 decimal digits | Exact rational or 500-digit float |
The precision tier only affects floating-point operations. Integers and rationals are always exact regardless of the selected tier.
Exact vs. Approximate Results
- = green — The displayed result is exact.
- ≈ orange — The displayed result is a clean rounded view of a binary float. Click to expand. This happens when the result is a
mpffloat — for example,0.1stored in binary is0.0999...999at 50-digit precision.
The key advantage: if you stay within integer and rational operations, everything is green. Binary approximation only appears when you explicitly use decimal literals (0.1) or transcendental functions (sin, sqrt).
When to Use
Choose the gmp module for number theory, exact arithmetic, symbolic-style computation, or any domain where fractions must remain exact. It is the best module for educational contexts where 1/3*3 should equal 1, and for cryptographic applications requiring arbitrary-precision integers.
Reference
- Library: GMP (GNU Multiple Precision Arithmetic Library)
- Version: 6.3.0
- License: LGPL-3.0
- Author: GNU Project
- Documentation: gmplib.org
The interval module wraps every number in a guaranteed enclosure [lower, upper]. The true mathematical result is always contained within the interval bounds. This makes rounding errors visible and quantifiable instead of hidden.
How It Works
Every floating-point operation is computed twice: once rounding down (for the lower bound) and once rounding up (for the upper bound). The resulting interval is guaranteed to contain the exact mathematical result. When both bounds are equal, the result is exact. When they differ, the gap reveals the accumulated rounding error.
Types
| Type | Wraps | Precision |
|---|---|---|
| ifloat | interval<float> | ~7 decimal digits |
| idouble (default) | interval<double> | ~15 decimal digits |
| ildouble | interval<long double> | ~18 decimal digits |
Unsuffixed literals (0.1, 1.0) default to ildouble. Use explicit constructors for other types: ifloat(0.1), ildouble(0.1).
The Classic Example
Try 0.1+0.2. In every other module, you get a single number. Here you get an interval:
- Display:
≈ [0.3, 0.3]— the smart-rounded view looks clean - Expand:
[0.29999999999999999, 0.30000000000000004]— the true bounds reveal that 0.3 is enclosed but not exact
Compare with 0.5+0.5 which gives = [1, 1] — a point interval, because 0.5 and 1.0 are exactly representable in binary.
Exact vs. Approximate Results
- = green — Both bounds are identical: the result is a point interval with no rounding error. Examples:
0.5+0.5,2.0*3.0,0.5+0.25. - ≈ orange — The bounds differ: rounding error is present. Click to expand and see the exact bounds. The gap between bounds quantifies the accumulated error.
Comparisons and Indeterminacy
Comparing intervals is fundamentally different from comparing numbers. When two intervals overlap, the comparison is indeterminate: neither true nor false can be guaranteed. In that case, the module throws a comparison_error.
| Expression | Result | Why |
|---|---|---|
1.0+1.0==2.0 | true | Both sides are point intervals, identical |
0.1+0.2==0.3 | uncertain comparison | Intervals overlap — cannot determine equality |
This is not a limitation — it is the mathematically correct answer. If two intervals overlap, claiming they are equal or unequal would be a lie. The module refuses to guess.
When to Use
Choose the ival module to understand and visualize rounding error propagation. It is ideal for numerical analysis education, validating the stability of algorithms, and any context where you need a guaranteed enclosure of the true result rather than a best-effort approximation.
Reference
- Library: Boost.Numeric.Interval
- Version: 1.87.0
- License: BSL-1.0 (Boost Software License)
- Author: Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
- Documentation: boost.org/numeric/interval
The mpfr module provides high-precision binary floating-point and complex arithmetic using the MPFR and MPC libraries. It is the go-to module for scientific computing where you need many digits of precision with correctly-rounded results.
Precision Tiers
| Tier | Real Type | Complex Type | Guaranteed Digits |
|---|---|---|---|
| 50 | mpfr50 | mpc50 | 50 decimal digits |
| 100 | mpfr100 | mpc100 | 100 decimal digits |
| 500 | mpfr500 | mpc500 | 500 decimal digits |
MPFR guarantees correct rounding for every basic operation: the result is the closest representable value to the exact mathematical result.
Exact vs. Approximate Results
- = green — The displayed result is the exact internal value. This applies to values that happen to be exactly representable in binary:
0.75,0.5+0.25,0, and integer-valued floats like100000. - ≈ orange — The displayed result is a clean rounded view. Click to expand and see the full-precision value. Like all binary floating-point, decimal fractions such as
0.1and0.1+0.2are not exact.
The key difference from c++: with 50–500 digits of precision, the binary error is pushed far to the right. The result is still approximate, but the approximation is vastly more precise than a double.
Comparison with Other Modules
| Expression | c++ (double) | mpfr50 | dec50 | gmp (mpq) |
|---|---|---|---|---|
0.1+0.2 | ≈ 0.3 (16 digits) | ≈ 0.3 (50 digits) | = 0.3 (exact) | ≈ 0.3 (50 digits) |
0.75 | = 0.75 | = 0.75 | = 0.75 | = 0.75 |
1/3 | N/A | ≈ 0.333… | ≈ 0.333… | = 1/3 |
sqrt(2) | ≈ 1.414… (16 digits) | ≈ 1.414… (50 digits) | ≈ 1.414… (50 digits) | ≈ 1.414… (50 digits) |
MPFR excels at transcendental functions (sin, cos, exp, log, sqrt) where every digit is correctly rounded. For pure rational arithmetic, consider the gmp module instead.
When to Use
Choose the mpfr module for scientific calculations requiring many digits of verified precision: computing mathematical constants, evaluating convergence of series, numerical analysis, or any domain where you need to distinguish between true results and floating-point artifacts.
Reference
- Library: MPFR (floats) + MPC (complex)
- Version: MPFR 4.2.1 / MPC 1.3.1
- License: LGPL-3.0
- Author: GNU Project
- Documentation: mpfr.org
The bin module provides high-precision binary floating-point and complex arithmetic using Boost.Multiprecision cpp_bin_float. It is a pure C++ alternative to mpfr — no external library required — with configurable decimal digit precision.
Precision Tiers
| Tier | Real Type | Complex Type | Precision |
|---|---|---|---|
| 50 | bin50 | binc50 | 50 decimal digits |
| 100 | bin100 | binc100 | 100 decimal digits |
| 500 | bin500 | binc500 | 500 decimal digits |
Exact vs. Approximate Results
- = green — The displayed result is the exact internal value. Values exactly representable in binary —
0.75,0.5,0.125— are always green. - ≈ orange — The displayed result is a clean rounded view. Click to expand and see the full-precision value. Like all binary floating-point, decimal fractions such as
0.1and0.1+0.2are not exactly representable in binary.
The behavior is identical to mpfr: binary representation errors exist, but are pushed much further right with each precision tier.
Comparison with mpfr
| bin | mpfr | |
|---|---|---|
| Backend | Pure C++ (Boost, header-only) | MPFR + MPC (C library) |
| Dependency | None beyond Boost | libmpfr, libmpc, libgmp |
| Correct rounding | Basic operations (+−×÷, sqrt) | All operations including transcendentals |
| Transcendentals | Via Boost.Math (not all guaranteed) | Full support (sin, cos, exp, log…) |
When to Use
Choose the bin module when you want high-precision binary floating-point without any external library dependency. It is a good drop-in replacement for mpfr in environments where the MPFR library is unavailable. For certified scientific results requiring correctly-rounded transcendentals, prefer mpfr.
Reference
- Library: Boost.Multiprecision (
cpp_bin_float) - Version: 1.87.0
- License: BSL-1.0 (Boost Software License)
- Author: John Maddock
- Documentation: boost.org — cpp_bin_float
The int module provides exact integer arithmetic at arbitrary or fixed precision using Boost.Multiprecision cpp_int. Every result is exact — no floating-point, no rounding, and no overflow for the arbitrary-precision type.
Type System
| Type | Width | Sign | Range |
|---|---|---|---|
| cpp_int | Arbitrary (grows as needed) | Signed | Unlimited — never overflows |
| int128 / int256 / int512 / int1024 | Fixed | Signed | ±2N−1 |
| uint128 / uint256 / uint512 / uint1024 | Fixed | Unsigned | 0 to 2N−1 |
Unsuffixed integer literals default to cpp_int. Use explicit type names for fixed-width: int512(x), uint256(x).
Exact vs. Approximate Results
- = green — Always. Integer arithmetic is exact by definition. There is no ≈ in this module.
Overflow Behavior
- cpp_int — Never overflows. The value grows dynamically to accommodate any result.
2^1000is computed exactly. - intN / uintN — Overflow wraps silently (unchecked mode). Use
cpp_intif overflow safety matters.
Note: unary minus is not defined for unsigned types (uint128, etc.) — negating an unsigned value is mathematically undefined.
Division
Division between integers performs integer division truncated toward zero: 7/2 = 3, -7/2 = -3. For exact rational results, use the gmp module (mpq).
When to Use
Choose the int module for combinatorics, cryptography, number theory, or any computation requiring exact integer results beyond the 64-bit limit. cpp_int is ideal for factorials, large powers, and primality testing. Fixed-width types are useful when a specific bit width is required, for example to match a hardware or protocol specification.
Reference
- Library: Boost.Multiprecision (
cpp_int) - Version: 1.87.0
- License: BSL-1.0 (Boost Software License)
- Author: John Maddock
- Documentation: boost.org — cpp_int