86

In C++ (and C), a floating point literal without suffix defaults to double, while the suffix f implies a float. But what is the suffix to get a long double?

Without knowing, I would define, say,

const long double x = 3.14159265358979323846264338328;

But my worry is that the variable x contains fewer significant bits of 3.14159265358979323846264338328 than 64, because this is a double literal. Is this worry justified?

Walter
  • 44,150
  • 20
  • 113
  • 196
  • 2
    Just preprocess this file `#include LDBL_MAX` and you see `1.18973149535723176502e+4932L` which answers your question. – Marc Glisse Feb 04 '14 at 16:27
  • As to the question "is this worry justified" it really depends. Your PI value has a precision of 1e-28. Machine epsilon for double is ~2.22e-16, so it IS smaller than what you are looking for to capture this number precisely. Does it matter? Depends on whether or not you need to be that precise in your calculations... – IdeaHat Feb 04 '14 at 16:35
  • @EricPostpischil it obviously shows how to spell out a `long double` constant... (suffix L) – Marc Glisse Feb 05 '14 at 12:13

4 Answers4

95

From the C++ Standard

The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double.

It is interesting to compare with corresponding paragraph of the C Standard. In C there is used term floating constant instead of floating literal in C++:

4 An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
28

The C suffix is L. I'd strongly suspect that it is the same for C++.

Your worry is justified. Your literal would first be converted to a double, and thus truncated, and then converted back to long double.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
13

Your concern is valid and you should use a L suffix for long double literal.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
2

I tried the l suffix:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
  float t1 = 1.10000000000000000002l;
  double t2 = 1.10000000000000000002l;
  long double t3 = 1.10000000000000000002L;
  cout << setprecision(25);
  cout << t1 << endl;
  cout << t2 << endl;
  cout << t3 << endl;
}

Still I get this output indicating the lack of desired precision:

1.10000002384185791015625
1.100000000000000088817842
1.100000000000000088817842
DKS
  • 188
  • 10
  • 1
    With many implementations, the representations (and thus value range, precision etc.) of `double` and `long double` are identical (e.g. 64 bit IEEE float). Just like on many implementations, the representations of `long` and `long long` are identical (e.g. 64 bit 2's complement signed integer). In both cases though, they are still treated as different by the type system. E.g. calling a function template with `1.0` and `1.0L` will deduce different types and call different specializations of the template. Same with `1L` and `1LL`. – Paul Groke Dec 19 '21 at 04:15