Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

One does not thank logic. -- Sarek, "Journey to Babel", stardate 3842.4


devel / comp.lang.c / Re: Adjusting printf() rounding

SubjectAuthor
* Re: Adjusting printf() roundingJames Kuyper
`* Re: Adjusting printf() roundingTim Rentsch
 `- Re: Adjusting printf() roundingjames...@alumni.caltech.edu

1
Re: Adjusting printf() rounding

<t2pn31$jkc$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21113&group=comp.lang.c#21113

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Adjusting printf() rounding
Date: Fri, 8 Apr 2022 12:15:28 -0400
Organization: A noiseless patient Spider
Lines: 76
Message-ID: <t2pn31$jkc$1@dont-email.me>
References: <t2pag0$eab$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 8 Apr 2022 16:15:29 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="dc110a91ed9ca0c8ea17ca8aa2afce30";
logging-data="20108"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sYHMxRAehHeEqBRv4UMkdpZ2Icixaj4U="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.7.0
Cancel-Lock: sha1:e0oErFghS0aJjy4+oQykBiZt6P4=
In-Reply-To: <t2pag0$eab$1@dont-email.me>
Content-Language: en-US
 by: James Kuyper - Fri, 8 Apr 2022 16:15 UTC

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.
>
> 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

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.

<fenv.h> defines macros for rounding directions. A given macro is
defined if and only if the implementation supports that rounding
direction. The standard describes 5 rounding modes, implementations are
allowed to add to that list.

#pragma STDC FENV_ROUND

May be used to set the rounding direction. One option is FE_DYNAMIC,
which allows the rounding direction to be changed by calls to
fesetround(). Be sure to check the return value from fesetround() - it's
not required to succeed.

If this worries you, it might be a good idea to use one of the new
decimal floating point types: _Decimal32, _Decimal64, or _Decimal128, if
your implementation supports them. The decimal floating point types have
their own equivalents of all the rounding direction features I described
above. The decimal floating point rounding direction need not be the
same as the regular floating point types.

Re: Adjusting printf() rounding

<86ilri9q6q.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21131&group=comp.lang.c#21131

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (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: <t2pag0$eab$1@dont-email.me> <t2pn31$jkc$1@dont-email.me>
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=
 by: Tim Rentsch - Sat, 9 Apr 2022 16:28 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> 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 <cstdio>
>> 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.

Re: Adjusting printf() rounding

<61a0874c-6851-400d-bd03-9ba116801c75n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21132&group=comp.lang.c#21132

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a0c:c788:0:b0:444:2c7f:4126 with SMTP id k8-20020a0cc788000000b004442c7f4126mr6127551qvj.50.1649562661372;
Sat, 09 Apr 2022 20:51:01 -0700 (PDT)
X-Received: by 2002:a05:620a:4047:b0:67d:6729:b241 with SMTP id
i7-20020a05620a404700b0067d6729b241mr17870066qko.151.1649562661192; Sat, 09
Apr 2022 20:51:01 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 9 Apr 2022 20:51:01 -0700 (PDT)
In-Reply-To: <86ilri9q6q.fsf@linuxsc.com>
Injection-Info: google-groups.googlegroups.com; posting-host=108.48.119.9; posting-account=Ix1u_AoAAAAILVQeRkP2ENwli-Uv6vO8
NNTP-Posting-Host: 108.48.119.9
References: <t2pag0$eab$1@dont-email.me> <t2pn31$jkc$1@dont-email.me> <86ilri9q6q.fsf@linuxsc.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <61a0874c-6851-400d-bd03-9ba116801c75n@googlegroups.com>
Subject: Re: Adjusting printf() rounding
From: jameskuy...@alumni.caltech.edu (james...@alumni.caltech.edu)
Injection-Date: Sun, 10 Apr 2022 03:51:01 +0000
Content-Type: text/plain; charset="UTF-8"
 by: james...@alumni.calt - Sun, 10 Apr 2022 03:51 UTC

On Saturday, April 9, 2022 at 12:28:43 PM UTC-4, Tim Rentsch wrote:
> James Kuyper <james...@alumni.caltech.edu> 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 <cstdio>
> >> 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,

That is quite correct.
"The accuracy of the floating-point operations ( + , - , * , / ) and of the
library functions in <math.h> and <complex.h> that return floating-
point results is implementation-defined, as is the accuracy of the
conversion between floating-point internal representations and
string representations performed by the library functions in
<stdio.h> , <stdlib.h> , and <wchar.h> . The implementation may
state that the accuracy is unknown." (5.2.2.4.2p8).

As a result of what the standard fails to mandate about the
accuracy of floating point operations, a fully conforming
implementation need not implement floating point in any useful
fashion. In particular, a conforming implementation of
printf("%g", LDBL_MAX) could perform the conversion between
internal and string representations so inaccurately that the result
would be -LDBL_MAX, so long as it's documentation says that it's
that inaccurate, or even merely says "the accuracy is unknown".

> ... 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.

That's why I used "supposed" rather than "required".

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor