From 36b866665e7c0dd8c33d042361870bfe6fbf254c Mon Sep 17 00:00:00 2001 From: David Declerck Date: Mon, 23 Dec 2024 14:24:10 +0100 Subject: [PATCH] Skip more than a single leading 0 in exponent display --- libcob/ChangeLog | 5 +++++ libcob/termio.c | 10 ++++++++-- tests/testsuite.src/run_misc.at | 25 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/libcob/ChangeLog b/libcob/ChangeLog index 3f5237171..bd4e74e4f 100644 --- a/libcob/ChangeLog +++ b/libcob/ChangeLog @@ -1,4 +1,9 @@ +2024-12-23 David Declerck + + * termio.c (clean_double): skip more than a single leading + zero in exponent display + 2024-12-18 David Declerck * string.c: fix a bug where the source of STRING/UNSTRING/INSPECT diff --git a/libcob/termio.c b/libcob/termio.c index c44d31ce4..83ecba978 100644 --- a/libcob/termio.c +++ b/libcob/termio.c @@ -225,9 +225,15 @@ clean_double (char *wrk) char *pos = strrchr (wrk, 'E'); if (pos) { + char *src; pos += 2; /* skip E+ */ - if (pos[0] == '0') { - memmove (pos, pos + 1, strlen (pos)); + /* Skip leading zeroes */ + /* Note: each COBOL environment has a different output format for floats; + here we only check for internal consistency (support for other + formats might be considered for addition if widely requested) */ + for (src = pos; *src == '0'; ++src); + if (src != pos) { + memmove (pos, src, strlen (src) + 1); } return; } diff --git a/tests/testsuite.src/run_misc.at b/tests/testsuite.src/run_misc.at index 97157f4fa..69dfde687 100644 --- a/tests/testsuite.src/run_misc.at +++ b/tests/testsuite.src/run_misc.at @@ -15226,3 +15226,28 @@ AT_CHECK([$COMPILE prog.cob]) AT_CHECK([$COBCRUN_DIRECT ./prog], [0], [], []) AT_CLEANUP + + +AT_SETUP([DISPLAY FLOAT]) +AT_KEYWORDS([FLOAT-SHORT FLOAT-LONG COMP-1 COMP-2]) + +# Note: each COBOL environment has a different output format for floats; +# here we only check for internal consistency (support for other +# formats might be considered for addition if widely requested) + +AT_DATA([prog.cob], [ + IDENTIFICATION DIVISION. + PROGRAM-ID. prog. + DATA DIVISION. + WORKING-STORAGE SECTION. + 01 X FLOAT-SHORT VALUE 0.000012345. + 01 Y FLOAT-LONG VALUE 0.000012345. + PROCEDURE DIVISION. + DISPLAY X SPACE Y WITH NO ADVANCING. + STOP RUN. +]) + +AT_CHECK([$COMPILE prog.cob]) +AT_CHECK([$COBCRUN_DIRECT ./prog], [0], [1.2345E-5 1.2345E-5], []) + +AT_CLEANUP