1

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

Medo
  • 952
  • 3
  • 11
  • 22
  • 4
    "[...] which means it can represent a number up 2^128" -- uh no, it's not an integer you know. All bits are not available for storing the integer part. – unwind Apr 21 '17 at 14:54
  • Note that the actual floating-point number is probably only 80 bits (if you are on x86). The compiler reserves 128 bits for alignment. – Sven Marnach Apr 21 '17 at 15:30

2 Answers2

5

The header float.h contains some macros desribing the precision of the different floating-point data types; e.g. LDBL_MANT_DIG gives you the number of binary digits in the mantissa of a long double.

If you don't understand the format of floating-point numbers, I recommend you read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
0

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.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256