Thu May 9 22:38:29 2024 UTC (22d)
tests/lib/libc/stdio/t_printf: Fix another rounding error.

Noted by kre.

This doesn't break a passing test or fix a failed test, at least on
x86 -- our printf produces `0x1.533p+3' for the double case and
`0xa.99ap+0' for the long double case.  But of the hexadecimal number
literals that that start with 0x5 having three hexadigits to the
right of the fractional point, 0x5.4cdp+1 closest to the IEEE 754
binary64, VAX D, x86 extended precision, and IEEE 754 binary128
floating-point numbers closest to 10.6.

The reason is that the number 10.6 (or the nearest floating-point
number in any format with enough precision) is:

101.0100 1100 1100|1100... * 2^1 = 0x5.4cc|c...p+1

If we round at the vertical bar to the _nearest_ output with three
hexadigits of precision, the result is:

101.0100 1100 1101 * 2^1 = 0x5.4cdp+1


(riastradh)
diff -r1.15 -r1.16 src/tests/lib/libc/stdio/t_printf.c

cvs diff -r1.15 -r1.16 src/tests/lib/libc/stdio/t_printf.c (expand / switch to unified diff)

--- src/tests/lib/libc/stdio/t_printf.c 2024/05/09 12:24:24 1.15
+++ src/tests/lib/libc/stdio/t_printf.c 2024/05/09 22:38:29 1.16
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: t_printf.c,v 1.15 2024/05/09 12:24:24 riastradh Exp $ */ 1/* $NetBSD: t_printf.c,v 1.16 2024/05/09 22:38:29 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -185,52 +185,52 @@ ATF_TC_BODY(sprintf_zeropad, tc) @@ -185,52 +185,52 @@ ATF_TC_BODY(sprintf_zeropad, tc)
185ATF_TC(snprintf_double_a); 185ATF_TC(snprintf_double_a);
186ATF_TC_HEAD(snprintf_double_a, tc) 186ATF_TC_HEAD(snprintf_double_a, tc)
187{ 187{
188 atf_tc_set_md_var(tc, "descr", "Test printf a format"); 188 atf_tc_set_md_var(tc, "descr", "Test printf a format");
189} 189}
190 190
191ATF_TC_BODY(snprintf_double_a, tc) 191ATF_TC_BODY(snprintf_double_a, tc)
192{ 192{
193 char buf[1000]; 193 char buf[1000];
194 194
195 snprintf(buf, sizeof buf, "%.3a", (double)10.6); 195 snprintf(buf, sizeof buf, "%.3a", (double)10.6);
196 ATF_CHECK_MSG((strcmp(buf, "0x1.533p+3") == 0 || 196 ATF_CHECK_MSG((strcmp(buf, "0x1.533p+3") == 0 ||
197 strcmp(buf, "0x2.a66p+2") == 0 || 197 strcmp(buf, "0x2.a66p+2") == 0 ||
198 strcmp(buf, "0x5.4ccp+1") == 0 || 198 strcmp(buf, "0x5.4cdp+1") == 0 ||
199 strcmp(buf, "0xa.99ap+0") == 0), 199 strcmp(buf, "0xa.99ap+0") == 0),
200 "buf=%s", buf); 200 "buf=%s", buf);
201 201
202 snprintf(buf, sizeof buf, "%a", (double)0.125); 202 snprintf(buf, sizeof buf, "%a", (double)0.125);
203 ATF_CHECK_MSG((strcmp(buf, "0x1p-3") == 0 || 203 ATF_CHECK_MSG((strcmp(buf, "0x1p-3") == 0 ||
204 strcmp(buf, "0x2p-4") == 0 || 204 strcmp(buf, "0x2p-4") == 0 ||
205 strcmp(buf, "0x4p-5") == 0 || 205 strcmp(buf, "0x4p-5") == 0 ||
206 strcmp(buf, "0x8p-6") == 0), 206 strcmp(buf, "0x8p-6") == 0),
207 "buf=%s", buf); 207 "buf=%s", buf);
208} 208}
209 209
210ATF_TC(snprintf_long_double_a); 210ATF_TC(snprintf_long_double_a);
211ATF_TC_HEAD(snprintf_long_double_a, tc) 211ATF_TC_HEAD(snprintf_long_double_a, tc)
212{ 212{
213 atf_tc_set_md_var(tc, "descr", "Test printf La format"); 213 atf_tc_set_md_var(tc, "descr", "Test printf La format");
214} 214}
215 215
216ATF_TC_BODY(snprintf_long_double_a, tc) 216ATF_TC_BODY(snprintf_long_double_a, tc)
217{ 217{
218 char buf[1000]; 218 char buf[1000];
219 219
220 snprintf(buf, sizeof buf, "%.3La", 10.6L); 220 snprintf(buf, sizeof buf, "%.3La", 10.6L);
221 ATF_CHECK_MSG((strcmp(buf, "0x1.533p+3") == 0 || 221 ATF_CHECK_MSG((strcmp(buf, "0x1.533p+3") == 0 ||
222 strcmp(buf, "0x2.a66p+2") == 0 || 222 strcmp(buf, "0x2.a66p+2") == 0 ||
223 strcmp(buf, "0x5.4ccp+1") == 0 || 223 strcmp(buf, "0x5.4cdp+1") == 0 ||
224 strcmp(buf, "0xa.99ap+0") == 0), 224 strcmp(buf, "0xa.99ap+0") == 0),
225 "buf=%s", buf); 225 "buf=%s", buf);
226 226
227 snprintf(buf, sizeof buf, "%La", 0.125L); 227 snprintf(buf, sizeof buf, "%La", 0.125L);
228 ATF_CHECK_MSG((strcmp(buf, "0x1p-3") == 0 || 228 ATF_CHECK_MSG((strcmp(buf, "0x1p-3") == 0 ||
229 strcmp(buf, "0x2p-4") == 0 || 229 strcmp(buf, "0x2p-4") == 0 ||
230 strcmp(buf, "0x4p-5") == 0 || 230 strcmp(buf, "0x4p-5") == 0 ||
231 strcmp(buf, "0x8p-6") == 0), 231 strcmp(buf, "0x8p-6") == 0),
232 "buf=%s", buf); 232 "buf=%s", buf);
233 233
234#if LDBL_MAX_EXP >= 16384 && LDBL_MANT_DIG >= 64 234#if LDBL_MAX_EXP >= 16384 && LDBL_MANT_DIG >= 64
235 snprintf(buf, sizeof buf, "%La", -0xc.ecececececececep+3788L); 235 snprintf(buf, sizeof buf, "%La", -0xc.ecececececececep+3788L);
236 ATF_CHECK_MSG((strcmp(buf, "-0x1.9d9d9d9d9d9d9d9cp+3791") == 0 || 236 ATF_CHECK_MSG((strcmp(buf, "-0x1.9d9d9d9d9d9d9d9cp+3791") == 0 ||