Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <fp_roundtrip_conv.h>
Public Member Functions | |
RoundTripDoubleFormat (const double value) | |
Static Public Member Functions | |
static std::string | ToString (double value) |
static absl::StatusOr< double > | Parse (absl::string_view str_value) |
Friends | |
std::ostream & | operator<< (std::ostream &out, const RoundTripDoubleFormat &format) |
Prints the formatted double to the provided stream. | |
template<typename Sink > | |
void | AbslStringify (Sink &sink, const RoundTripDoubleFormat &format) |
Formatter using std::to_chars() to print a double
so that a round trip conversion back to double will result in the same number (using absl::from_chars()). One exception are NaNs that may not round trip (i.e. multiple NaNs could end up being printed the same).
Usage:
const double x = ...; LOG(INFO) << "x: " << RoundTripDoubleFormat(x);
const std::string x_str = absl::StrCat("x: ", RoundTripDoubleFormat(x));
ASSIGN_OR_RETURN(const double y, RoundTripDoubleFormat::Parse(x_str));
Definition at line 55 of file fp_roundtrip_conv.h.
|
inlineexplicit |
Definition at line 57 of file fp_roundtrip_conv.h.
|
static |
Parses the input string with absl::from_chars(), returning an error if the input string does not start with a number or has extra characters after it. It also fails if the number can't fit in a double
.
This function offers a round-trip from string printed/built by this formatter.
Definition at line 122 of file fp_roundtrip_conv.cc.
|
static |
Returns a string with the provided double formatted.
Prefer using operator<< when possible (with LOG(), StatusBuilder, std::cout...) since it avoids allocating a temporary string.
PERFORMANCE: operator<< may be noticeably faster in some extreme cases, especially in non-64bit platforms or when value is in (-∞, -1e+100] or in [-1e-100, 0). This is because the string won't fit in small-string-optimization buffer, and will thus need a heap memory allocation which is slow.
Definition at line 117 of file fp_roundtrip_conv.cc.
|
friend |
Prints the formatted double to the provided sink.
PERFORMANCE: the current implementation uses ToString() as the code has to be templated and thus inlined. It could be optimized a bit by using the same implementation pattern as operator<< (i.e. a stack-allocated buffer) but this would require exposing some internals here. It may not even be that bad for most practical case for the rationale explained in ToString() documentation.
Definition at line 72 of file fp_roundtrip_conv.h.
|
friend |
Prints the formatted double to the provided stream.
Definition at line 110 of file fp_roundtrip_conv.cc.