diagnostics formatting helper: set limited precision for doubles
this deals with a recurring problem in test code: very common "simple" fractional values can not be represented precisely as binary floating point. The classical example is 0.1 Since this is a diagnostics facility, we can cheat around this insidious problem by just setting a limited rendering precision. Floating point numbers behave deterministic; you just need to know how to deal with limited precision.
This commit is contained in:
parent
761154ae63
commit
7d9108a079
1 changed files with 23 additions and 0 deletions
|
|
@ -71,6 +71,9 @@ namespace util {
|
|||
namespace { // we need to guard the string conversion
|
||||
// to avoid a compiler error in case the type isn't convertible....
|
||||
|
||||
// precision for rendering of double values
|
||||
const auto DIAGNOSTICS_DOUBLE_PRECISION = 8;
|
||||
|
||||
|
||||
template<typename X>
|
||||
struct use_StringConversion : can_ToString<X> { };
|
||||
|
|
@ -108,6 +111,26 @@ namespace util {
|
|||
try { return boost::lexical_cast<string> (val); }
|
||||
catch(...) { return ""; }
|
||||
};
|
||||
|
||||
/** explicit specialisation to control precision of double values.
|
||||
* @note we set an explicit precision, since this is a diagnostic facility
|
||||
* and we typically do not want to see all digits, but, for test code,
|
||||
* we do want a predictable string representation of simple fractional
|
||||
* values like `0.1` (which can not be represented as binary floats)
|
||||
*/
|
||||
template<>
|
||||
struct _InvokeFailsafe<double>
|
||||
{
|
||||
static string
|
||||
toString (double const& val)
|
||||
try {
|
||||
std::ostringstream buffer;
|
||||
buffer.precision(DIAGNOSTICS_DOUBLE_PRECISION);
|
||||
buffer << val;
|
||||
return buffer.str();
|
||||
}
|
||||
catch(...) { return ""; }
|
||||
};
|
||||
}//(End) guards/helpers
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue