Design Decisions and Standards Deviations
C++14 Requirement
IEEE Deviations
Not All <cmath> Functions Meet the Definition of Correctly Rounded
IEEE 754 specifies for correct rounding: "This standard’s method of converting an infinitely precise result to a floating-point number, as determined by the applicable rounding direction. A floating-point number so obtained is said to be correctly rounded."
With the default evaluation method, the functions in this list compute in the type of the argument and round one time to that type, in the current rounding mode.
With a wider evaluation method, they round one time to the wider type, and the conversion to the type of the result can round again.
Most paths use an error bound to find if the result is clear of a rounding boundary, and if not, they compute again with more digits.
The last computation has no such check.
For some arguments, such as small arguments of log1p and acosh, the first computation is also the last.
-
exp,exp2,expm1,log,log10,log2,log1p -
sinh,cosh,tanh,asinh,acosh,atanh
The aim is a correctly rounded result, but there is no proof for all arguments.
A unit in the last place (ULP) is the distance between two adjacent values of the type.
An error of at most 0.5 ULP gives the nearest value in a nearest mode, but it does not select the value at a midpoint or the side in a directed mode: correct rounding in the current mode selects both.
A search of the arguments of log, log10 and log2 for decimal32_t and decimal64_t, and of log1p for decimal32_t, found each result farther from a rounding boundary than the error of the last computation.
The search covers all positive finite arguments in the canonical encoding, subnormal arguments included, in the five rounding modes.
It does not include the arguments with an exact result: 1, the powers of 10 for log10 and the powers of 2 for log2.
For decimal32_t, the search computes each argument.
For decimal64_t, it does not: a lattice method finds all the arguments whose result is within 10-15 ULP of a rounding boundary, and the search checks each of these.
This search is a test of the arguments, not a proof of the error bound, and it is not in this repository.
For the 128-bit types, exp, exp2, expm1, sinh, cosh and tanh have known arguments with a result next to the correctly rounded value.
sqrt and cbrt are correctly rounded for finite arguments in their canonical encoding, when they compute in the type of the argument.
The other elementary and special functions, such as pow, sin and tgamma, do not meet the definition of correctly rounded.
From the Intel libdfp README: "binary floating-point mathematical functions
implemented for example in C or other compiler math libraries are in general
not correctly rounded either".
Technically IEEE 754 differentiates the specification for mandatory operations (e.g. fma, sqrt, etc.) in Clause 5, and recommended operations (e.g. exp, log, etc.) in Clause 9, but this section applies to both.
This does not apply to Clause 5 functions like isnan which has a boolean result because ULP precision does not apply.
Floating Point Exception Flags are not Supported
The normal method in C++ to set and get the current floating point exception flags is via std::fegetexceptflag and std::fesetexceptflag.
The decision then becomes do we support constexpr or floating point exceptions?
We find that the former is far more useful than the latter.
The alternative to would be to use C style functions such as:
decimal64_t dec64_add(decimal64_t a, decimal64_t b, rounding_mode round, int* floating_point_flags);
This style of function is used in Intel libdfp because it is a C library. We find this to be unidiomatic and unergonomic for a modern C++ library.
C++ Standards Deviations
from_chars Distinguishes Overflow from Underflow
std::from_chars has an open issue with LWG here: https://cplusplus.github.io/LWG/lwg-active.html#3081.
The standard for <charconv> does not distinguish between underflow and overflow like strtod does.
boost::decimal::from_chars modifies value in order to communicate this to the user in a divergence from the standard.
This behavior is the same as that of boost::charconv::from_chars_erange.
from_chars Supports Rounding Mode
The C++ specification states that from_chars resulting value is to be rounded to nearest.
Since the types in this library are more sensitive to rounding mode differences, from_chars rounds using the current global rounding mode as reported by fegetround().
from_chars and to_chars both have an additional chars_format option
In order to support an IEEE 754 requirement to print a value to include its cohort information we introduced an additional chars_format option, cohort_preserving_scientific.
Scientific notation is the natural fit for cohort preservation because it mirrors the actual layout of the type, and it can represent every member of a cohort.
A second option, cohort_preserving_fixed, offers the same guarantee in fixed notation with an implied exponent of zero.
Because fixed notation has no exponent field, to_chars can only preserve members whose exponent is not positive; a positive exponent would require appending trailing zeros, which changes the cohort member that is recovered on read-back, so such values are rejected.
istream of Non-finite Values is Allowed
Unlike built-in binary floating point types the library allows input of non-finite values such as the below:
std::istringstream is("INF");
decimal32_t x;
is >> x;
const auto endpos {is.tellg()};
assert(isinf(x));
assert(endpos == 3);
The result of tellg() will only parse until a complete non-finite value is found and then terminate.
For example nanfinity will construct a NAN and set endpos = 3, or nan(snan)JUNK will construct SNAN and set endpos = 9 since nan(snan) contains a valid payload.
This is designed to match the value of r.ptr in <charconv>.
Locale Facets are Unsupported
Facets as discussed in N2849 section 3.10 are unimplemented.