Reputation: 1032
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
Upvotes: 1
Views: 1205
Reputation: 154235
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
, canlong double
identify the exact precision ofx
?
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
.
Upvotes: 0
Reputation: 602155
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.
Upvotes: 5