0

in C++ I'm defining the following types:

double doubleType = 1.6e-300;
long double longDoubleType = 1.6e-300;

I'll then print the values using:

cout << "double is of size " << sizeof(doubleType) << " and value is " << doubleType << endl;
cout << "long double is of size " << sizeof(longDoubleType) << " and value is " << longDoubleType << endl;

my output reads:

double is of size 8 and value is 1.6e-300
long double is of size 12 and value is -1.43863e-264

Whats causing the difference in the interpretation of the values?

François Andrieux
  • 28,148
  • 6
  • 56
  • 87

1 Answers1

0

It doesn't seem likely that you got this behavior with std::cout. More likely, you were using printf with the wrong format specifier, something like:

printf("%g", longDoubleType);

This is undefined behavior. It's permitted to do just about anything, but one of the common ways it plays out is that some of the bits of longDoubleType are interpreted as a double instead of a long double.

With printf, I can reproduce your output. With std::cout, I can't.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • I'm definitely using cout. I'm also using the minGW (minimalist GNU for Windows) compiler. My build command is "g++ main.cpp", nothing fancy. I also tried using the "-std=c++11" option and I get the same results. Can you check your build options, maybe I'm missing something you're including. – user2600449 Jan 16 '17 at 23:03
  • @user2600449: The minGW information helps. It looks like this is a [known problem](http://stackoverflow.com/questions/27537775/cout-long-double-issue) with minGW, stemming from a mismatch between the `long double` implementations used by minGW and the underlying Microsoft libraries. Other search results suggest it might be fixed in more recent versions (but it's probably not). – user2357112 Jan 16 '17 at 23:21
  • I saw some similar reports on some websites. I just installed the latest minGW a few weeks ago, so its probably not fixed yet. This seems like the most likely explanation. :/ – user2600449 Jan 16 '17 at 23:26