* gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded @ 2021-06-12 20:45 Pavel M 2021-06-13 9:42 ` Achim Gratz ` (3 more replies) 0 siblings, 4 replies; 6+ messages in thread From: Pavel M @ 2021-06-12 20:45 UTC (permalink / raw) To: cygwin Sample code (t903.c): #include <stdio.h> int main(void) { printf("%.43f\n", 0x1.52f8a8e32e982p-140); return 0; } Invocations: # gcc on Windows 10 (Cygwin) $ gcc t903.c -Wall -Wextra -std=c11 -pedantic -Wfatal-errors && ./a.exe 0.0000000000000000000000000000000000000000010 $ gcc --version gcc (GCC) 11.1.0 # gcc on Linux $ gcc t903.c -Wall -Wextra -std=c11 -pedantic -Wfatal-errors && ./a.exe 0.0000000000000000000000000000000000000000009 # clang on Windows $ clang t903.c -Wall -Wextra -std=c11 -ffp-model=strict -pedantic -Wfatal-errors && ./a.exe 0.0000000000000000000000000000000000000000009 # cl on Windows $ cl t903.c /std:c11 /Za /fp:strict && ./t903.exe 0.0000000000000000000000000000000000000000009 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded 2021-06-12 20:45 gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded Pavel M @ 2021-06-13 9:42 ` Achim Gratz 2021-06-13 19:19 ` Brian Inglis ` (2 subsequent siblings) 3 siblings, 0 replies; 6+ messages in thread From: Achim Gratz @ 2021-06-13 9:42 UTC (permalink / raw) To: cygwin Pavel M via Cygwin writes: > Sample code (t903.c): > #include <stdio.h> > int main(void) > { > printf("%.43f\n", 0x1.52f8a8e32e982p-140); > return 0; > } > > Invocations: > # gcc on Windows 10 (Cygwin) > $ gcc t903.c -Wall -Wextra -std=c11 -pedantic -Wfatal-errors && ./a.exe > 0.0000000000000000000000000000000000000000010 You will want to report this against newlib, I'd think. If I remember correctly, newlib uses a single guard digit for the conversion and for such an algorithm the result you see is expected and correct. Whether any later C standard would mandate to use an algorithm that uses higher precision (in terms of guard digits you'd end up with 15 for your example before you get the expected result I think) I have no idea. Regards, Achim. -- +<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+ SD adaptation for Waldorf rackAttack V1.04R1: http://Synth.Stromeko.net/Downloads.html#WaldorfSDada ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded 2021-06-12 20:45 gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded Pavel M 2021-06-13 9:42 ` Achim Gratz @ 2021-06-13 19:19 ` Brian Inglis 2021-11-03 21:07 ` Pavel M [not found] ` <CAL9Mx1utCxXFPy3vOK0ZOkX2qzt8ntQ0LEyD4WvLpCRRbTLYeQ@mail.gmail.com> 3 siblings, 0 replies; 6+ messages in thread From: Brian Inglis @ 2021-06-13 19:19 UTC (permalink / raw) To: cygwin [-- Attachment #1: Type: text/plain, Size: 1851 bytes --] On 2021-06-12 14:45, Pavel M via Cygwin wrote: > Sample code (t903.c): > #include <stdio.h> > int main(void) > { > printf("%.43f\n", 0x1.52f8a8e32e982p-140); > return 0; > } > > Invocations: > # gcc on Windows 10 (Cygwin) > $ gcc t903.c -Wall -Wextra -std=c11 -pedantic -Wfatal-errors && ./a.exe > 0.0000000000000000000000000000000000000000010 > > $ gcc --version > gcc (GCC) 11.1.0 > > # gcc on Linux > $ gcc t903.c -Wall -Wextra -std=c11 -pedantic -Wfatal-errors && ./a.exe > 0.0000000000000000000000000000000000000000009 > > # clang on Windows > $ clang t903.c -Wall -Wextra -std=c11 -ffp-model=strict -pedantic > -Wfatal-errors && ./a.exe > 0.0000000000000000000000000000000000000000009 > > # cl on Windows > $ cl t903.c /std:c11 /Za /fp:strict && ./t903.exe > 0.0000000000000000000000000000000000000000009 See newlib mailing list incorrectly rounded square root thread starting: https://sourceware.org/pipermail/newlib/2021/018369.html As suggested lately, I tried adding -frounding-math $ info gcc frounding-math to see if it makes any difference to your test on gcc 10 and got the same result. It may need to be applied to the library functions to have any useful impact. I also tried Cygwin clang and got the same result. It may be an accuracy issue or a bug in the underlying newlib printf conversion. I tried fcvtbuf in the attached test program and found it does the rounding as expected on Cygwin: $ gcc -o test-printf-round{,.c} && ./test-printf-round sgn 0 decpt -42 +0.0000000000000000000000000000000000000000009 0.0000000000000000000000000000000000000000010 -- Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada This email may be disturbing to some readers as it contains too much technical detail. Reader discretion is advised. [Data in binary units and prefixes, physical quantities in SI.] [-- Attachment #2: test-printf-round.c --] [-- Type: text/plain, Size: 971 bytes --] /* test-printf-round.c */ #define _POSIX_SOURCE #define _XOPEN_SOURCE 600 #include <stdio.h> #include <stdlib.h> #include <string.h> #if 0 #define V 0X1.52f8a8e32e982P+140 #define V 0X0.0000000000000P+0 #endif #define V 0X1.52f8a8e32e982P-140 #define DECIMALS 43 int main(void) { int decpt = 0; int sgn = -1; char *b; static char zeros[128] = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; static char buf[128] = { 0 }; b = fcvtbuf(V, DECIMALS, &decpt, &sgn, buf); printf("sgn %d decpt %d %s%.*s%s%s %.*f\n", sgn, decpt, !sgn ? "+" : 1 == sgn ? "-" : "", /* 0 < decpt -> decpt digits | . | buf + decpt digits */ /* decpt < 0 -> 0. | decpt zeros | buf digits */ decpt < 0 ? 2 : decpt, decpt < 0 ? "0." : b, decpt < 0 ? zeros + strlen(zeros) + decpt : ".", decpt < 0 ? b : b + decpt, DECIMALS, V); } ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded 2021-06-12 20:45 gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded Pavel M 2021-06-13 9:42 ` Achim Gratz 2021-06-13 19:19 ` Brian Inglis @ 2021-11-03 21:07 ` Pavel M 2021-11-03 21:09 ` Pavel M [not found] ` <CAL9Mx1utCxXFPy3vOK0ZOkX2qzt8ntQ0LEyD4WvLpCRRbTLYeQ@mail.gmail.com> 3 siblings, 1 reply; 6+ messages in thread From: Pavel M @ 2021-11-03 21:07 UTC (permalink / raw) To: cygwin Hello. Is there any update on this? Thanks. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded 2021-11-03 21:07 ` Pavel M @ 2021-11-03 21:09 ` Pavel M 0 siblings, 0 replies; 6+ messages in thread From: Pavel M @ 2021-11-03 21:09 UTC (permalink / raw) To: cygwin Ah, sorry! Now I see: https://www.mail-archive.com/cygwin@cygwin.com/msg168488.html. ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <CAL9Mx1utCxXFPy3vOK0ZOkX2qzt8ntQ0LEyD4WvLpCRRbTLYeQ@mail.gmail.com>]
* Re: gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded [not found] ` <CAL9Mx1utCxXFPy3vOK0ZOkX2qzt8ntQ0LEyD4WvLpCRRbTLYeQ@mail.gmail.com> @ 2021-11-03 21:32 ` Pavel M 0 siblings, 0 replies; 6+ messages in thread From: Pavel M @ 2021-11-03 21:32 UTC (permalink / raw) To: cygwin To Brian Inglis: thanks! Forwarded to newlib mailing list. ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-11-03 21:33 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2021-06-12 20:45 gcc 11.1.0: printf("%.43f\n", 0x1.52f8a8e32e982p-140): printed value is incorrectly rounded Pavel M 2021-06-13 9:42 ` Achim Gratz 2021-06-13 19:19 ` Brian Inglis 2021-11-03 21:07 ` Pavel M 2021-11-03 21:09 ` Pavel M [not found] ` <CAL9Mx1utCxXFPy3vOK0ZOkX2qzt8ntQ0LEyD4WvLpCRRbTLYeQ@mail.gmail.com> 2021-11-03 21:32 ` Pavel M
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).