1

Why aren't long double numbers not displayed properly? I'm using MinGw32 4.9.2. For example when a = 56 and b = 3, a is displayed as -2.6815615859885194e+154, b as -3 and the resulting operation is -5.075883675e-116

#include <iostream>
#include <iomanip>
using namespace std;

long double a, b, res;
int main()
{
    cout << setprecision(10);
    cout << "enter a: ";
    cin >> a;
    cout << a << endl;
    cout << "enter b: ";
    cin >> b;
    cout << b << endl;
    res = a * b;
    cout << sum;
    return 0;
}
Grzegorz
  • 11
  • 2
  • 1
    Code::Blocks is an IDE, not a compiler. What compiler are you using? Also, what values are you providing for `a`and `b` are you providing that show an invalid result? Or are you talking about the values shown in the debugger? – Matteo Italia Apr 28 '17 at 06:10
  • 1
    (BTW your `sum` is actually the *product* of `a` and `b`) – Matteo Italia Apr 28 '17 at 06:11
  • What happens if you use a more reasonable value for the precision (see http://stackoverflow.com/questions/476212/what-is-the-precision-of-long-double-in-c)? 100 is unrealistically high. – Paul Floyd Apr 28 '17 at 06:14
  • @MatteoItalia - probably safe to assume (geez, did I _really_ just write that) that it's the MinGW (x86) toolset. – enhzflep Apr 28 '17 at 06:15
  • 1
    Works perfectly in Code::Blocks using MinGw32 4.9.2 – Aeonos Apr 28 '17 at 06:16
  • @Aeonos - beat me to it. a = 7, b = 8 printed result is 56 (win7 x64, 32bit mingw) – enhzflep Apr 28 '17 at 06:17
  • Unable to reproduce: https://ideone.com/EW1Gsi – user4581301 Apr 28 '17 at 06:24
  • 2
    @enhzflep: my main fear is about what exact MinGW version we are talking about and the precise configuration. There were [known incompatibilities](http://stackoverflow.com/questions/30080618/long-double-is-printed-incorrectly-with-iostreams-on-mingw) between the gcc compiler default settings for `long double` and the MinGW runtime (which is bolted over the MSVCRT); before going down that rabbit hole I wanted to be sure that's what we are after. – Matteo Italia Apr 28 '17 at 06:29
  • Ahhhhh. Thanks @MatteoItalia - I'd not been aware of that quagmire before today. Many thanks. :) – enhzflep Apr 28 '17 at 06:40

0 Answers0