printf floating-point output variations only with alpine docker on Windows

49 views Asked by At

I ran across some variations in floating point output from printf, but only when running in a node:lts-alpine3.19 (imageid ae7f6c370720) container in Docker (engine 25.0.3, desktop 4.28.0) on Windows 10 (22H2 build 19045.4170)

Steps to reproduce:

  • Install WSL 2
  • Install Docker Desktop
  • Run this:
docker run -it node:lts-alpine3.19 /bin/sh
cat > aa.c <<EOF
#include <stdio.h>
int main()
{
        double val = 1.2;
        printf("%lf\n", val);
}
EOF
gcc aa.c -o aa
./aa   # Emits 1.200000 as expected
valgrind ./aa 2> /dev/null   # Emits 1.199999

Further testing shows that it works fine in a debian:bookworm container, both with the valgrind package (3.19.0) and valgrind installed from source (3.22.0, to match the alpine container) It also works fine in a node:lts-alpine3.19 container running on an Amazon Linux VM instead of Windows.

I've narrowed this down to variations in printf output, the underlying bit pattern of the double value is the same, as shown by this code:

#include <stdio.h>
int main() {
        char ddchar[] = { 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0xf3, 0x3f }; // aka 1.2
        double *ddv = (double *)ddchar;
        printf("ddv %lf\n", *ddv);
}

Can anyone explain why this variation occurs, and why it happens only in this particular environment?

0

There are 0 answers