Search code examples
clong-double

How to identify the precision of long double datatype


My compiler is telling me that the size of sizeof(long double) is 16 byte, which means it can represent a number up 2^128. Now, I want to know until how many digits the precision can handle. For instance, if x= 0.1234567812345678, can long double identify the exact precision of x here?

Thank you


Solution

  • I want to know until how many digits the precision can handle.

    LDBL_DIG, in <float.h>, is the number of minimum number of significant decimal digits that will be converted from text to long double distinctively. It is at least 10 and may be 18 or so on your platform. 0.1234567812345678 and 123.4567812345678 have 16 significant decimal digits.


    My compiler is telling me that the size of sizeof(long double) is 16 byte, which means it can represent a number up 2^128

    It could mean something like that if long double was an integer, yet floating point numbers are distributed logarithmically. Further, some systems do not use all 16 bytes for data. Some are padding. @unwind @Sven Marnach. Use the values in <float.h> to characterize the long double your platform is using.


    if x= 0.1234567812345678, can long double identify the exact precision of x?

    No, unless you are on a rare platform that uses decimal floating point.

    Most platforms use a binary floating point. So unless the number can be represented as an exact sum of powers of 2, like 42.125, the value saved as a long double will be a nearby approximation. Code will convert text 0.1234567812345678 to the nearest long double which may have an exact value of 0.1234567812345677972896140772718354128301143646240234375.