Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #83528
| From | Paavo Helde <eesnimi@osa.pri.ee> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Adjusting printf() rounding |
| Date | 2022-04-08 15:40 +0300 |
| Organization | A noiseless patient Spider |
| Message-ID | <t2pag0$eab$1@dont-email.me> (permalink) |
To be honest, this is a bit off-topic for C++, but maybe you guys have
some insight here.
This is an excerpt from
"https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/printf-printf-l-wprintf-wprintf-l?view=msvc-170"
"Starting in Windows 10 version 2004 (build 19041), the printf family of
functions prints exactly representable floating point numbers according
to the IEEE 754 rules for rounding. In previous versions of Windows,
exactly representable floating point numbers ending in '5' would always
round up. IEEE 754 states that they must round to the closest even digit
(also known as "Banker's Rounding"). For example, both printf("%1.0f",
1.5) and printf("%1.0f", 2.5) should round to 2. Previously, 1.5 would
round to 2 and 2.5 would round to 3. [...] This change only affects
programs built using Visual Studio 2019 version 16.2 and later."
glibc has similar page and similar rules, though they do not speak about
printf or iostream directly:
"https://www.gnu.org/software/libc/manual/html_node/Rounding.html"
I can understand that rounding-to-even rule is good in mathematical
operations as it is statistically unbiased. However, with printf the
rounding appears in decimal, not in binary, and the result is meant
primarily for human consumption, not for statistics. It is not clear to
me these things should follow the same ideology, what do you think?
What's worse, it appears MSVC have not kept the promises which they
made, and have reverted back to pre-MSVC19 behavior in VS2019 Update 11
(_MSC_VER==1929), rounding ties now up again.
For me this means we have nasty cross-platform differences which we
would rather try to avoid. How could we coerce glibc() to round up, or
failing that, coerce MSVC to round down? I was thinking about adding or
subtracting one ULP to the number beforehand, but this would surely ruin
some other outputs (in my program I have no control over the numbers or
precisions, these come from outside).
A demo program:
$ cat test6.cpp
#include <cstdio>
int main() {
std::printf("%.3g\n", 212.5);
}
$ g++ test6.cpp
$ ./a.out
212
TIA
Paavo
Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread
Adjusting printf() rounding Paavo Helde <eesnimi@osa.pri.ee> - 2022-04-08 15:40 +0300
Re: Adjusting printf() rounding Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-04-08 06:02 -0700
Re: Adjusting printf() rounding Marcel Mueller <news.5.maazl@spamgourmet.org> - 2022-04-08 18:21 +0200
Re: Adjusting printf() rounding Paavo Helde <eesnimi@osa.pri.ee> - 2022-04-08 19:38 +0300
Re: Adjusting printf() rounding Sams Lara <samlara622@gmail.com> - 2022-04-08 18:48 +0100
Re: Adjusting printf() rounding Paavo Helde <eesnimi@osa.pri.ee> - 2022-04-08 23:44 +0300
Re: Adjusting printf() rounding Marcel Mueller <news.5.maazl@spamgourmet.org> - 2022-04-09 12:58 +0200
Re: Adjusting printf() rounding Geoff <geoff@invalid.invalid> - 2022-04-09 08:45 -0700
Re: Adjusting printf() rounding Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2022-04-09 13:05 -0700
Re: Adjusting printf() rounding red floyd <no.spam.here@its.invalid> - 2022-04-09 13:36 -0700
Re: Adjusting printf() rounding Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-04-09 16:08 -0700
Re: Adjusting printf() rounding Manfred <noname@add.invalid> - 2022-04-08 21:30 +0200
Re: Adjusting printf() rounding "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2022-04-09 10:11 +0200
Re: Adjusting printf() rounding Paavo Helde <eesnimi@osa.pri.ee> - 2022-04-10 16:39 +0300
Re: Adjusting printf() rounding Sams Lara <samlara622@gmail.com> - 2022-04-10 18:36 +0100
Re: Adjusting printf() rounding Paavo Helde <eesnimi@osa.pri.ee> - 2022-04-10 21:50 +0300
Re: Adjusting printf() rounding Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-04-09 09:05 -0700
Re: Adjusting printf() rounding Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-04-09 09:10 -0700
csiph-web