Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<xlocnum>: Drop ios_base::showpoint flag when printing non-finite values #4212

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions stl/inc/xlocnum
Original file line number Diff line number Diff line change
Expand Up @@ -1371,10 +1371,13 @@ protected:
}

_Buf.resize(_Bufsize + 50); // add fudge factor
const bool _Is_finite = (_STD isfinite)(_Val);
const auto _Adjusted_flags = // TRANSITION, DevCom-10519861
_Is_finite ? _Iosbase.flags() : _Iosbase.flags() & ~ios_base::showpoint;
const auto _Ngen = static_cast<size_t>(_CSTD sprintf_s(
&_Buf[0], _Buf.size(), _Ffmt(_Fmt, 0, _Iosbase.flags()), static_cast<int>(_Precision), _Val));
&_Buf[0], _Buf.size(), _Ffmt(_Fmt, 0, _Adjusted_flags), static_cast<int>(_Precision), _Val));

return _Fput_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, (_STD isfinite)(_Val));
return _Fput_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, _Is_finite);
}

virtual _OutIt __CLR_OR_THIS_CALL do_put(
Expand All @@ -1395,10 +1398,13 @@ protected:
}

_Buf.resize(_Bufsize + 50); // add fudge factor
const bool _Is_finite = (_STD isfinite)(_Val);
const auto _Adjusted_flags = // TRANSITION, DevCom-10519861
_Is_finite ? _Iosbase.flags() : _Iosbase.flags() & ~ios_base::showpoint;
const auto _Ngen = static_cast<size_t>(_CSTD sprintf_s(
&_Buf[0], _Buf.size(), _Ffmt(_Fmt, 'L', _Iosbase.flags()), static_cast<int>(_Precision), _Val));
&_Buf[0], _Buf.size(), _Ffmt(_Fmt, 'L', _Adjusted_flags), static_cast<int>(_Precision), _Val));

return _Fput_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, (_STD isfinite)(_Val));
return _Fput_v3(_Dest, _Iosbase, _Fill, _Buf.c_str(), _Ngen, _Is_finite);
}
#pragma warning(pop)

Expand Down
49 changes: 49 additions & 0 deletions tests/std/tests/GH_003867_output_nan/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <cassert>
#include <iomanip>
#include <limits>
#include <locale>
#include <sstream>
Expand Down Expand Up @@ -54,8 +55,56 @@ void test_gh_3867() {
#endif // TEST_CUSTOM_FACET
}

// Also test GH-4210: With setprecision(0) showpoint fixed, a bogus '.' is emitted for infinity and NaN

template <class FloatingPoint>
void test_output_nonfinite_value(const FloatingPoint x) {
const auto s1 = [x] {
ostringstream os;
os << setprecision(0) << showpoint << fixed;
os << x;
return os.str();
}();
const auto s2 = [x] {
ostringstream os;
os << setprecision(0) << noshowpoint << fixed;
os << x;
return os.str();
}();
assert(s1 == s2);

const auto s3 = [x] {
ostringstream os;
os << setprecision(0) << showpoint << fixed << showpos;
os << x;
return os.str();
}();
const auto s4 = [x] {
ostringstream os;
os << setprecision(0) << noshowpoint << fixed << showpos;
os << x;
return os.str();
}();
assert(s3 == s4);
}

template <class FloatingPoint>
void test_gh_4210() {
constexpr auto inf_val = numeric_limits<FloatingPoint>::infinity();
constexpr auto nan_val = numeric_limits<FloatingPoint>::quiet_NaN();

test_output_nonfinite_value(inf_val);
test_output_nonfinite_value(-inf_val);
test_output_nonfinite_value(nan_val);
test_output_nonfinite_value(-nan_val);
}

int main() {
test_gh_3867<float>();
test_gh_3867<double>();
test_gh_3867<long double>();

test_gh_4210<float>();
test_gh_4210<double>();
test_gh_4210<long double>();
}