Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Adjusting printf() rounding Date: Sat, 09 Apr 2022 09:28:29 -0700 Organization: A noiseless patient Spider Lines: 36 Message-ID: <86ilri9q6q.fsf@linuxsc.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="b8e2dcfc2935dcfe9151ae6ef6f0e059"; logging-data="10326"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18B8yxg8goTeaWMa8OhvgB/X76F0oGbuSY=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:8devfT3pih4HhYThJK07/tybsJY= sha1:fypu+PoC+V2n046f5ruplE20n5A= Xref: csiph.com comp.lang.c:165594 James Kuyper writes: > On 4/8/22 08:40, Paavo Helde wrote: > >> To be honest, this is a bit off-topic for C++, but maybe you guys >> have some insight here. >> >> [.. behavior of rounding on floating-point output ..] >> >> A demo program: >> >> $ cat test6.cpp >> #include >> int main() { >> std::printf("%.3g\n", 212.5); >> } >> $ g++ test6.cpp >> $ ./a.out >> 212 > > I know nothing MSVC-specific, but I can tell you what the C > standard requires: > > When printing floating point numbers, the *printf*() family is > supposed to obey the current rounding direction. > [.. discussion of rounding modes and how to set them ..] I believe this assertion is an overstatement. Although there are certain cases where the C standard does mandate correct rounding (which implies using the current rounding direction), AFAICT there is no general requirement that all *printf() floating-point conversions be correctly rounded, or that they obey the current rounding direction. Note that some statements are made about rounding (both correct rounding and current rounding direction) under a "Recommended practice" heading, but these statements are only recommendations, not requirements.