}
template<typename W, int P, bool IM>
-std::ostream &Real<W, P, IM>::AsFortran(std::ostream &o, int kind) const {
+std::ostream &Real<W, P, IM>::AsFortran(
+ std::ostream &o, int kind, bool minimal) const {
if (IsNotANumber()) {
o << "(0._" << kind << "/0.)";
} else if (IsInfinite()) {
using B = decimal::BinaryFloatingPointNumber<P>;
const auto *value{reinterpret_cast<const B *>(this)};
char buffer[24000]; // accommodate real*16
- auto result{decimal::ConvertToDecimal<P>(buffer, sizeof buffer,
- static_cast<decimal::DecimalConversionFlags>(0),
+ decimal::DecimalConversionFlags flags{}; // default: exact representation
+ if (minimal) {
+ flags = decimal::Minimize;
+ }
+ auto result{decimal::ConvertToDecimal<P>(buffer, sizeof buffer, flags,
static_cast<int>(sizeof buffer), decimal::RoundNearest, *value)};
const char *p{result.str};
if (DEREF(p) == '-' || *p == '+') {
// Emits a character representation for an equivalent Fortran constant
// or parenthesized constant expression that produces this value.
- std::ostream &AsFortran(std::ostream &, int kind) const;
+ std::ostream &AsFortran(std::ostream &, int kind, bool minimal = false) const;
private:
using Significand = Integer<significandBits>; // no implicit bit
MATCH(x, ivf.value.ToUInt64())(ldesc);
if (rounding.mode == RoundingMode::TiesToEven) { // to match stold()
std::stringstream ss;
- vr.value.AsFortran(ss, kind);
+ vr.value.AsFortran(ss, kind, false /*exact*/);
std::string decimal{ss.str()};
const char *p{decimal.data()};
MATCH(x, static_cast<std::uint64_t>(std::stold(decimal)))
static constexpr int kind{REAL::bits / 8};
std::stringstream ss, css;
- x.AsFortran(ss, kind);
+ x.AsFortran(ss, kind, false /*exact*/);
std::string s{ss.str()};
if (IsNaN(rj)) {
css << "(0._" << kind << "/0.)";