ctypes.c_longdouble is the ctypes type for the C long double. It lets you declare function arguments, return values and structure fields of that type, but a Python float is only a 64-bit double, so every value you read through .value is rounded to double precision.
The size and precision of long double depend on the platform and compiler, which makes c_longdouble the least portable of the ctypes numeric types.
C leaves the format of long double to the implementation. The original answer claimed it always offers "at least 80 bits"; that is true only on some platforms.
| Platform and compiler | Typical long double format | Size |
|---|---|---|
| x86-64 Linux (GCC, Clang) | x87 80-bit extended precision | 16 bytes (padded) |
| Windows (MSVC) | Same as double | 8 bytes |
| macOS on Apple silicon | Same as double | 8 bytes |
| ARM64 Linux (GCC) | IEEE 754 binary128 | 16 bytes |
Check what your build uses:
On x86-64 Linux this prints 16 and False. When long double has the same size as double, ctypes makes c_longdouble an alias of c_double, so the second line prints True on platforms where they match.
The .value attribute converts to and from a Python float:
The stored C value can hold more bits than the Python object you read back, but you only ever see the rounded double. If you need the extra digits you have to read the raw bytes and decode them with a library such as NumPy (numpy.longdouble) or do the arithmetic in C.
Here is a corrected version of the original circle-area example. The C code uses the L suffix so the constant is a long double literal:
Declare both the argument and the return type. The default return type is c_int, which would give garbage for a floating-point result:
Because restype is c_longdouble, the result arrives as a Python float already, so there is no .value call, and the printed number has double precision even though the C computation used extended precision.
When you need the extra bits on x86-64 Linux, copy the bytes into a NumPy long double array. Passing a pointer to a C buffer avoids the conversion to float:
NumPy's longdouble follows the same C type as the compiler that built NumPy, so it matches the library on the same platform. If the array's element size is not what you expect, check np.dtype(np.longdouble).itemsize.
Use it only to match an existing C signature, for example a library that exposes long double functions such as sqrtl from the math library. For new interfaces prefer double, which is portable and exact in Python, or a decimal or arbitrary-precision type when you need exact money arithmetic. For money, use Python's decimal module instead of any binary floating-point type.
No. The C side may compute in 80 bits, but .value and returned results are converted to a 64-bit Python float. Use NumPy longdouble arrays and pointers to keep the extra bits.
Only on platforms where long double is the same size as double, such as Windows with MSVC. On x86-64 Linux they are different types.
You probably left restype unset. It defaults to c_int, so a floating-point return value is misread. Set restype = ctypes.c_longdouble.
No. Binary floating point cannot represent most decimal fractions exactly. Use decimal.Decimal or integer cents.
ctypes.c_longdouble maps the platform-dependent C long double. Its size and precision vary by compiler and operating system, it may be an alias of c_double, and reading values through .value rounds them to a Python float. Always set argtypes and restype, and use NumPy longdouble buffers when you need the full extended precision.