std::cout printing nan or zeros for float or double variables in armv7l

  arm, c++, cross-compiling, raspberry-pi

I’m writing a C++ program for a Raspberry PI 3B+ doing cross compilation using Docker container conanio/gcc7-armv7 for cross compilation. Everything was working fine until I started printing double values, and getting "nan" or wrong values in the output. I simplified my code to this minimal example that shows the problem:

#include <cstdio>
#include <iomanip>
#include <iostream>

int main() {
    double foo = 1.234567;
    float bar = 89.10121314;
    std::cout << std::fixed << std::setw(4) << std::setprecision(3) << "foo " << foo << 'n';
    std::printf("foo %4.2fn", foo);
    std::cout << std::fixed << std::setw(4) << std::setprecision(3) << "bar " << bar << 'n';
    std::printf("bar %4.2fn", bar);

I’m building on a x86_64 laptop running Ubuntu 20.04. When I build for x86_64 with gcc 9.3.0 and run it on my laptop, then I get the expected output:

foo 1.235
foo 1.23
bar 89.101
bar 89.10

But when I cross compile and run it in my Raspberry PI then std::cout prints -nan:

foo -nan
foo 1.23
bar -nan
bar 89.10

std::cout doesn’t work but printf works fine. Any idea was it’s going on here?


Source: Windows Questions C++