Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Types and Literals

C long double extended precision

Reference for the C long double type: implementation-dependent size and precision, the L literal suffix, %Lf and %Lg formats, math functions with the l suffix.

long double is the widest floating-point type in C. The standard only promises that it is at least as precise as double; what you actually get depends on the compiler and the CPU. On x86 Linux with GCC it is the 80-bit x87 extended format stored in 16 bytes, with 64 bits of mantissa (about 18 to 19 decimal digits). On 64-bit Windows with MSVC it is the same as double. On some ARM and PowerPC systems it is a 128-bit format.

Use it when double is not accurate enough for an intermediate result, such as summing millions of values or checking a numerical algorithm. Do not use it where portability of results matters, because different platforms give different answers.

C
long double x = 3.14159265358979323846L;
printf("%.18Lf\n", x);

Try it

C How much extra precision do we get?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Accumulating error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
long double value = 2.5L;
Item Description
Literal suffix L or l: 1.0L. Without it, a literal is double and loses precision before conversion.
printf %Lf, %Le, %Lg (capital L). %lf does not mean long double.
scanf %Lf, %Lg
Limits LDBL_DIG, LDBL_MANT_DIG, LDBL_MAX, LDBL_MIN, LDBL_EPSILON in <float.h>
Math functions sinl, cosl, sqrtl, powl, fabsl, floorl, expl, logl and the rest of <math.h> with an l suffix

On x86-64 Linux (GCC): 16 bytes of storage, 64-bit mantissa, LDBL_DIG 18, largest value about 1.19 ร— 10^4932.

Errors / undefined behavior: passing a long double to %f, or a double to %Lf, is undefined behavior, because the argument sizes differ. Operations behave like those of double with respect to NaN, infinity and domain errors.

Usage notes

  • The literal needs the L. long double x = 0.1; stores the double approximation of 0.1; 0.1L gives the better one.
  • Calling the wrong math function silently narrows. sqrt(x) with a long double converts to double and back. Use sqrtl, or tgmath.h, which picks the right one.
  • Speed: x87 arithmetic is slower than SSE double arithmetic and cannot be vectorized. Expect a noticeable cost in tight loops.
  • Storage is padded: the 80-bit value sits in a 16-byte slot, and the extra bytes are unspecified. Do not compare two long double objects with memcmp.
  • Results differ between platforms. The same program can print different digits on Linux x86, MSVC and Apple Silicon. Test on each target.
  • Intermediate precision: on x87 targets the compiler may keep double temporaries at higher precision (FLT_EVAL_METHOD), another source of cross-platform differences.
  • C++ has the same type and the same rules for the suffix and the L modifier.