* [LTP] [PATCH] nextafter01: Check exact results and convert to new API
@ 2026-10-19 12:35 Jan Stancek via ltp
2026-10-19 13:13 ` Petr Vorel
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Jan Stancek via ltp @ 2026-10-19 12:35 UTC (permalink / raw)
To: ltp
The test can hardly fail. Block 1 accepts nextafter(1.0, 1.1) whenever
(answer + 1.0) / 2 rounds to 1.0 as a float, which is also true when
nextafter() returns 1.0 unchanged. Block 2 never recomputes 'check'
and compares the stale value from block 1, which is exactly 1.0, so its
'check != 1.0' condition is always false and any return value passes.
Only the x == y case in block 3 is really verified. The file header also
names the test "scalb".
Compare the results with the exact next doubles instead: 1.0 + 2^-52
above and 1.0 - 2^-53 below. Both are exactly representable, so a plain
equality test is sufficient. Rewrite the test with the new API while at
it and fix the description.
Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
testcases/misc/math/nextafter/nextafter01.c | 145 +++++---------------
1 file changed, 33 insertions(+), 112 deletions(-)
diff --git a/testcases/misc/math/nextafter/nextafter01.c b/testcases/misc/math/nextafter/nextafter01.c
index f4b212d4daa2..a74b7dc8e974 100644
--- a/testcases/misc/math/nextafter/nextafter01.c
+++ b/testcases/misc/math/nextafter/nextafter01.c
@@ -1,127 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
- *
- * Copyright (c) International Business Machines Corp., 2002
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Copyright (c) International Business Machines Corp., 2002
+ * Copyright (c) 2026 Jan Stancek <jstancek@redhat.com>
*/
-/* 01/02/2003 Port to LTP avenkat@us.ibm.com */
-/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
-
-/*
- * NAME
- * scalb
+/*\
+ * Verify that nextafter() returns the next representable double in the
+ * direction of its second argument:
*
- * CALLS
- * nextafter(3C)
- *
- * ALGORITHM
- * Check results from the above functions against expected values.
- *
- * RESTRICTIONS
- * Checks for basic functionality, nothing fancy
+ * - nextafter(1.0, 1.1) is 1.0 + 2^-52 (DBL_EPSILON)
+ * - nextafter(1.0, 0.9) is 1.0 - 2^-53
+ * - nextafter(1.0, 1.0) is 1.0
*/
-#include <errno.h>
#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "test.h"
+#include "tst_test.h"
-#define FAILED 0
-#define PASSED 1
+/* DBL_EPSILON spelled out, as sparse does not know __DBL_EPSILON__ */
+#define EPSILON (1.0 / (1ULL << 52))
-char *TCID = "nextafter01";
+static struct tcase {
+ double x;
+ double y;
+ double exp;
+} tcases[] = {
+ {1.0, 1.1, 1.0 + EPSILON},
+ {1.0, 0.9, 1.0 - EPSILON / 2},
+ {1.0, 1.0, 1.0},
+};
-int local_flag = PASSED;
-int block_number;
-FILE *temp;
-int TST_TOTAL = 1;
-
-void setup();
-void blenter();
-void blexit();
-
-/*--------------------------------------------------------------*/
-int main()
+static void run(unsigned int n)
{
- double answer;
- double check; /* tmp variable */
+ struct tcase *tc = &tcases[n];
+ double ret = nextafter(tc->x, tc->y);
- setup(); /* temp file is now open */
-/*--------------------------------------------------------------*/
- blenter();
-
- answer = nextafter(1.0, 1.1);
- check = (answer + 1.0) / 2;
- if ((check != answer) && ((float)check != 1.0)) {
- fprintf(temp, "nextafter returned %e, expected answer or 1.0\n",
- answer);
- local_flag = FAILED;
+ if (ret == tc->exp) {
+ tst_res(TPASS, "nextafter(%g, %g) == %.17g", tc->x, tc->y, ret);
+ } else {
+ tst_res(TFAIL, "nextafter(%g, %g) == %.17g, expected %.17g",
+ tc->x, tc->y, ret, tc->exp);
}
-
- blexit();
-/*--------------------------------------------------------------*/
- blenter();
-
- answer = nextafter(1.0, 0.9);
- if ((check != answer) && (check != 1.0)) {
- fprintf(temp, "nextafter returned %e, expected answer or 1.0\n",
- answer);
- local_flag = FAILED;
- }
-
- blexit();
-/*--------------------------------------------------------------*/
- blenter();
-
- answer = nextafter(1.0, 1.0);
- if (answer != 1.0) {
- fprintf(temp, "nextafter 3 returned %e, expected 1.0\n",
- answer);
- local_flag = FAILED;
- }
-
- blexit();
-/*--------------------------------------------------------------*/
-
- tst_exit();
}
-/*--------------------------------------------------------------*/
-
-/***** ***** LTP Port *****/
-
-/* FUNCTIONS */
-
-void setup()
-{
- temp = stderr;
-}
-
-void blenter()
-{
- local_flag = PASSED;
-}
-
-void blexit()
-{
- if (local_flag == PASSED)
- tst_resm(TPASS, "Test passed");
- else
- tst_resm(TFAIL, "Test failed");
-}
-
-/***** ***** *****/
+static struct tst_test test = {
+ .test = run,
+ .tcnt = ARRAY_SIZE(tcases),
+};
--
2.56.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [LTP] [PATCH] nextafter01: Check exact results and convert to new API
2026-10-19 12:35 [LTP] [PATCH] nextafter01: Check exact results and convert to new API Jan Stancek via ltp
@ 2026-10-19 13:13 ` Petr Vorel
2026-10-20 10:03 ` Andrea Cervesato via ltp
2026-10-19 19:05 ` [LTP] " linuxtestproject.agent
2026-10-19 21:08 ` [LTP] [PATCH] " Avinesh Kumar via ltp
2 siblings, 1 reply; 6+ messages in thread
From: Petr Vorel @ 2026-10-19 13:13 UTC (permalink / raw)
To: Jan Stancek; +Cc: ltp
Hi Jan,
> The test can hardly fail. Block 1 accepts nextafter(1.0, 1.1) whenever
> (answer + 1.0) / 2 rounds to 1.0 as a float, which is also true when
> nextafter() returns 1.0 unchanged. Block 2 never recomputes 'check'
> and compares the stale value from block 1, which is exactly 1.0, so its
> 'check != 1.0' condition is always false and any return value passes.
> Only the x == y case in block 3 is really verified. The file header also
> names the test "scalb".
> Compare the results with the exact next doubles instead: 1.0 + 2^-52
> above and 1.0 - 2^-53 below. Both are exactly representable, so a plain
> equality test is sufficient. Rewrite the test with the new API while at
> it and fix the description.
Thanks!
Reviewed-by: Petr Vorel <pvorel@suse.cz>
...
> -char *TCID = "nextafter01";
> +static struct tcase {
> + double x;
> + double y;
> + double exp;
> +} tcases[] = {
> + {1.0, 1.1, 1.0 + EPSILON},
> + {1.0, 0.9, 1.0 - EPSILON / 2},
> + {1.0, 1.0, 1.0},
> +};
...
> + if (ret == tc->exp) {
Hm, we still does not have TST_EXP_EQ_* variant for double.
Likely not worth of having it (not many tests comparing double).
Kind regards,
Petr
> + tst_res(TPASS, "nextafter(%g, %g) == %.17g", tc->x, tc->y, ret);
> + } else {
> + tst_res(TFAIL, "nextafter(%g, %g) == %.17g, expected %.17g",
> + tc->x, tc->y, ret, tc->exp);
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [LTP] [PATCH] nextafter01: Check exact results and convert to new API
2026-10-19 13:13 ` Petr Vorel
@ 2026-10-20 10:03 ` Andrea Cervesato via ltp
2026-10-20 10:06 ` Petr Vorel
0 siblings, 1 reply; 6+ messages in thread
From: Andrea Cervesato via ltp @ 2026-10-20 10:03 UTC (permalink / raw)
To: Petr Vorel; +Cc: ltp
Hi Petr,
> ...
> > + if (ret == tc->exp) {
> Hm, we still does not have TST_EXP_EQ_* variant for double.
> Likely not worth of having it (not many tests comparing double).
>
> Kind regards,
> Petr
actually I think it's a good idea, even if we don't have many examples
like this one. Maybe we can skip it for this time, keeping an eye on
patches to check for similar patterns.
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [LTP] [PATCH] nextafter01: Check exact results and convert to new API
2026-10-20 10:03 ` Andrea Cervesato via ltp
@ 2026-10-20 10:06 ` Petr Vorel
0 siblings, 0 replies; 6+ messages in thread
From: Petr Vorel @ 2026-10-20 10:06 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
> Hi Petr,
> > ...
> > > + if (ret == tc->exp) {
> > Hm, we still does not have TST_EXP_EQ_* variant for double.
> > Likely not worth of having it (not many tests comparing double).
> > Kind regards,
> > Petr
> actually I think it's a good idea, even if we don't have many examples
> like this one. Maybe we can skip it for this time, keeping an eye on
> patches to check for similar patterns.
+1, once we have it, people can start using it.
I suggest: Jan, feel free to merge as is and either send a separate patch to add
it or I send a patch to add it later (let's don't block a test rewrite unless
you have time to add TST_EXP_EQ_*).
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [LTP] nextafter01: Check exact results and convert to new API
2026-10-19 12:35 [LTP] [PATCH] nextafter01: Check exact results and convert to new API Jan Stancek via ltp
2026-10-19 13:13 ` Petr Vorel
@ 2026-10-19 19:05 ` linuxtestproject.agent
2026-10-19 21:08 ` [LTP] [PATCH] " Avinesh Kumar via ltp
2 siblings, 0 replies; 6+ messages in thread
From: linuxtestproject.agent @ 2026-10-19 19:05 UTC (permalink / raw)
To: Jan Stancek; +Cc: ltp
Hi Jan,
On Mon, 19 Oct 2026 at 14:35:31 +0200, Jan Stancek via ltp wrote:
> nextafter01: Check exact results and convert to new API
Verdict - Reviewed
---
Note:
The agent can sometimes produce false positives although often its
findings are genuine. If you find issues with the review, please
comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [LTP] [PATCH] nextafter01: Check exact results and convert to new API
2026-10-19 12:35 [LTP] [PATCH] nextafter01: Check exact results and convert to new API Jan Stancek via ltp
2026-10-19 13:13 ` Petr Vorel
2026-10-19 19:05 ` [LTP] " linuxtestproject.agent
@ 2026-10-19 21:08 ` Avinesh Kumar via ltp
2 siblings, 0 replies; 6+ messages in thread
From: Avinesh Kumar via ltp @ 2026-10-19 21:08 UTC (permalink / raw)
To: Jan Stancek, ltp
Hi,
> +/*\
> + * Verify that nextafter() returns the next representable double in the
nit:
:manpage:`nextafter(3)`
Reviewed-by: Avinesh Kumar <avinesh.kumar@suse.com>
Regards,
Avinesh
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-10-20 10:07 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-19 12:35 [LTP] [PATCH] nextafter01: Check exact results and convert to new API Jan Stancek via ltp
2026-10-19 13:13 ` Petr Vorel
2026-10-20 10:03 ` Andrea Cervesato via ltp
2026-10-20 10:06 ` Petr Vorel
2026-10-19 19:05 ` [LTP] " linuxtestproject.agent
2026-10-19 21:08 ` [LTP] [PATCH] " Avinesh Kumar via ltp
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.