0

I tried to output a long double to the console directly using qDebug() and indirectly via QString::number() but both do not accept long double.

Is it true that there is no easy way of printing a long double floating point number to the console using Qt? Why?!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
  • Since a `long double` does not need to take any more space then a `double` they may have felt it was pointless. – NathanOliver Jul 11 '16 at 20:09
  • @NathanOliver: That depends on your platform. 16-bit Windows uses 80 bits for `long double` and 64 bits for `double`. I wouldn't be surprised if there are other examples. – Adrian McCarthy Jul 11 '16 at 21:22
  • 1
    You can use this http://stackoverflow.com/questions/2326850/long-double-to-string – demonplus Jul 12 '16 at 05:34

2 Answers2

2

You can provide your own overload of operator<<():

QDebug& operator<<(QDebug& d, long double f)
{
    return d << static_cast<double>(f);
}

This won't show you any extra precision, of course, but may be what you need.

Be aware, however, that a future version of Qt might implement such a function, putting you in violation of the One-Definition Rule. To avoid this, you should guard it with an appropriate #if test for the exact Qt version (or range of versions) that you have verified do not provide a conflicting definition. Also, please consider contributing your implementation to Qt.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
0

There's no overarching reason. At least as of Qt 5.6, nobody bothered to implement it. That's all.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313