Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The long double type in C++ is a fundamental, built-in floating-point data type designed to provide a precision at least as strict as that of a standard double. It represents the most precise floating-point format natively supported by the compiler and target architecture. According to the C++ Standard, the size and precision guarantees follow a strict ordering: sizeof(float) <= sizeof(double) <= sizeof(long double).

Implementation-Defined Sizing

The exact bit-width, precision, and memory layout of long double are implementation-defined and highly dependent on the compiler, operating system, and hardware architecture:
  • x86/x86-64 (Linux/macOS): Typically implemented as the x87 80-bit extended precision format. Due to memory alignment requirements, sizeof(long double) usually evaluates to 12 or 16 bytes, even though only 10 bytes (80 bits) are utilized for the significand and exponent.
  • x86/x86-64 (Windows/MSVC): Implemented as a 64-bit IEEE 754 double-precision float. On this toolchain, long double is identical to double in both size (sizeof(long double) == 8) and precision.
  • ARM64 / Specific UNIX platforms: Often implemented as a 128-bit IEEE 754 quadruple-precision floating-point format (sizeof(long double) == 16).

Syntax and Literals

To explicitly declare a long double literal, append the L or l suffix to a floating-point number. Without the suffix, the compiler treats the literal as a standard double, which may result in a silent loss of precision during initialization before the assignment occurs.
// Explicit long double literals using the 'L' suffix
long double pi_val = 3.141592653589793238462643383279502884L;
long double planck_const = 6.62607015e-34L; 

// Warning: Literal is parsed as a double; precision is truncated before assignment
long double truncated_val = 3.141592653589793238462643383279502884; 

Standard Library Interfaces

Type Traits and Limits

The <limits> header provides compile-time introspection for the specific characteristics of long double on the target platform.
#include <iostream>
#include <limits>

int main() {
    // Number of base-10 digits that can be represented without change
    std::cout << std::numeric_limits<long double>::digits10 << '\n';
    
    // Maximum finite value representable by the type
    std::cout << std::numeric_limits<long double>::max() << '\n';
    
    // Difference between 1.0 and the next representable value
    std::cout << std::numeric_limits<long double>::epsilon() << '\n';
    
    return 0;
}

Mathematical Functions

The <cmath> header provides overloaded mathematical functions for long double. In modern C++, the standard functions (e.g., std::sin, std::exp) are overloaded to accept long double natively, but the C-style l-suffixed functions remain available.
#include <cmath>

long double val = 2.0L;

// C++ overloaded standard math function
long double result1 = std::sqrt(val); 

// C-style explicit long double math function
long double result2 = std::sqrtl(val); 

Formatted I/O

When using C++ streams (<iostream>), long double is handled automatically via operator overloading. However, when interfacing with C-style I/O (<cstdio>), the L length modifier must be used in conjunction with a floating-point conversion specifier (f, e, g).
#include <cstdio>

long double large_frac = 0.1234567890123456789L;

// %Lf is strictly required for long double in printf/scanf
std::printf("%.19Lf\n", large_frac); 
Master C++ with Deep Grasping Methodology!Learn More