Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
operations_research::RoundTripDoubleFormat Class Reference

#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)
 

Detailed Description

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));

Note
some operating systems do not support std::to_chars() for double yet but only implement it for integers (see kStdToCharsDoubleIsSupported). On those operating systems, a formatting equivalent to using "%.*g" with max_digits10 precision is used.

Definition at line 55 of file fp_roundtrip_conv.h.

Constructor & Destructor Documentation

◆ RoundTripDoubleFormat()

operations_research::RoundTripDoubleFormat::RoundTripDoubleFormat ( const double value)
inlineexplicit

Definition at line 57 of file fp_roundtrip_conv.h.

Member Function Documentation

◆ Parse()

absl::StatusOr< double > operations_research::RoundTripDoubleFormat::Parse ( absl::string_view str_value)
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.

◆ ToString()

std::string operations_research::RoundTripDoubleFormat::ToString ( double value)
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.

Friends And Related Symbol Documentation

◆ AbslStringify

template<typename Sink >
void AbslStringify ( Sink & sink,
const RoundTripDoubleFormat & format )
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.

◆ operator<<

std::ostream & operator<< ( std::ostream & out,
const RoundTripDoubleFormat & format )
friend

Prints the formatted double to the provided stream.

Definition at line 110 of file fp_roundtrip_conv.cc.


The documentation for this class was generated from the following files: