https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91214

            Bug ID: 91214
           Summary: first atof function call not return correct result
           Product: gcc
           Version: 9.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xiaoyi_wu at yahoo dot com
  Target Milestone: ---

The first call to atof in the program gives incorrect result.  Here is the
terminal session:

lima:~$ uname -a
Linux lima 5.1.17-300.fc30.x86_64 #1 SMP Wed Jul 10 15:20:27 UTC 2019 x86_64
x86_64 x86_64 GNU/Linux
lima:~$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array --with-isl --enable-offload-targets=nvptx-none
--without-cuda-driver --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.1.1 20190503 (Red Hat 9.1.1-1) (GCC)
lima:~$ cat main.cc
#include <cstdio>
#include <cstdlib>
int main(int argc, const char **argv) {
  if (argc != 2) return printf("usage$ %s input.txt\n", *argv);
  FILE *file = fopen(argv[1], "r");
  char line[256];
  while (fgets(line, 256, file)) {
    printf("{%s} %g\n", line, atof(line));
  }
  fclose(file);
}
lima:~$ cat input.txt
123.45
234.56
345.67
lima:~$ g++ -Wall -O3 main.cc
lima:~$ a.out input.txt
{123.45
} 0
{234.56
} 234.56
{345.67
} 345.67
lima:~$

This small program read lines from input.txt in a loop, and print the line and
the atof value of the line out.  But the first atof returns a 0 instead of the
expected 123.45.

Reply via email to