C precision double: compiler dependent?

on my 32-bit machine (with an Intel T7700 duo core), I have 15 digits of precision for double and long double types for C language. I compared the LDBL_DIG parameters for the long double and DBL_DIG for the two, and they are both 15. I got these answers using MVS2008. I was wondering if these results can be compiler dependent or do they just depend on my processor?

Thanks a lot...

+4
source share
5 answers

Some compilers support a long double format with higher precision than double. Microsoft MSVC is not one of them. If 15 significant digits are not good enough, the chances are very high, so you should not use a floating point type. Check out this thread for arbitrary precision libraries.

+1
source

They may depend on the compiler, but in general they will simply depend on the architecture. If compilers use the same include files, in particular, they probably won't change. It is a good idea to test them to be sure, though, if you want to write portable code.

+1
source

Right They are implementation dependent. The only guarantees of standard C are:

  • float is a subset of double and double is a subset of long double (6.2.5 / 10)
  • FLT_RADIX β‰₯ 2 (5.2.4.2.2 / 9)
  • FLT_DIG β‰₯ 6, DBL_DIG β‰₯ 10, LDBL_DIG β‰₯ 10
  • FLT_MIN_10_EXP, DBL_MIN_10_EXP LDBL_MIN_10_EXP ≀ -37
  • FLT_MAX_10_EXP, DBL_MAX_10_EXP, LDBL_MAX_10_EXP β‰₯ +37
  • FLT_MAX, DBL_MAX, LDBL_MAX β‰₯ 1e+37 (5.2.4.2.2 / 10)
  • FLT_EPSILON ≀ 1e-5, DBL_EPSILON ≀ 1e-9, LDBL_EPSILON ≀ 1e-9 (5.2.4.2.2 / 11)
  • FLT_MIN, DBL_MIN, LDBL_MIN ≀ 1e-37

Treatment long double = double permitted by standard C.

+1
source

While the C standard does not require this, it STRONGLY WARNS that float and double are standard IEEE 754 single and double precision floating point types, respectively. That they are located on any architecture that supports them in hardware (which means almost everywhere).

Something a bit more complicated with long double , since not many architectures support floating point types with higher precision. The standard requires that a long double have at least a range and precision like double . This means that if the architecture does not support anything else, the type long double is identical to double . And even if it does (e.g. x87), some compilers still make long double equivalent to double (e.g. M $ VC), while others display an extended precision type like long double (e.g. Borland and GCC).

Even if the compiler provides an extended type of precision, there is still no standard for what β€œextended precision” means. On x87 it is 80-bit. Some other architectures have 128-bit four-point types. Even on x87, some compilers have sizeof(long double) = 10, while others use it to align, so it's 12 or 16 (out of 8 if long double is double ).

So, the bottom line, the implementation of long double varies across platforms. The only thing you can be sure of is that it is at least equivalent to double . If you want to write portable code, not depend on its presentation - keep it away from interfaces and binary I / O. Using a long double in your program's internal calculations is fine, though.

+1
source

You should also be aware that some floating point processors support several levels of accuracy for intermediate results, and this level can be controlled at runtime. Applications were affected by such things as buggy versions of DirectX libraries that choose a lower level of accuracy during library calls and forget to restore settings, which affects subsequent FP calculations in the caller.

0
source

Source: https://habr.com/ru/post/1306024/


All Articles